diff --git a/pom.xml b/pom.xml index 1a71daf2..4b5eee46 100644 --- a/pom.xml +++ b/pom.xml @@ -153,11 +153,6 @@ capsule 0.7.1 - - org.tukaani - xz - 1.9 - com.github.luben zstd-jni diff --git a/src/main/java/io/usethesource/vallang/io/binary/stream/Compressor.java b/src/main/java/io/usethesource/vallang/io/binary/stream/Compressor.java index 9d65c726..47df58ef 100644 --- a/src/main/java/io/usethesource/vallang/io/binary/stream/Compressor.java +++ b/src/main/java/io/usethesource/vallang/io/binary/stream/Compressor.java @@ -27,9 +27,6 @@ import java.io.OutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; -import org.tukaani.xz.LZMA2Options; -import org.tukaani.xz.XZInputStream; -import org.tukaani.xz.XZOutputStream; import com.github.luben.zstd.ZstdInputStream; import com.github.luben.zstd.ZstdOutputStream; import com.github.luben.zstd.util.Native; @@ -60,8 +57,8 @@ public void setLevel(int level) { result.setLevel(level); return result; } - case Header.Compression.XZ: { - return new XZOutputStream(rawStream, new LZMA2Options(level)); + case Header.Compression.UNSUPPORTED_XZ: { + throw new UnsupportedOperationException("XZ compression is not supported anymore, please use an older version of rascal/vallang to open the file and store it with a different level of compression"); } case Header.Compression.ZSTD: { return new ZstdOutputStream(rawStream, level); @@ -77,8 +74,8 @@ public static InputStream wrapStream(InputStream raw, int algorithm) throws IOEx return raw; case Header.Compression.GZIP: return new GZIPInputStream(raw); - case Header.Compression.XZ: - return new XZInputStream(raw); + case Header.Compression.UNSUPPORTED_XZ: + throw new UnsupportedOperationException("XZ compression is not supported anymore, please use an older version of rascal/vallang to open the file and store it with a different level of compression"); case Header.Compression.ZSTD: if (Compressor.zstdAvailable()) { if (raw instanceof ByteBufferInputStream && ((ByteBufferInputStream)raw).getByteBuffer().isDirect()) { diff --git a/src/main/java/io/usethesource/vallang/io/binary/stream/Header.java b/src/main/java/io/usethesource/vallang/io/binary/stream/Header.java index 878d44da..7ca9ed79 100644 --- a/src/main/java/io/usethesource/vallang/io/binary/stream/Header.java +++ b/src/main/java/io/usethesource/vallang/io/binary/stream/Header.java @@ -17,7 +17,8 @@ public static final class Compression { public static final byte NONE = 0; public static final byte GZIP = 1; - public static final byte XZ = 2; + /** Never officially available, but still reserved */ + public static final byte UNSUPPORTED_XZ = 2; public static final byte ZSTD = 3; } diff --git a/src/main/java/io/usethesource/vallang/io/binary/stream/IValueInputStream.java b/src/main/java/io/usethesource/vallang/io/binary/stream/IValueInputStream.java index 86aabf03..7775bf8e 100644 --- a/src/main/java/io/usethesource/vallang/io/binary/stream/IValueInputStream.java +++ b/src/main/java/io/usethesource/vallang/io/binary/stream/IValueInputStream.java @@ -16,6 +16,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.channels.FileChannel; +import java.util.Arrays; import java.util.function.Supplier; import org.checkerframework.checker.nullness.qual.Nullable; @@ -49,6 +50,9 @@ public IValueInputStream(InputStream in, IValueFactory vf, Supplier t while (read < currentHeader.length) { read += in.read(currentHeader, read, currentHeader.length - read); } + if (!Arrays.equals(currentHeader, Header.MAIN)) { + throw new IOException("Incorrect file header, expected: [" + toHex(Header.MAIN) + "] but found: [" + toHex(currentHeader) + "]"); + } int compression = in.read(); in = Compressor.wrapStream(in, compression); @@ -56,6 +60,15 @@ public IValueInputStream(InputStream in, IValueFactory vf, Supplier t } + private static String toHex(byte[] main) { + String result = ""; + for (byte b : main) { + result += String.format("%#04x ", b); + } + return result.trim(); + } + + public IValueInputStream(FileChannel channel, IValueFactory vf, Supplier typeStoreSupplier) throws IOException { this(new FileChannelDirectInputStream(channel), vf, typeStoreSupplier); } diff --git a/src/main/java/io/usethesource/vallang/io/binary/stream/IValueOutputStream.java b/src/main/java/io/usethesource/vallang/io/binary/stream/IValueOutputStream.java index 7a68603a..0d91f55e 100644 --- a/src/main/java/io/usethesource/vallang/io/binary/stream/IValueOutputStream.java +++ b/src/main/java/io/usethesource/vallang/io/binary/stream/IValueOutputStream.java @@ -55,7 +55,7 @@ public enum CompressionRate { Light(Header.Compression.ZSTD, 1), Normal(Header.Compression.ZSTD, 5), Strong(Header.Compression.ZSTD, 13), - Extreme(Header.Compression.XZ, 6), + Extreme(Header.Compression.ZSTD, 19), XML(Compression.NONE, 0) ;