aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-05 22:44:54 +0200
committerPacien TRAN-GIRARD2016-05-05 22:44:54 +0200
commit58a37ddccd5b23b87ee236b44f65337ec0cedcc8 (patch)
treef99a7976958438b75955189763ee37943b0b3aa2
parentbf4eeeaca44404599b5f9c74e714e1830704c30c (diff)
downloadxblast-58a37ddccd5b23b87ee236b44f65337ec0cedcc8.tar.gz
Factorize byte compression
-rw-r--r--src/ch/epfl/xblast/server/GameStateSerializer.java20
1 files changed, 11 insertions, 9 deletions
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 {
33 * @return a list of byte corresponding to the blocks' images 33 * @return a list of byte corresponding to the blocks' images
34 */ 34 */
35 private static List<Byte> serializeBoard(BoardPainter painter, Board board) { 35 private static List<Byte> serializeBoard(BoardPainter painter, Board board) {
36 return RunLengthEncoder.encode(Cell.SPIRAL_ORDER.stream() 36 return Collections.unmodifiableList(Cell.SPIRAL_ORDER.stream()
37 .map(c -> painter.byteForCell(board, c)) 37 .map(c -> painter.byteForCell(board, c))
38 .collect(Collectors.toList())); 38 .collect(Collectors.toList()));
39 } 39 }
@@ -81,7 +81,7 @@ public final class GameStateSerializer {
81 * @return the serialized explosions 81 * @return the serialized explosions
82 */ 82 */
83 private static List<Byte> serializeExplosions(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Board board) { 83 private static List<Byte> serializeExplosions(Map<Cell, Bomb> bombedCells, Set<Cell> blastedCells, Board board) {
84 return RunLengthEncoder.encode(Cell.ROW_MAJOR_ORDER.stream() 84 return Collections.unmodifiableList(Cell.ROW_MAJOR_ORDER.stream()
85 .map(c -> serializeExplosion(bombedCells, blastedCells, c, board)) 85 .map(c -> serializeExplosion(bombedCells, blastedCells, c, board))
86 .collect(Collectors.toList())); 86 .collect(Collectors.toList()));
87 } 87 }
@@ -125,13 +125,15 @@ public final class GameStateSerializer {
125 } 125 }
126 126
127 /** 127 /**
128 * Prepends its size to a Byte List. 128 * Compresses and returns the given byte list using the run-length encoding.
129 * The length of the compressed sequence is prepended to the result.
129 * 130 *
130 * @param l Byte List to be prefixed 131 * @param l Byte List to be compressed
131 * @return the Byte List prefixed with its size 132 * @return the compressed byte sequence prefixed by its size
132 */ 133 */
133 private static List<Byte> prependSize(List<Byte> l) { 134 private static List<Byte> compress(List<Byte> l) {
134 return Lists.prepended(l, (byte) l.size()); 135 List<Byte> rle = RunLengthEncoder.encode(l);
136 return Lists.prepended(rle, (byte) rle.size());
135 } 137 }
136 138
137 /** 139 /**
@@ -143,8 +145,8 @@ public final class GameStateSerializer {
143 */ 145 */
144 public static List<Byte> serialize(BoardPainter bp, GameState gs) { 146 public static List<Byte> serialize(BoardPainter bp, GameState gs) {
145 return Collections.unmodifiableList(Stream.of( 147 return Collections.unmodifiableList(Stream.of(
146 prependSize(serializeBoard(bp, gs.board())), 148 compress(serializeBoard(bp, gs.board())),
147 prependSize(serializeExplosions(gs.bombedCells(), gs.blastedCells(), gs.board())), 149 compress(serializeExplosions(gs.bombedCells(), gs.blastedCells(), gs.board())),
148 serializePlayers(gs.players(), gs.ticks()), 150 serializePlayers(gs.players(), gs.ticks()),
149 serializeRemainingTime(gs.remainingTime()) 151 serializeRemainingTime(gs.remainingTime())
150 ).flatMap(List::stream).collect(Collectors.toList())); 152 ).flatMap(List::stream).collect(Collectors.toList()));