NbtIo .java

net.minecraft.nbt.NbtIo (class, 207 lines): members: writeUnnamedTagWithFallback, StringFallbackDataOutput, SYNC_OUTPUT_OPTIONS, parseCompressed, read.

net.minecraft.nbtclass207 linescommon6.6 KB

API Surface (17)

CompoundTag readCompressed(final Path file, final NbtAccounter accounter)
CompoundTag readCompressed(final InputStream in, final NbtAccounter accounter)
void parseCompressed(final Path file, final StreamTagVisitor output, final NbtAccounter accounter)
void parseCompressed(final InputStream in, final StreamTagVisitor output, final NbtAccounter accounter)
void writeCompressed(final CompoundTag tag, final Path file)
void writeCompressed(final CompoundTag tag, final OutputStream out)
void write(final CompoundTag tag, final Path file)
CompoundTag read(final DataInput input)
CompoundTag read(final DataInput input, final NbtAccounter accounter)
void write(final CompoundTag tag, final DataOutput output)
void parse(final DataInput input, final StreamTagVisitor output, final NbtAccounter accounter)
Tag readAnyTag(final DataInput input, final NbtAccounter accounter)
void writeAnyTag(final Tag tag, final DataOutput output)
void writeUnnamedTag(final Tag tag, final DataOutput output)
void writeUnnamedTagWithFallback(final Tag tag, final DataOutput output)
Tag readUnnamedTag(final DataInput input, final NbtAccounter accounter)
void writeUTF(final String s)

Fields & Constants (1)

SYNC_OUTPUT_OPTIONS

String literals (5)

"Root tag must be a named compound tag"
"Loading NBT data"
"NBT Tag"
"Tag type"
"Failed to write NBT String"

Full Source

NbtIo.java207 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.nbt;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import java.io.BufferedOutputStream;
5
import java.io.DataInput;
6
import java.io.DataInputStream;
7
import java.io.DataOutput;
8
import java.io.DataOutputStream;
9
import java.io.IOException;
10
import java.io.InputStream;
11
import java.io.OutputStream;
12
import java.io.UTFDataFormatException;
13
import java.nio.file.Files;
14
import java.nio.file.OpenOption;
15
import java.nio.file.Path;
16
import java.nio.file.StandardOpenOption;
17
import java.util.zip.GZIPInputStream;
18
import java.util.zip.GZIPOutputStream;
19
import net.minecraft.CrashReport;
20
import net.minecraft.CrashReportCategory;
21
import net.minecraft.util.DelegateDataOutput;
22
import net.minecraft.util.FastBufferedInputStream;
23
import net.minecraft.util.Util;
24
import org.jspecify.annotations.Nullable;
25
26
public class NbtIo {
27
	private static final OpenOption[] SYNC_OUTPUT_OPTIONS = new OpenOption[]{
28
		StandardOpenOption.SYNC, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING
29
	};
30
31
	public static CompoundTag readCompressed(final Path file, final NbtAccounter accounter) throws IOException {
32
		try (
33
			InputStream rawInput = Files.newInputStream(file);
34
			InputStream input = new FastBufferedInputStream(rawInput);
35
		) {
36
			return readCompressed(input, accounter);
37
		}
38
	}
39
40
	private static DataInputStream createDecompressorStream(final InputStream in) throws IOException {
41
		return new DataInputStream(new FastBufferedInputStream(new GZIPInputStream(in)));
42
	}
43
44
	private static DataOutputStream createCompressorStream(final OutputStream out) throws IOException {
45
		return new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(out)));
46
	}
47
48
	public static CompoundTag readCompressed(final InputStream in, final NbtAccounter accounter) throws IOException {
49
		try (DataInputStream dis = createDecompressorStream(in)) {
50
			return read(dis, accounter);
51
		}
52
	}
53
54
	public static void parseCompressed(final Path file, final StreamTagVisitor output, final NbtAccounter accounter) throws IOException {
55
		try (
56
			InputStream rawInput = Files.newInputStream(file);
57
			InputStream input = new FastBufferedInputStream(rawInput);
58
		) {
59
			parseCompressed(input, output, accounter);
60
		}
61
	}
62
63
	public static void parseCompressed(final InputStream in, final StreamTagVisitor output, final NbtAccounter accounter) throws IOException {
64
		try (DataInputStream dis = createDecompressorStream(in)) {
65
			parse(dis, output, accounter);
66
		}
67
	}
68
69
	public static void writeCompressed(final CompoundTag tag, final Path file) throws IOException {
70
		try (
71
			OutputStream out = Files.newOutputStream(file, SYNC_OUTPUT_OPTIONS);
72
			OutputStream bufferedOut = new BufferedOutputStream(out);
73
		) {
74
			writeCompressed(tag, bufferedOut);
75
		}
76
	}
