1818package org .glavo .nbt .internal .input ;
1919
2020import net .jpountz .lz4 .LZ4BlockInputStream ;
21- import org .jetbrains .annotations .Nullable ;
2221
2322import java .io .EOFException ;
2423import java .io .IOException ;
2524import java .io .InputStream ;
2625import java .nio .ByteBuffer ;
27- import java .util .Objects ;
26+ import java .util .zip . GZIPInputStream ;
2827
29- public final class LZ4DataReader extends BoundedDataReader {
30- private final LZ4BlockInputStream lz4Stream ;
28+ public abstract class DecompressStreamDataReader extends BoundedDataReader {
29+ private static final boolean LZ4_AVAILABLE ;
3130
32- public LZ4DataReader (RawDataReader rawReader , long limit ) {
31+ static {
32+ boolean lz4Available = false ;
33+ try {
34+ Class .forName ("net.jpountz.lz4.LZ4BlockInputStream" , false , DecompressStreamDataReader .class .getClassLoader ());
35+ lz4Available = true ;
36+ } catch (ClassNotFoundException ignored ) {
37+ }
38+ LZ4_AVAILABLE = lz4Available ;
39+ }
40+
41+ public static DecompressStreamDataReader newGZipDataReader (RawDataReader rawReader , long limit ) throws IOException {
42+ return new DecompressStreamDataReader (rawReader , limit ) {
43+ @ Override
44+ protected InputStream newDecompressStream (InputStream rawInputStream ) throws IOException {
45+ return new GZIPInputStream (rawInputStream );
46+ }
47+ };
48+ }
49+
50+ public static DecompressStreamDataReader newLZ4DataReader (RawDataReader rawReader , long limit ) throws IOException {
51+ if (!LZ4_AVAILABLE ) {
52+ throw new IOException ("Missing LZ4 library, please add it to your classpath." );
53+ }
54+
55+ return new DecompressStreamDataReader (rawReader , limit ) {
56+ @ Override
57+ protected InputStream newDecompressStream (InputStream rawInputStream ) {
58+ return LZ4BlockInputStream .newBuilder ().build (rawInputStream );
59+ }
60+ };
61+ }
62+
63+ private final InputStream decompressStream ;
64+
65+ public DecompressStreamDataReader (RawDataReader rawReader , long limit ) throws IOException {
3366 super (rawReader , rawReader .getDecompressBuffer (), limit );
34- this .lz4Stream = LZ4BlockInputStream . newBuilder (). build (asInputStream ());
67+ this .decompressStream = newDecompressStream (asInputStream ());
3568
3669 assert getBuffer ().getByteBuffer ().hasArray ();
3770 }
3871
72+ protected abstract InputStream newDecompressStream (InputStream rawInputStream ) throws IOException ;
73+
3974 @ Override
4075 public void ensureBufferRemaining (int required ) throws IOException {
4176 if (getBuffer ().remaining () >= required ) {
@@ -54,7 +89,7 @@ public void ensureBufferRemaining(int required) throws IOException {
5489 byte [] array = output .array ();
5590 try {
5691 while (output .position () < required ) {
57- int n = lz4Stream .read (array , output .arrayOffset () + output .position (), output .remaining ());
92+ int n = decompressStream .read (array , output .arrayOffset () + output .position (), output .remaining ());
5893 if (n <= 0 ) {
5994 throw new EOFException ("Not enough data to read, required: " + required + ", remaining: " + (endPosition - getRawReader ().position ()));
6095 }
0 commit comments