|
17 | 17 |
|
18 | 18 | package org.apache.ignite.spi.discovery.zk.internal; |
19 | 19 |
|
| 20 | +import java.io.BufferedOutputStream; |
20 | 21 | import java.io.ByteArrayInputStream; |
| 22 | +import java.io.IOException; |
21 | 23 | import java.io.Serializable; |
22 | 24 | import java.util.ArrayList; |
23 | 25 | import java.util.Arrays; |
|
39 | 41 | import java.util.concurrent.atomic.AtomicBoolean; |
40 | 42 | import java.util.concurrent.atomic.AtomicReference; |
41 | 43 | import java.util.zip.DataFormatException; |
42 | | -import java.util.zip.Deflater; |
| 44 | +import java.util.zip.DeflaterOutputStream; |
43 | 45 | import java.util.zip.Inflater; |
44 | 46 | import java.util.zip.InflaterInputStream; |
45 | 47 | import org.apache.ignite.Ignite; |
@@ -4064,33 +4066,21 @@ private <T> T unmarshalZip(byte[] zipBytes) throws Exception { |
4064 | 4066 |
|
4065 | 4067 | /** |
4066 | 4068 | * @param obj Object. |
4067 | | - * @return Bytes. |
| 4069 | + * @return Zip-compressed marshalled bytes. |
4068 | 4070 | * @throws IgniteCheckedException If failed. |
4069 | 4071 | */ |
4070 | 4072 | byte[] marshalZip(Object obj) throws IgniteCheckedException { |
4071 | 4073 | assert obj != null; |
4072 | 4074 |
|
4073 | | - return zip(U.marshal(marsh, obj)); |
4074 | | - } |
4075 | | - |
4076 | | - /** |
4077 | | - * @param bytes Bytes to compress. |
4078 | | - * @return Zip-compressed bytes. |
4079 | | - */ |
4080 | | - private static byte[] zip(byte[] bytes) { |
4081 | | - Deflater deflater = new Deflater(); |
4082 | | - |
4083 | | - deflater.setInput(bytes); |
4084 | | - deflater.finish(); |
| 4075 | + GridByteArrayOutputStream out = new GridByteArrayOutputStream(); |
4085 | 4076 |
|
4086 | | - GridByteArrayOutputStream out = new GridByteArrayOutputStream(bytes.length); |
4087 | | - |
4088 | | - final byte[] buf = new byte[bytes.length]; |
4089 | | - |
4090 | | - while (!deflater.finished()) { |
4091 | | - int cnt = deflater.deflate(buf); |
4092 | | - |
4093 | | - out.write(buf, 0, cnt); |
| 4077 | + // BufferedOutputStream's 8 KB buffer coalesces JdkMarshaller's ~1 KB ObjectOutputStream |
| 4078 | + // block-data writes into fewer Deflater JNI calls. |
| 4079 | + try (BufferedOutputStream zipOut = new BufferedOutputStream(new DeflaterOutputStream(out))) { |
| 4080 | + U.marshal(marsh, obj, zipOut); |
| 4081 | + } |
| 4082 | + catch (IOException e) { |
| 4083 | + throw new IgniteCheckedException("Failed to marshal object: " + obj, e); |
4094 | 4084 | } |
4095 | 4085 |
|
4096 | 4086 | return out.toByteArray(); |
|
0 commit comments