|
2 | 2 |
|
3 | 3 | import dev.dewy.nbt.Tag; |
4 | 4 | import dev.dewy.nbt.TagType; |
| 5 | +import dev.dewy.nbt.utils.CompressionType; |
5 | 6 | import dev.dewy.nbt.utils.Pair; |
6 | 7 | import dev.dewy.nbt.utils.ReadFunction; |
7 | 8 |
|
8 | | -import java.io.DataInput; |
9 | | -import java.io.DataOutput; |
10 | | -import java.io.IOException; |
| 9 | +import java.io.*; |
11 | 10 | import java.util.HashMap; |
12 | 11 | import java.util.Map; |
| 12 | +import java.util.zip.GZIPInputStream; |
| 13 | +import java.util.zip.GZIPOutputStream; |
13 | 14 |
|
14 | 15 | /** |
15 | 16 | * Implementation of the compound tag. A map in its raw form. |
@@ -114,6 +115,24 @@ public void writeRoot(DataOutput output, String rootName) throws IOException { |
114 | 115 | write(output); |
115 | 116 | } |
116 | 117 |
|
| 118 | + /** |
| 119 | + * Write the compound tag to a {@link File} with a name of its own, using a given compression scheme. |
| 120 | + * |
| 121 | + * @param rootName The root compound's name. |
| 122 | + * @param file The file to be written to. |
| 123 | + * @param compression The compression to be applied. |
| 124 | + * @throws IOException If any IO error occurs. |
| 125 | + */ |
| 126 | + public void writeRootToFile(String rootName, File file, CompressionType compression) throws IOException { |
| 127 | + DataOutputStream out = compression == CompressionType.GZIP |
| 128 | + ? new DataOutputStream(new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) |
| 129 | + : new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); |
| 130 | + |
| 131 | + writeRoot(out, rootName); |
| 132 | + |
| 133 | + out.close(); |
| 134 | + } |
| 135 | + |
117 | 136 | @Override |
118 | 137 | public ReadFunction<DataInput, CompoundTag> getReader() { |
119 | 138 | return read; |
@@ -260,4 +279,35 @@ public static Pair<String, CompoundTag> readNamedRoot(DataInput input) throws IO |
260 | 279 |
|
261 | 280 | return new Pair<>(input.readUTF(), read.read(input)); |
262 | 281 | } |
| 282 | + |
| 283 | + /** |
| 284 | + * Reads a root compound (full NBT structure) from a {@link File} with a given kind of compression. |
| 285 | + * |
| 286 | + * @param file The file to read from. |
| 287 | + * @param compression The compression of the file. |
| 288 | + * @throws IOException if any kind of IO error occurs. |
| 289 | + * @return The root compound read from the file. |
| 290 | + */ |
| 291 | + public static CompoundTag readRootFromFile(File file, CompressionType compression) throws IOException { |
| 292 | + return readNamedRootFromFile(file, compression).getRight(); |
| 293 | + } |
| 294 | + |
| 295 | + /** |
| 296 | + * Reads a root compound (full NBT structure) from a {@link File} with a given kind of compression, with its name attached. |
| 297 | + * |
| 298 | + * @param file The file to read from. |
| 299 | + * @param compression The compression of the file. |
| 300 | + * @throws IOException if any kind of IO error occurs. |
| 301 | + * @return A {@link Pair} with the name of the root tag on the left and the root tag object on the right. |
| 302 | + */ |
| 303 | + public static Pair<String, CompoundTag> readNamedRootFromFile(File file, CompressionType compression) throws IOException { |
| 304 | + DataInputStream in = compression == CompressionType.GZIP |
| 305 | + ? new DataInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)))) |
| 306 | + : new DataInputStream(new BufferedInputStream(new FileInputStream(file))); |
| 307 | + |
| 308 | + Pair<String, CompoundTag> result = readNamedRoot(in); |
| 309 | + |
| 310 | + in.close(); |
| 311 | + return result; |
| 312 | + } |
263 | 313 | } |
0 commit comments