8
8
import java .util .zip .GZIPInputStream ;
9
9
import java .util .zip .GZIPOutputStream ;
10
10
11
- import com .github .luben .zstd .ZstdInputStream ;
12
- import com .github .luben .zstd .ZstdOutputStream ;
11
+ import com .github .luben .zstd .ZstdInputStreamNoFinalizer ;
12
+ import com .github .luben .zstd .ZstdOutputStreamNoFinalizer ;
13
13
import org .anarres .lzo .LzoAlgorithm ;
14
14
import org .anarres .lzo .LzoCompressor ;
15
15
import org .anarres .lzo .LzoLibrary ;
@@ -25,32 +25,13 @@ public class Encoder {
25
25
26
26
private Encoder () { }
27
27
28
- public static byte [] encode (Codec codec , byte [] input ) {
28
+ public static byte [] encode (Codec codec , byte [] input ) throws IOException {
29
29
if (codec == Codec .RAW ) {
30
30
return input ;
31
31
}
32
32
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
33
- OutputStream os ;
34
- try {
35
- switch (codec ) {
36
- case GZIP :
37
- os = new GZIPOutputStream (byteArrayOutputStream );
38
- break ;
39
- case ZSTD :
40
- os = new ZstdOutputStream (byteArrayOutputStream );
41
- break ;
42
- case LZOP :
43
- LzoCompressor lzoCompressor = LzoLibrary .getInstance ().newCompressor (LzoAlgorithm .LZO1X , null );
44
- os = new LzoOutputStream (byteArrayOutputStream , lzoCompressor );
45
- break ;
46
- case CUSTOM :
47
- default :
48
- throw new RuntimeException ("Unsupported codec: " + codec );
49
- }
33
+ try (OutputStream os = makeOutputStream (codec , byteArrayOutputStream )) {
50
34
os .write (input );
51
- os .close ();
52
- } catch (IOException exception ) {
53
- throw new RuntimeException (exception );
54
35
}
55
36
return byteArrayOutputStream .toByteArray ();
56
37
}
@@ -60,30 +41,49 @@ public static byte[] decode(Codec codec, byte[] input) throws IOException {
60
41
return input ;
61
42
}
62
43
63
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
64
- ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream (input );
65
- InputStream is ;
44
+ try (
45
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
46
+ ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream (input );
47
+ InputStream is = makeInputStream (codec , byteArrayInputStream )
48
+ ) {
49
+ byte [] buffer = new byte [1024 ];
50
+ int length ;
51
+ while ((length = is .read (buffer )) != -1 ) {
52
+ byteArrayOutputStream .write (buffer , 0 , length );
53
+ }
54
+ return byteArrayOutputStream .toByteArray ();
55
+ }
56
+ }
57
+
58
+ private static OutputStream makeOutputStream (Codec codec ,
59
+ ByteArrayOutputStream byteArrayOutputStream ) throws IOException {
66
60
switch (codec ) {
67
61
case GZIP :
68
- is = new GZIPInputStream (byteArrayInputStream );
69
- break ;
62
+ return new GZIPOutputStream (byteArrayOutputStream );
70
63
case ZSTD :
71
- is = new ZstdInputStream (byteArrayInputStream );
72
- break ;
64
+ return new ZstdOutputStreamNoFinalizer (byteArrayOutputStream );
73
65
case LZOP :
74
- is = new LzopInputStream ( byteArrayInputStream );
75
- break ;
66
+ LzoCompressor lzoCompressor = LzoLibrary . getInstance (). newCompressor ( LzoAlgorithm . LZO1X , null );
67
+ return new LzoOutputStream ( byteArrayOutputStream , lzoCompressor ) ;
76
68
case CUSTOM :
77
69
default :
78
70
throw new RuntimeException ("Unsupported codec: " + codec );
79
71
}
80
- byte [] buffer = new byte [1024 ];
81
- int length ;
82
- while ((length = is .read (buffer )) != -1 ) {
83
- byteArrayOutputStream .write (buffer , 0 , length );
72
+ }
73
+
74
+ private static InputStream makeInputStream (Codec codec ,
75
+ ByteArrayInputStream byteArrayInputStream ) throws IOException {
76
+ switch (codec ) {
77
+ case GZIP :
78
+ return new GZIPInputStream (byteArrayInputStream );
79
+ case ZSTD :
80
+ return new ZstdInputStreamNoFinalizer (byteArrayInputStream );
81
+ case LZOP :
82
+ return new LzopInputStream (byteArrayInputStream );
83
+ case CUSTOM :
84
+ default :
85
+ throw new RuntimeException ("Unsupported codec: " + codec );
84
86
}
85
- is .close ();
86
- return byteArrayOutputStream .toByteArray ();
87
87
}
88
88
89
89
}
0 commit comments