1818package org .apache .rocketmq .client .java .misc ;
1919
2020import apache .rocketmq .v2 .ReceiveMessageRequest ;
21+ import com .github .luben .zstd .ZstdInputStream ;
22+ import com .github .luben .zstd .ZstdOutputStream ;
2123import java .io .ByteArrayInputStream ;
2224import java .io .ByteArrayOutputStream ;
25+ import java .io .FilterInputStream ;
26+ import java .io .FilterOutputStream ;
2327import java .io .IOException ;
28+ import java .io .InputStream ;
2429import java .lang .management .ManagementFactory ;
2530import java .lang .management .RuntimeMXBean ;
2631import java .net .InetAddress ;
3540import java .util .Random ;
3641import java .util .zip .CRC32 ;
3742import java .util .zip .DeflaterOutputStream ;
43+ import java .util .zip .GZIPInputStream ;
44+ import java .util .zip .GZIPOutputStream ;
3845import java .util .zip .InflaterInputStream ;
46+ import net .jpountz .lz4 .LZ4FrameInputStream ;
47+ import net .jpountz .lz4 .LZ4FrameOutputStream ;
3948import org .apache .commons .lang3 .StringUtils ;
4049
4150public class Utilities {
@@ -140,37 +149,59 @@ public static String hostName() {
140149 }
141150 }
142151
143- public static byte [] compressBytesGzip (final byte [] src , final int level ) throws IOException {
144- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (src .length );
152+ public static byte [] compressBytesGZIP (final byte [] src ) throws IOException {
153+ try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (src .length )) {
154+ try (FilterOutputStream outputStream = new GZIPOutputStream (byteArrayOutputStream )) {
155+ outputStream .write (src );
156+ outputStream .flush ();
157+ }
158+ return byteArrayOutputStream .toByteArray ();
159+ }
160+ }
161+
162+ public static byte [] compressBytesZSTD (final byte [] src , final int level ) throws IOException {
163+ try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (src .length )) {
164+ try (FilterOutputStream outputStream = new ZstdOutputStream (byteArrayOutputStream , level )) {
165+ outputStream .write (src );
166+ outputStream .flush ();
167+ }
168+ return byteArrayOutputStream .toByteArray ();
169+ }
170+ }
171+
172+ public static byte [] compressBytesZLIB (final byte [] src , final int level ) throws IOException {
145173 java .util .zip .Deflater defeater = new java .util .zip .Deflater (level );
146- DeflaterOutputStream deflaterOutputStream =
147- new DeflaterOutputStream ( byteArrayOutputStream , defeater );
148- try {
174+ try ( ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ( src . length );
175+ DeflaterOutputStream deflaterOutputStream =
176+ new DeflaterOutputStream ( byteArrayOutputStream , defeater )) {
149177 deflaterOutputStream .write (src );
150178 deflaterOutputStream .finish ();
151- deflaterOutputStream .close ();
152-
153179 return byteArrayOutputStream .toByteArray ();
154180 } finally {
155- try {
156- byteArrayOutputStream .close ();
157- } catch (IOException ignore ) {
158- // Exception not expected here.
159- }
160181 defeater .end ();
161182 }
162183 }
163184
164- public static byte [] uncompressBytesGzip (final byte [] src ) throws IOException {
185+ public static byte [] compressBytesLZ4 (byte [] src ) throws IOException {
186+ try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (src .length )) {
187+ try (FilterOutputStream outputStream = new LZ4FrameOutputStream (byteArrayOutputStream )) {
188+ outputStream .write (src );
189+ outputStream .flush ();
190+ }
191+ return byteArrayOutputStream .toByteArray ();
192+ }
193+ }
194+
195+ public static byte [] decompressBytes (final byte [] src ) throws IOException {
165196 byte [] uncompressData = new byte [src .length ];
166197
167198 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream (src );
168- InflaterInputStream inflaterInputStream = new InflaterInputStream ( byteArrayInputStream );
199+ FilterInputStream filterInputStream = getStreamByMagicCode ( src , byteArrayInputStream );
169200 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (src .length );
170201
171202 try {
172203 int length ;
173- while ((length = inflaterInputStream .read (uncompressData , 0 , uncompressData .length )) > 0 ) {
204+ while ((length = filterInputStream .read (uncompressData , 0 , uncompressData .length )) > 0 ) {
174205 byteArrayOutputStream .write (uncompressData , 0 , length );
175206 }
176207 byteArrayOutputStream .flush ();
@@ -183,7 +214,7 @@ public static byte[] uncompressBytesGzip(final byte[] src) throws IOException {
183214 // Exception not expected here.
184215 }
185216 try {
186- inflaterInputStream .close ();
217+ filterInputStream .close ();
187218 } catch (IOException ignore ) {
188219 // Exception not expected here.
189220 }
@@ -195,6 +226,29 @@ public static byte[] uncompressBytesGzip(final byte[] src) throws IOException {
195226 }
196227 }
197228
229+ private static FilterInputStream getStreamByMagicCode (byte [] src , InputStream inputStream ) throws IOException {
230+ // Automatically select the appropriate decompression algorithm according to magic code
231+ // GZIP magic code: 0x1F 0x8B
232+ // ZLIB magic code: 0x78
233+ // LZ4 magic code: 0x04 0x22 0x4D 0x18
234+ // ZSTD magic code: 0x28 0xB5 0x2F 0xFD
235+ FilterInputStream filterInputStream ;
236+ if ((src [0 ] & 0xFF ) == 0x1F && (src [1 ] & 0xFF ) == 0x8B ) {
237+ filterInputStream = new GZIPInputStream (inputStream );
238+ } else if ((src [0 ] & 0xFF ) == 0x78 ) {
239+ filterInputStream = new InflaterInputStream (inputStream );
240+ } else if ((src [0 ] & 0xFF ) == 0x04 && (src [1 ] & 0xFF ) == 0x22 && (src [2 ] & 0xFF ) == 0x4D
241+ && (src [3 ] & 0xFF ) == 0x18 ) {
242+ filterInputStream = new LZ4FrameInputStream (inputStream );
243+ } else if (((src [0 ] & 0xFF ) == 0x28 && (src [1 ] & 0xFF ) == 0xB5 && (src [2 ] & 0xFF ) == 0x2F
244+ && (src [3 ] & 0xFF ) == 0xFD )) {
245+ filterInputStream = new ZstdInputStream (inputStream );
246+ } else {
247+ throw new IOException ("Unknown compression format" );
248+ }
249+ return filterInputStream ;
250+ }
251+
198252 public static String encodeHexString (ByteBuffer byteBuffer , boolean toLowerCase ) {
199253 return new String (encodeHex (byteBuffer , toLowerCase ));
200254 }
0 commit comments