-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathNBTWriter.java
79 lines (63 loc) · 1.77 KB
/
NBTWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package net.querz.nbt.io;
import net.querz.nbt.NamedTag;
import net.querz.nbt.Tag;
import net.querz.io.LittleEndianOutputStream;
import java.io.*;
import java.util.zip.GZIPOutputStream;
public final class NBTWriter {
private boolean compression;
private boolean littleEndian;
public NBTWriter compressed(boolean compressed) {
compression = compressed;
return this;
}
public NBTWriter littleEndian() {
littleEndian = true;
return this;
}
public NBTWriter bigEndian() {
littleEndian = false;
return this;
}
public void writeNamed(OutputStream out, NamedTag named) throws IOException {
writeNamed(out, named.name(), named.tag());
}
public void write(OutputStream out, Tag tag) throws IOException {
writeNamed(out, "", tag);
}
public void writeNamed(OutputStream out, String name, Tag tag) throws IOException {
OutputStream stream;
if (compression) {
stream = new BufferedOutputStream(new GZIPOutputStream(out));
} else {
stream = new BufferedOutputStream(out);
}
DataOutput output;
if (littleEndian) {
output = new LittleEndianOutputStream(stream);
} else {
output = new DataOutputStream(stream);
}
output.writeByte(tag.getType().id);
if (tag.getType() != Tag.Type.END) {
output.writeUTF(name);
tag.write(output);
}
stream.flush();
}
public void writeNamed(File file, NamedTag named) throws IOException {
try (FileOutputStream fos = new FileOutputStream(file)) {
writeNamed(fos, named);
}
}
public void write(File file, Tag tag) throws IOException {
try (FileOutputStream fos = new FileOutputStream(file)) {
write(fos, tag);
}
}
public void writeNamed(File file, String name, Tag tag) throws IOException {
try (FileOutputStream fos = new FileOutputStream(file)) {
writeNamed(fos, name, tag);
}
}
}