Skip to content

Commit 7290cd8

Browse files
committed
Add TagLoader improvements and test suite
1 parent dbbb8ff commit 7290cd8

3 files changed

Lines changed: 94 additions & 4 deletions

File tree

‎src/main/java/org/glavo/nbt/internal/input/TagLoader.java‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import java.nio.file.StandardOpenOption;
3030
import java.util.Objects;
3131

32-
public abstract class TagLoader<T extends Tag, S> implements Tag.Loader<T, S> {
32+
public sealed abstract class TagLoader<T extends Tag, S> implements Tag.Loader<T, S> {
3333

3434
public static @Nullable Tag readTag(DataReader reader) throws IOException {
3535
byte tagByte = reader.readByte();
@@ -73,14 +73,14 @@ public abstract class TagLoader<T extends Tag, S> implements Tag.Loader<T, S> {
7373
protected final boolean autoDecompress;
7474

7575
protected TagLoader(Class<T> tagClass, MinecraftEdition edition, boolean autoDecompress) {
76-
this.tagClass = tagClass;
77-
this.edition = edition;
76+
this.tagClass = Objects.requireNonNull(tagClass, "tagClass");
77+
this.edition = Objects.requireNonNull(edition, "edition");
7878
this.autoDecompress = autoDecompress;
7979
}
8080

8181
protected final T check(@Nullable Tag tag) throws IOException {
8282
if (tag == null) {
83-
throw new IOException("Unexpected null tag");
83+
throw new IOException("Unexpected TAG_END");
8484
}
8585
try {
8686
return tagClass.cast(tag);
@@ -89,6 +89,16 @@ protected final T check(@Nullable Tag tag) throws IOException {
8989
}
9090
}
9191

92+
@Override
93+
public String toString() {
94+
return "Tag.Loader.%s[tagClass=%s, edition=%s, autoDecompress=%s]".formatted(
95+
getClass().getSimpleName(),
96+
tagClass.getSimpleName(),
97+
edition,
98+
autoDecompress
99+
);
100+
}
101+
92102
private static abstract class AbstractBuilder<T extends Tag, S> implements Tag.Loader.Builder<T, S> {
93103
protected final Class<T> tagClass;
94104
protected MinecraftEdition edition = MinecraftEdition.JAVA_EDITION;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2026 Glavo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.glavo.nbt.tag;
17+
18+
import org.glavo.nbt.TestResources;
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.EnumSource;
21+
22+
import java.io.IOException;
23+
import java.nio.channels.Channels;
24+
import java.nio.channels.FileChannel;
25+
import java.nio.channels.ReadableByteChannel;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
import java.nio.file.StandardOpenOption;
29+
30+
import static org.junit.jupiter.api.Assertions.*;
31+
32+
public final class TagLoaderTest {
33+
34+
enum Loader {
35+
INPUT_STREAM {
36+
@Override
37+
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
38+
return Tag.Loader.ofPath(tagClass).load(file);
39+
}
40+
},
41+
FILE_CHANNEL {
42+
@Override
43+
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
44+
try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ)) {
45+
return Tag.Loader.ofByteChannel(tagClass).load(channel);
46+
}
47+
}
48+
},
49+
READABLE_BYTE_CHANNEL {
50+
@Override
51+
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
52+
try (ReadableByteChannel channel = Channels.newChannel(Files.newInputStream(file))) {
53+
return Tag.Loader.ofByteChannel(tagClass).load(channel);
54+
}
55+
}
56+
},
57+
PATH {
58+
@Override
59+
<T extends Tag> T load(Path file, Class<T> tagClass) throws IOException {
60+
return Tag.Loader.ofPath(tagClass).load(file);
61+
}
62+
};
63+
64+
abstract <T extends Tag> T load(Path file, Class<T> tagClass) throws IOException;
65+
}
66+
67+
/// Loads a level.dat file (compressed with gzip)
68+
@ParameterizedTest
69+
@EnumSource
70+
void testLoadLevelDat(Loader loader) throws IOException {
71+
Path levelDatPath = TestResources.getResource("/assets/nbt/level.dat");
72+
CompoundTag levelDat = loader.load(levelDatPath, CompoundTag.class);
73+
assertEquals("", levelDat.getName());
74+
assertEquals(1, levelDat.size());
75+
76+
Tag dataTag = levelDat.get("Data");
77+
assertNotNull(dataTag);
78+
assertInstanceOf(CompoundTag.class, dataTag);
79+
}
80+
}
4.09 KB
Binary file not shown.

0 commit comments

Comments
 (0)