77
78
	public static void writeCompressed(final CompoundTag tag, final OutputStream out) throws IOException {
79
		try (DataOutputStream dos = createCompressorStream(out)) {
80
			write(tag, dos);
81
		}
82
	}
83
84
	public static void write(final CompoundTag tag, final Path file) throws IOException {
85
		try (
86
			OutputStream out = Files.newOutputStream(file, SYNC_OUTPUT_OPTIONS);
87
			OutputStream bufferedOut = new BufferedOutputStream(out);
88
			DataOutputStream dos = new DataOutputStream(bufferedOut);
89
		) {
90
			write(tag, dos);
91
		}
92
	}
93
94
	public static @Nullable CompoundTag read(final Path file) throws IOException {
95
		if (!Files.exists(file)) {
96
			return null;
97
		}
98
99
		try (
100
			InputStream in = Files.newInputStream(file);
101
			DataInputStream dis = new DataInputStream(in);
102
		) {
103
			return read(dis, NbtAccounter.unlimitedHeap());
104
		}
105
	}
106
107
	public static CompoundTag read(final DataInput input) throws IOException {
108
		return read(input, NbtAccounter.unlimitedHeap());
109
	}
110
111
	public static CompoundTag read(final DataInput input, final NbtAccounter accounter) throws IOException {
112
		if (readUnnamedTag(input, accounter) instanceof CompoundTag compoundTag) {
113
			return compoundTag;
114
		} else {
115
			throw new IOException("Root tag must be a named compound tag");
116
		}
117
	}
118
119
	public static void write(final CompoundTag tag, final DataOutput output) throws IOException {
120
		writeUnnamedTagWithFallback(tag, output);
121
	}
122
123
	public static void parse(final DataInput input, final StreamTagVisitor output, final NbtAccounter accounter) throws IOException {
124
		TagType<?> type = TagTypes.getType(input.readByte());
125
		if (type == EndTag.TYPE) {
126
			if (output.visitRootEntry(EndTag.TYPE) == StreamTagVisitor.ValueResult.CONTINUE) {
127
				output.visitEnd();
128
			}
129
		} else {
130
			switch (output.visitRootEntry(type)) {
131
				case HALT:
132
				default:
133
					break;
134
				case BREAK:
135
					StringTag.skipString(input);
136
					type.skip(input, accounter);
137
					break;
138
				case CONTINUE:
139
					StringTag.skipString(input);
140
					type.parse(input, output, accounter);
141
			}
142
		}
143
	}
144
145
	public static Tag readAnyTag(final DataInput input, final NbtAccounter accounter) throws IOException {
146
		byte type = input.readByte();
147
		return type == 0 ? EndTag.INSTANCE : readTagSafe(input, accounter, type);
148
	}
149
150
	public static void writeAnyTag(final Tag tag, final DataOutput output) throws IOException {
151
		output.writeByte(tag.getId());
152
		if (tag.getId() != 0) {
153
			tag.write(output);
154
		}
155
	}
156
157
	public static void writeUnnamedTag(final Tag tag, final DataOutput output) throws IOException {
158
		output.writeByte(tag.getId());
159
		if (tag.getId() != 0) {
160
			output.writeUTF("");
161
			tag.write(output);
162
		}
163
	}
164
165
	public static void writeUnnamedTagWithFallback(final Tag tag, final DataOutput output) throws IOException {
166
		writeUnnamedTag(tag, new NbtIo.StringFallbackDataOutput(output));
167
	}
168
169
	@VisibleForTesting
170
	public static Tag readUnnamedTag(final DataInput input, final NbtAccounter accounter) throws IOException {
171
		byte type = input.readByte();
172
		if (type == 0) {
173
			return EndTag.INSTANCE;
174
		}
175
176
		StringTag.skipString(input);
177
		return readTagSafe(input, accounter, type);
178
	}
179
180
	private static Tag readTagSafe(final DataInput input, final NbtAccounter accounter, final byte type) {
181
		try {
182
			return TagTypes.getType(type).load(input, accounter);
183
		} catch (IOException e) {
184
			CrashReport report = CrashReport.forThrowable(e, "Loading NBT data");
185
			CrashReportCategory category = report.addCategory("NBT Tag");
186
			category.setDetail("Tag type", type);
187
			throw new ReportedNbtException(report);
188
		}
189
	}
190
191
	public static class StringFallbackDataOutput extends DelegateDataOutput {
192
		public StringFallbackDataOutput(final DataOutput parent) {
193
			super(parent);
194
		}
195
196
		@Override
197
		public void writeUTF(final String s) throws IOException {
198
			try {
199
				super.writeUTF(s);
200
			} catch (UTFDataFormatException exception) {
201
				Util.logAndPauseIfInIde("Failed to write NBT String", exception);
202
				super.writeUTF("");
203
			}
204
		}
205
	}
206
}
207

Decompiled Minecraft 26.3 source of NbtIo (net/minecraft/nbt/NbtIo.java) with Mojang official mappings, stored verbatim on the site.