Skip to content

Commit

Permalink
Pull in aircompressor to load ZStd NBT, fix JSON→NBT NPE
Browse files Browse the repository at this point in the history
  • Loading branch information
unascribed committed Aug 20, 2023
1 parent dceb309 commit ae33856
Show file tree
Hide file tree
Showing 44 changed files with 6,909 additions and 4 deletions.
11 changes: 11 additions & 0 deletions aircompressor-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ license {
matching('**/io/github/steveice10/opennbt/**') {
header = file('opennbt-LICENSE')
}
matching('**/io/airlift/compress/**') {
header = file('aircompressor-LICENSE')
}
}

tasks.build.dependsOn proguardJar
12 changes: 11 additions & 1 deletion src/main/java/com/unascribed/nbted/CommandProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public CommandProcessor(NBTTag _root, TagPrinter _printer, FileInfo _fileInfo) {
} else if (set.has("big-endian")) {
endianness = Endianness.BIG;
} else {
endianness = fileInfo.endianness;
endianness = fileInfo.endianness == null ? Endianness.BIG : fileInfo.endianness;
}
Compression compression;
if (set.has("compression")) {
Expand Down Expand Up @@ -495,6 +495,16 @@ public CommandProcessor(NBTTag _root, TagPrinter _printer, FileInfo _fileInfo) {
if (!prompt("You are saving an NBT file with a JSON extension. Are you sure you want to do this?", false)) {
return;
}
} else if (compression == Compression.ZSTD) {
if (outFile.getName().endsWith(".dat") || outFile.getName().endsWith(".nbt")) {
if (!prompt("You are saving a non-standard Zstd NBT file with a standard extension. Are you sure you want to do this?", true)) {
return;
}
} else if (!outFile.getName().endsWith(".zat") && !outFile.getName().endsWith(".znbt")) {
if (!prompt("You are saving a non-standard Zstd NBT file with an unknown extension. Are you sure you want to do this?", true)) {
return;
}
}
} else if (!outFile.getName().endsWith(".dat") && !outFile.getName().endsWith(".nbt")) {
if (!prompt("You are saving an NBT file with a nonstandard extension. Are you sure you want to do this?", true)) {
return;
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/unascribed/nbted/Compression.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@
import java.util.zip.GZIPOutputStream;
import java.util.zip.InflaterInputStream;

import io.airlift.compress.zstd.ZstdInputStream;
import io.airlift.compress.zstd.ZstdOutputStream;

public enum Compression {
NONE("None"),
DEFLATE("Deflate"),
GZIP("GZip");
GZIP("GZip"),
ZSTD("ZStandard"),
;
private final String name;
private Compression(String name) {
Compression(String name) {
this.name = name;
}

Expand All @@ -41,6 +46,7 @@ public InputStream wrap(InputStream is) throws IOException {
case NONE: return is;
case DEFLATE: return new InflaterInputStream(is);
case GZIP: return new GZIPInputStream(is);
case ZSTD: return new ZstdInputStream(is);
default: throw new AssertionError("missing case for "+this);
}
}
Expand All @@ -51,6 +57,7 @@ public OutputStream wrap(OutputStream os) throws IOException {
case NONE: return os;
case DEFLATE: return new DeflaterOutputStream(os);
case GZIP: return new GZIPOutputStream(os);
case ZSTD: return new ZstdOutputStream(os);
default: throw new AssertionError("missing case for "+this);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/unascribed/nbted/NBTEd.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ public static void main(String[] args) throws Exception {
detectedCompressionMethod = Compression.GZIP;
} else if (magic8 == 0x78) {
detectedCompressionMethod = Compression.DEFLATE;
} else if (magic16 == 0xb528) {
detectedCompressionMethod = Compression.ZSTD;
} else {
detectedCompressionMethod = Compression.NONE;
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/io/airlift/compress/Compressor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.airlift.compress;

import java.nio.ByteBuffer;

public interface Compressor
{
int maxCompressedLength(int uncompressedSize);

/**
* @return number of bytes written to the output
*/
int compress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength);

void compress(ByteBuffer input, ByteBuffer output);
}
29 changes: 29 additions & 0 deletions src/main/java/io/airlift/compress/Decompressor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.airlift.compress;

import java.nio.ByteBuffer;

public interface Decompressor
{
/**
* @return number of bytes written to the output
*/
int decompress(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength)
throws MalformedInputException;

void decompress(ByteBuffer input, ByteBuffer output)
throws MalformedInputException;
}
24 changes: 24 additions & 0 deletions src/main/java/io/airlift/compress/IncompatibleJvmException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.airlift.compress;

public class IncompatibleJvmException
extends RuntimeException
{
public IncompatibleJvmException(String message)
{
super(message);
}
}
37 changes: 37 additions & 0 deletions src/main/java/io/airlift/compress/MalformedInputException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.airlift.compress;

public class MalformedInputException
extends RuntimeException
{
private final long offset;

public MalformedInputException(long offset)
{
this(offset, "Malformed input");
}

public MalformedInputException(long offset, String reason)
{
super(reason + ": offset=" + offset);
this.offset = offset;
}

public long getOffset()
{
return offset;
}
}
Loading

0 comments on commit ae33856

Please # to comment.