Skip to content

Commit 9711767

Browse files
committed
feat: fetch upstream & bump to 3.0.9
1 parent 249e0a8 commit 9711767

File tree

7 files changed

+239
-66
lines changed

7 files changed

+239
-66
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ An Named Binary Tag library built including VarInt support introduced by Minecra
1717
<dependency>
1818
<groupId>com.github.AllayMC</groupId>
1919
<artifactId>NBT</artifactId>
20-
<version>3.0.8</version>
20+
<version>3.0.9</version>
2121
</dependency>
2222
</dependencies>
2323
```

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=3.0.8
1+
version=3.0.9

src/main/java/org/cloudburstmc/nbt/NBTInputStream.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.cloudburstmc.nbt;
22

3+
import org.cloudburstmc.nbt.util.stream.LimitedDataInput;
4+
35
import java.io.Closeable;
46
import java.io.DataInput;
57
import java.io.IOException;
@@ -9,6 +11,7 @@
911
import java.util.Objects;
1012

1113
import static org.cloudburstmc.nbt.NbtUtils.MAX_DEPTH;
14+
import static org.cloudburstmc.nbt.NbtUtils.MAX_READ_SIZE;
1215

1316
public class NBTInputStream implements Closeable {
1417
private final DataInput input;
@@ -17,7 +20,16 @@ public class NBTInputStream implements Closeable {
1720
private boolean closed = false;
1821

1922
public NBTInputStream(DataInput input, boolean internKeys, boolean internValues) {
20-
this.input = Objects.requireNonNull(input, "input");
23+
this(input, internKeys, internValues, MAX_READ_SIZE);
24+
}
25+
26+
public NBTInputStream(DataInput input, boolean internKeys, boolean internValues, long maxReadSize) {
27+
Objects.requireNonNull(input, "input");
28+
if (input instanceof LimitedDataInput) {
29+
this.input = input;
30+
} else {
31+
this.input = new LimitedDataInput(input, maxReadSize);
32+
}
2133
this.internKeys = internKeys;
2234
this.internValues = internValues;
2335
}
@@ -26,6 +38,10 @@ public NBTInputStream(DataInput input) {
2638
this(input, false, false);
2739
}
2840

41+
public NBTInputStream(DataInput input, long maxReadSize) {
42+
this(input, false, false, maxReadSize);
43+
}
44+
2945
public Object readTag() throws IOException {
3046
return readTag(MAX_DEPTH);
3147
}

src/main/java/org/cloudburstmc/nbt/NbtUtils.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,76 @@
2222

2323
public class NbtUtils {
2424
public static final int MAX_DEPTH = 16;
25+
public static final long MAX_READ_SIZE = 0; // Disabled by default
2526

2627
private NbtUtils() {
2728
}
2829

2930
public static NBTInputStream createReader(InputStream stream, boolean internKeys, boolean internValues) {
31+
return createReader(stream, internKeys, internValues, MAX_READ_SIZE);
32+
}
33+
34+
public static NBTInputStream createReader(InputStream stream, boolean internKeys, boolean internValues, long maxReadSize) {
3035
requireNonNull(stream, "stream");
31-
return new NBTInputStream(new DataInputStream(stream), internKeys, internValues);
36+
return new NBTInputStream(new DataInputStream(stream), internKeys, internValues, maxReadSize);
3237
}
3338

3439
public static NBTInputStream createReaderLE(InputStream stream, boolean internKeys, boolean internValues) {
40+
return createReaderLE(stream, internKeys, internValues, MAX_READ_SIZE);
41+
}
42+
43+
public static NBTInputStream createReaderLE(InputStream stream, boolean internKeys, boolean internValues, long maxReadSize) {
3544
requireNonNull(stream, "stream");
36-
return new NBTInputStream(new LittleEndianDataInputStream(stream), internKeys, internValues);
45+
return new NBTInputStream(new LittleEndianDataInputStream(stream, maxReadSize), internKeys, internValues);
3746
}
3847

3948
public static NBTInputStream createGZIPReader(InputStream stream, boolean internKeys, boolean internValues) throws IOException {
40-
return createReader(new GZIPInputStream(stream), internKeys, internValues);
49+
return createGZIPReader(stream, internKeys, internValues, MAX_READ_SIZE);
50+
}
51+
52+
public static NBTInputStream createGZIPReader(InputStream stream, boolean internKeys, boolean internValues, long maxReadSize) throws IOException {
53+
return createReader(new GZIPInputStream(stream), internKeys, internValues, maxReadSize);
4154
}
4255

4356
public static NBTInputStream createNetworkReader(InputStream stream, boolean internKeys, boolean internValues) {
57+
return createNetworkReader(stream, internKeys, internValues, MAX_READ_SIZE);
58+
}
59+
60+
public static NBTInputStream createNetworkReader(InputStream stream, boolean internKeys, boolean internValues, long maxReadSize) {
4461
requireNonNull(stream, "stream");
45-
return new NBTInputStream(new NetworkDataInputStream(stream), internKeys, internValues);
62+
return new NBTInputStream(new NetworkDataInputStream(stream, maxReadSize), internKeys, internValues);
4663
}
4764

4865
public static NBTInputStream createReader(InputStream stream) {
49-
return createReader(stream, false, false);
66+
return createReader(stream, MAX_READ_SIZE);
67+
}
68+
69+
public static NBTInputStream createReader(InputStream stream, long maxReadSize) {
70+
return createReader(stream, false, false, maxReadSize);
5071
}
5172

5273
public static NBTInputStream createReaderLE(InputStream stream) {
53-
return createReaderLE(stream, false, false);
74+
return createReaderLE(stream, MAX_READ_SIZE);
75+
}
76+
77+
public static NBTInputStream createReaderLE(InputStream stream, long maxReadSize) {
78+
return createReaderLE(stream, false, false, maxReadSize);
5479
}
5580

5681
public static NBTInputStream createGZIPReader(InputStream stream) throws IOException {
57-
return createGZIPReader(stream, false, false);
82+
return createGZIPReader(stream, MAX_READ_SIZE);
83+
}
84+
85+
public static NBTInputStream createGZIPReader(InputStream stream, long maxReadSize) throws IOException {
86+
return createGZIPReader(stream, false, false, maxReadSize);
5887
}
5988

6089
public static NBTInputStream createNetworkReader(InputStream stream) {
61-
return createNetworkReader(stream, false, false);
90+
return createNetworkReader(stream, MAX_READ_SIZE);
91+
}
92+
93+
public static NBTInputStream createNetworkReader(InputStream stream, long maxReadSize) {
94+
return createNetworkReader(stream, false, false, maxReadSize);
6295
}
6396

6497
public static NBTOutputStream createWriter(OutputStream stream) {
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package org.cloudburstmc.nbt.util.stream;
2+
3+
import org.cloudburstmc.nbt.NbtUtils;
4+
5+
import java.io.Closeable;
6+
import java.io.DataInput;
7+
import java.io.IOException;
8+
9+
public class LimitedDataInput implements DataInput, Closeable {
10+
11+
private final DataInput delegate;
12+
private final long maxReadSize;
13+
14+
private long readSize = 0;
15+
16+
public LimitedDataInput(DataInput delegate) {
17+
this(delegate, NbtUtils.MAX_READ_SIZE);
18+
}
19+
20+
public LimitedDataInput(DataInput delegate, long maxReadSize) {
21+
this.delegate = delegate;
22+
this.maxReadSize = maxReadSize;
23+
}
24+
25+
@Override
26+
public void close() throws IOException {
27+
if (delegate instanceof Closeable) {
28+
((Closeable) delegate).close();
29+
}
30+
}
31+
32+
protected void tryRead(int size) throws IOException {
33+
this.readSize += size;
34+
if (this.maxReadSize > 0 && this.readSize > this.maxReadSize) {
35+
throw new IOException("Read size exceeded limit: read=" + this.readSize + ", limit=" + this.maxReadSize);
36+
}
37+
}
38+
39+
@Override
40+
public void readFully(byte[] b) throws IOException {
41+
this.tryRead(b.length);
42+
this.delegate.readFully(b);
43+
}
44+
45+
@Override
46+
public void readFully(byte[] b, int off, int len) throws IOException {
47+
this.tryRead(len);
48+
this.delegate.readFully(b, off, len);
49+
}
50+
51+
@Override
52+
public int skipBytes(int n) throws IOException {
53+
this.tryRead(n);
54+
return this.delegate.skipBytes(n);
55+
}
56+
57+
@Override
58+
public boolean readBoolean() throws IOException {
59+
this.tryRead(1);
60+
return this.delegate.readBoolean();
61+
}
62+
63+
@Override
64+
public byte readByte() throws IOException {
65+
this.tryRead(1);
66+
return this.delegate.readByte();
67+
}
68+
69+
@Override
70+
public int readUnsignedByte() throws IOException {
71+
this.tryRead(1);
72+
return this.delegate.readUnsignedByte();
73+
}
74+
75+
@Override
76+
public short readShort() throws IOException {
77+
this.tryRead(2);
78+
return this.delegate.readShort();
79+
}
80+
81+
@Override
82+
public int readUnsignedShort() throws IOException {
83+
this.tryRead(2);
84+
return this.delegate.readUnsignedShort();
85+
}
86+
87+
@Override
88+
public char readChar() throws IOException {
89+
this.tryRead(2);
90+
return this.delegate.readChar();
91+
}
92+
93+
@Override
94+
public int readInt() throws IOException {
95+
this.tryRead(4);
96+
return this.delegate.readInt();
97+
}
98+
99+
@Override
100+
public long readLong() throws IOException {
101+
this.tryRead(8);
102+
return this.delegate.readLong();
103+
}
104+
105+
@Override
106+
public float readFloat() throws IOException {
107+
this.tryRead(4);
108+
return this.delegate.readFloat();
109+
}
110+
111+
@Override
112+
public double readDouble() throws IOException {
113+
this.tryRead(8);
114+
return this.delegate.readDouble();
115+
}
116+
117+
@Override
118+
public String readLine() throws IOException {
119+
String line = this.delegate.readLine();
120+
this.tryRead(line.length()); // not ideal, but we do not have a better way to estimate the size
121+
return line;
122+
}
123+
124+
@Override
125+
public String readUTF() throws IOException {
126+
String utf = this.delegate.readUTF();
127+
this.tryRead(utf.length()); // not ideal, but we do not have a better way to estimate the size
128+
return utf;
129+
}
130+
131+
public DataInput delegate() {
132+
return this.delegate;
133+
}
134+
}

0 commit comments

Comments
 (0)