|
| 1 | +package dev.dewy.nbt; |
| 2 | + |
| 3 | +import dev.dewy.nbt.tags.collection.CompoundTag; |
| 4 | +import lombok.NonNull; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.util.Base64; |
| 9 | +import java.util.zip.DeflaterOutputStream; |
| 10 | +import java.util.zip.GZIPInputStream; |
| 11 | +import java.util.zip.GZIPOutputStream; |
| 12 | +import java.util.zip.InflaterInputStream; |
| 13 | + |
| 14 | +/** |
| 15 | + * Standard interface for reading and writing NBT data structures. |
| 16 | + * |
| 17 | + * @author dewy |
| 18 | + */ |
| 19 | +public class Nbt { |
| 20 | + private @NonNull TagTypeRegistry typeRegistry; |
| 21 | + private final @NonNull NbtWriter writer; |
| 22 | + private final @NonNull NbtReader reader; |
| 23 | + |
| 24 | + /** |
| 25 | + * Constructs an instance of this class using a default {@link TagTypeRegistry} (supporting the standard 12 tag types). |
| 26 | + */ |
| 27 | + public Nbt() { |
| 28 | + this(new TagTypeRegistry()); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructs an instance of this class using a given {@link TagTypeRegistry}. |
| 33 | + * |
| 34 | + * @param typeRegistry the tag type registry to be used, typically containing custom tag entries. |
| 35 | + */ |
| 36 | + public Nbt(@NonNull TagTypeRegistry typeRegistry) { |
| 37 | + this.typeRegistry = typeRegistry; |
| 38 | + |
| 39 | + this.writer = new NbtWriter(typeRegistry); |
| 40 | + this.reader = new NbtReader(typeRegistry); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Writes the given root {@link CompoundTag} to a provided {@link DataOutput} stream. |
| 45 | + * |
| 46 | + * @param compound the NBT structure to write, contained within a {@link CompoundTag}. |
| 47 | + * @param output the stream to write to. |
| 48 | + * @throws IOException if any I/O error occurs. |
| 49 | + */ |
| 50 | + public void toStream(@NonNull CompoundTag compound, @NonNull DataOutput output) throws IOException { |
| 51 | + this.writer.toStream(compound, output); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Writes the given root {@link CompoundTag} to a {@link File} with no compression. |
| 56 | + * |
| 57 | + * @param compound the NBT structure to write, contained within a {@link CompoundTag}. |
| 58 | + * @param file the file to write to. |
| 59 | + * @throws IOException if any I/O error occurs. |
| 60 | + */ |
| 61 | + public void toFile(@NonNull CompoundTag compound, @NonNull File file) throws IOException { |
| 62 | + this.toFile(compound, file, CompressionType.NONE); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Writes the given root {@link CompoundTag} to a {@link File} using a certain {@link CompressionType}. |
| 67 | + * |
| 68 | + * @param compound the NBT structure to write, contained within a {@link CompoundTag}. |
| 69 | + * @param file the file to write to. |
| 70 | + * @param compression the compression to be applied. |
| 71 | + * @throws IOException if any I/O error occurs. |
| 72 | + */ |
| 73 | + public void toFile(@NonNull CompoundTag compound, @NonNull File file, @NonNull CompressionType compression) throws IOException { |
| 74 | + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); |
| 75 | + DataOutputStream dos = null; |
| 76 | + |
| 77 | + switch (compression) { |
| 78 | + case NONE: |
| 79 | + dos = new DataOutputStream(bos); |
| 80 | + break; |
| 81 | + case GZIP: |
| 82 | + dos = new DataOutputStream(new GZIPOutputStream(bos)); |
| 83 | + break; |
| 84 | + case ZLIB: |
| 85 | + dos = new DataOutputStream(new DeflaterOutputStream(bos)); |
| 86 | + } |
| 87 | + |
| 88 | + this.toStream(compound, dos); |
| 89 | + dos.close(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Converts the given root {@link CompoundTag} to a {@code byte[]} array. |
| 94 | + * |
| 95 | + * @param compound the NBT structure to write, contained within a {@link CompoundTag}. |
| 96 | + * @return the resulting {@code byte[]} array. |
| 97 | + * @throws IOException if any I/O error occurs. |
| 98 | + */ |
| 99 | + public byte[] toByteArray(@NonNull CompoundTag compound) throws IOException { |
| 100 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 101 | + DataOutputStream w = new DataOutputStream(new BufferedOutputStream(baos)); |
| 102 | + |
| 103 | + this.toStream(compound, w); |
| 104 | + w.flush(); |
| 105 | + |
| 106 | + return baos.toByteArray(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Converts the given root {@link CompoundTag} to a Base64 encoded string. |
| 111 | + * |
| 112 | + * @param compound the NBT structure to write, contained within a {@link CompoundTag}. |
| 113 | + * @return the resulting Base64 encoded string. |
| 114 | + * @throws IOException if any I/O error occurs. |
| 115 | + */ |
| 116 | + public String toBase64(@NonNull CompoundTag compound) throws IOException { |
| 117 | + return new String(Base64.getEncoder().encode(this.toByteArray(compound)), StandardCharsets.UTF_8); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Reads an NBT data structure (root {@link CompoundTag}) from a {@link DataInput} stream. |
| 122 | + * |
| 123 | + * @param input the stream to read from. |
| 124 | + * @return the root {@link CompoundTag} read from the stream. |
| 125 | + * @throws IOException if any I/O error occurs. |
| 126 | + */ |
| 127 | + public CompoundTag fromStream(@NonNull DataInput input) throws IOException { |
| 128 | + return this.reader.fromStream(input); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Reads an NBT data structure (root {@link CompoundTag}) from a {@link File}. |
| 133 | + * |
| 134 | + * @param file the file to read from. |
| 135 | + * @return the root {@link CompoundTag} read from the stream. |
| 136 | + * @throws IOException if any I/O error occurs. |
| 137 | + */ |
| 138 | + public CompoundTag fromFile(@NonNull File file) throws IOException { |
| 139 | + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); |
| 140 | + DataInputStream in; |
| 141 | + |
| 142 | + switch (CompressionType.getCompression(new FileInputStream(file))) { |
| 143 | + case NONE: |
| 144 | + in = new DataInputStream(bis); |
| 145 | + break; |
| 146 | + case GZIP: |
| 147 | + in = new DataInputStream(new GZIPInputStream(bis)); |
| 148 | + break; |
| 149 | + case ZLIB: |
| 150 | + in = new DataInputStream(new InflaterInputStream(bis)); |
| 151 | + break; |
| 152 | + default: |
| 153 | + throw new IllegalStateException("Illegal compression type. This should never happen."); |
| 154 | + } |
| 155 | + |
| 156 | + CompoundTag result = this.fromStream(in); |
| 157 | + in.close(); |
| 158 | + |
| 159 | + return result; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Reads an NBT data structure (root {@link CompoundTag}) from a {@code byte[]} array. |
| 164 | + * |
| 165 | + * @param bytes the {@code byte[]} array to read from. |
| 166 | + * @return the root {@link CompoundTag} read from the stream. |
| 167 | + * @throws IOException if any I/O error occurs. |
| 168 | + */ |
| 169 | + public CompoundTag fromByteArray(@NonNull byte[] bytes) throws IOException { |
| 170 | + return fromStream(new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(bytes)))); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Decodes an NBT data structure (root {@link CompoundTag}) from a Base64 encoded string. |
| 175 | + * |
| 176 | + * @param encoded the encoded Base64 string to decode. |
| 177 | + * @return the decoded root {@link CompoundTag}. |
| 178 | + * @throws IOException if any I/O error occurs. |
| 179 | + */ |
| 180 | + public CompoundTag fromBase64(@NonNull String encoded) throws IOException { |
| 181 | + return fromByteArray(Base64.getDecoder().decode(encoded)); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Returns the {@link TagTypeRegistry} currently in use by this instance. |
| 186 | + * |
| 187 | + * @return the {@link TagTypeRegistry} currently in use by this instance. |
| 188 | + */ |
| 189 | + public TagTypeRegistry getTypeRegistry() { |
| 190 | + return typeRegistry; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Sets the {@link TagTypeRegistry} currently in use by this instance. Used to utilise custom-made tag types. |
| 195 | + * |
| 196 | + * @param typeRegistry the new {@link TagTypeRegistry} to be set. |
| 197 | + */ |
| 198 | + public void setTypeRegistry(@NonNull TagTypeRegistry typeRegistry) { |
| 199 | + this.typeRegistry = typeRegistry; |
| 200 | + |
| 201 | + this.writer.setTypeRegistry(typeRegistry); |
| 202 | + this.reader.setTypeRegistry(typeRegistry); |
| 203 | + } |
| 204 | +} |
0 commit comments