From 58a37ddccd5b23b87ee236b44f65337ec0cedcc8 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 5 May 2016 22:44:54 +0200 Subject: Factorize byte compression --- src/ch/epfl/xblast/server/GameStateSerializer.java | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/ch/epfl/xblast/server/GameStateSerializer.java b/src/ch/epfl/xblast/server/GameStateSerializer.java index a471962..a24ed95 100644 --- a/src/ch/epfl/xblast/server/GameStateSerializer.java +++ b/src/ch/epfl/xblast/server/GameStateSerializer.java @@ -33,7 +33,7 @@ public final class GameStateSerializer { * @return a list of byte corresponding to the blocks' images */ private static List serializeBoard(BoardPainter painter, Board board) { - return RunLengthEncoder.encode(Cell.SPIRAL_ORDER.stream() + return Collections.unmodifiableList(Cell.SPIRAL_ORDER.stream() .map(c -> painter.byteForCell(board, c)) .collect(Collectors.toList())); } @@ -81,7 +81,7 @@ public final class GameStateSerializer { * @return the serialized explosions */ private static List serializeExplosions(Map bombedCells, Set blastedCells, Board board) { - return RunLengthEncoder.encode(Cell.ROW_MAJOR_ORDER.stream() + return Collections.unmodifiableList(Cell.ROW_MAJOR_ORDER.stream() .map(c -> serializeExplosion(bombedCells, blastedCells, c, board)) .collect(Collectors.toList())); } @@ -125,13 +125,15 @@ public final class GameStateSerializer { } /** - * Prepends its size to a Byte List. + * Compresses and returns the given byte list using the run-length encoding. + * The length of the compressed sequence is prepended to the result. * - * @param l Byte List to be prefixed - * @return the Byte List prefixed with its size + * @param l Byte List to be compressed + * @return the compressed byte sequence prefixed by its size */ - private static List prependSize(List l) { - return Lists.prepended(l, (byte) l.size()); + private static List compress(List l) { + List rle = RunLengthEncoder.encode(l); + return Lists.prepended(rle, (byte) rle.size()); } /** @@ -143,8 +145,8 @@ public final class GameStateSerializer { */ public static List serialize(BoardPainter bp, GameState gs) { return Collections.unmodifiableList(Stream.of( - prependSize(serializeBoard(bp, gs.board())), - prependSize(serializeExplosions(gs.bombedCells(), gs.blastedCells(), gs.board())), + compress(serializeBoard(bp, gs.board())), + compress(serializeExplosions(gs.bombedCells(), gs.blastedCells(), gs.board())), serializePlayers(gs.players(), gs.ticks()), serializeRemainingTime(gs.remainingTime()) ).flatMap(List::stream).collect(Collectors.toList())); -- cgit v1.2.3