@@ -50,7 +50,7 @@ public class CompressedMessage extends AbstractMessage implements NonMarshallabl
5050 */
5151 private static final int MAX_DEFLATE_BLOCK_OVERHEAD = 64 ;
5252
53- /** Compressed data chunks: filled by {@link #compress(ByteBuffer) } on send, by the serializer on receive. */
53+ /** Compressed data chunks: filled by {@link Compressor } on send, by the serializer on receive. */
5454 List <byte []> chunks ;
5555
5656 /** Index of the next chunk to send. */
@@ -65,26 +65,11 @@ public class CompressedMessage extends AbstractMessage implements NonMarshallabl
6565 /** Flag indicating whether this is the last chunk. */
6666 boolean finalChunk ;
6767
68- /** Compression level. */
69- private int compressionLvl ;
70-
7168 /** Constructor. */
7269 public CompressedMessage () {
7370 // No-op.
7471 }
7572
76- /**
77- * @param buf Source buffer with serialized data.
78- * @param compressionLvl Compression level.
79- */
80- public CompressedMessage (ByteBuffer buf , int compressionLvl ) {
81- dataSize = buf .remaining ();
82- this .compressionLvl = compressionLvl ;
83-
84- if (dataSize > 0 )
85- compress (buf );
86- }
87-
8873 /** @return Raw data size. */
8974 public int dataSize () {
9075 return dataSize ;
@@ -102,40 +87,6 @@ public byte[] nextChunk() {
10287 return chunkIdx < chunks .size () ? chunks .get (chunkIdx ++) : null ;
10388 }
10489
105- /** @param buf Buffer. */
106- private void compress (ByteBuffer buf ) {
107- Deflater deflater = new Deflater (compressionLvl , true );
108-
109- try {
110- deflater .setInput (buf );
111- deflater .finish ();
112-
113- chunks = new ArrayList <>(dataSize / CHUNK_SIZE + 1 );
114-
115- // Incompressible data may expand: raw-deflate worst case below CHUNK_SIZE is one stored block (~11 bytes overhead).
116- byte [] chunk0 = new byte [dataSize >= CHUNK_SIZE ? CHUNK_SIZE : dataSize + 16 ];
117-
118- int len = 0 ;
119-
120- while (!deflater .finished ()) {
121- len += deflater .deflate (chunk0 , len , chunk0 .length - len );
122-
123- if (len == chunk0 .length && !deflater .finished ()) {
124- chunks .add (chunk0 );
125-
126- chunk0 = new byte [CHUNK_SIZE ];
127- len = 0 ;
128- }
129- }
130-
131- if (len > 0 )
132- chunks .add (len == chunk0 .length ? chunk0 : Arrays .copyOf (chunk0 , len ));
133- }
134- finally {
135- deflater .end ();
136- }
137- }
138-
13990 /** @return Uncompressed data. */
14091 private byte [] uncompress () {
14192 if (chunks == null )
@@ -201,4 +152,85 @@ private byte[] uncompress() {
201152 @ Override public String toString () {
202153 return S .toString (CompressedMessage .class , this );
203154 }
155+
156+ /** Compresses serialized data piece by piece, so that the data never has to be held in one buffer. */
157+ public static class Compressor implements AutoCloseable {
158+ /** */
159+ private final Deflater deflater ;
160+
161+ /** */
162+ private final List <byte []> chunks = new ArrayList <>();
163+
164+ /** Chunk being filled. */
165+ private byte [] chunk ;
166+
167+ /** Number of bytes in {@link #chunk}. */
168+ private int chunkLen ;
169+
170+ /** Raw data size. */
171+ private int dataSize ;
172+
173+ /** @param compressionLvl Compression level. */
174+ public Compressor (int compressionLvl ) {
175+ deflater = new Deflater (compressionLvl , true );
176+ }
177+
178+ /**
179+ * Compresses the next piece of serialized data. The piece is consumed fully, so its buffer can be reused for
180+ * the next piece.
181+ *
182+ * @param piece Piece of serialized data.
183+ */
184+ public void compress (ByteBuffer piece ) {
185+ // Incompressible data may expand: raw-deflate worst case below CHUNK_SIZE is one stored block (~11 bytes overhead).
186+ if (chunk == null )
187+ chunk = new byte [Math .min (CHUNK_SIZE , piece .remaining () + 16 )];
188+
189+ dataSize += piece .remaining ();
190+
191+ deflater .setInput (piece );
192+
193+ deflate (false );
194+ }
195+
196+ /** @return Message with the compressed data. */
197+ public CompressedMessage finish () {
198+ CompressedMessage msg = new CompressedMessage ();
199+
200+ msg .dataSize = dataSize ;
201+
202+ if (dataSize > 0 ) {
203+ deflater .finish ();
204+
205+ deflate (true );
206+
207+ if (chunkLen > 0 )
208+ chunks .add (chunkLen == chunk .length ? chunk : Arrays .copyOf (chunk , chunkLen ));
209+
210+ msg .chunks = chunks ;
211+ }
212+
213+ return msg ;
214+ }
215+
216+ /** @param finish Whether to deflate until the stream is finished rather than until the input is consumed. */
217+ private void deflate (boolean finish ) {
218+ do {
219+ if (chunkLen == chunk .length ) {
220+ chunks .add (chunk );
221+
222+ chunk = new byte [CHUNK_SIZE ];
223+ chunkLen = 0 ;
224+ }
225+
226+ chunkLen += deflater .deflate (chunk , chunkLen , chunk .length - chunkLen );
227+ }
228+ while (finish ? !deflater .finished () : !deflater .needsInput ());
229+ }
230+
231+ /** {@inheritDoc} */
232+ @ Override public void close () {
233+ deflater .end ();
234+ }
235+ }
204236}
0 commit comments