Skip to content

Commit c9328a3

Browse files
committed
Refactor decompression readers to factory methods
1 parent 4ccf63d commit c9328a3

2 files changed

Lines changed: 44 additions & 15 deletions

File tree

src/main/java/org/glavo/nbt/chunk/ChunkRegion.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,10 @@ static ChunkRegion readRegion(RawDataReader rawReader) throws IOException {
9191
}
9292

9393
BoundedDataReader reader = switch (compressType) {
94-
case 1 -> throw new IOException("GZip compression is not supported yet.");
94+
case 1 -> DecompressStreamDataReader.newGZipDataReader(rawReader, chunkRawContentLength);
9595
case 2 -> new ZlibDataReader(rawReader, chunkRawContentLength);
9696
case 3 -> new UncompressedDataReader(rawReader, chunkRawContentLength);
97-
case 4 -> {
98-
try {
99-
yield new LZ4DataReader(rawReader, chunkRawContentLength);
100-
} catch (NoClassDefFoundError e) {
101-
throw new IOException("Missing dependency for LZ4 compression: " + e.getMessage(), e);
102-
}
103-
}
97+
case 4 -> DecompressStreamDataReader.newLZ4DataReader(rawReader, chunkRawContentLength);
10498
default -> throw new IOException("Unsupported compression type: " + compressType);
10599
};
106100

src/main/java/org/glavo/nbt/internal/input/LZ4DataReader.java renamed to src/main/java/org/glavo/nbt/internal/input/DecompressStreamDataReader.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,59 @@
1818
package org.glavo.nbt.internal.input;
1919

2020
import net.jpountz.lz4.LZ4BlockInputStream;
21-
import org.jetbrains.annotations.Nullable;
2221

2322
import java.io.EOFException;
2423
import java.io.IOException;
2524
import java.io.InputStream;
2625
import 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

Comments
 (0)