diff options
| author | daoge_cmd <3523206925@qq.com> | 2026-03-01 12:16:08 +0800 |
|---|---|---|
| committer | daoge_cmd <3523206925@qq.com> | 2026-03-01 12:16:08 +0800 |
| commit | b691c43c44ff180d10e7d4a9afc83b98551ff586 (patch) | |
| tree | 3e9849222cbc6ba49f2f1fc6e5fe7179632c7390 /Minecraft.World/NbtIo.cpp | |
| parent | def8cb415354ac390b7e89052a50605285f1aca9 (diff) | |
Initial commit
Diffstat (limited to 'Minecraft.World/NbtIo.cpp')
| -rw-r--r-- | Minecraft.World/NbtIo.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/Minecraft.World/NbtIo.cpp b/Minecraft.World/NbtIo.cpp new file mode 100644 index 00000000..b6c63277 --- /dev/null +++ b/Minecraft.World/NbtIo.cpp @@ -0,0 +1,65 @@ +#include "stdafx.h" +#include "InputOutputStream.h" +#include "NbtIo.h" + +CompoundTag *NbtIo::readCompressed(InputStream *in) +{ + MemSect(26); + // 4J - this was using a try/finally block + DataInputStream dis = DataInputStream(in); // 4J - was new GZIPInputStream as well + CompoundTag *ret = NbtIo::read((DataInput *)&dis); + dis.close(); + MemSect(0); + return ret; +} + +void NbtIo::writeCompressed(CompoundTag *tag, OutputStream *out) +{ + // 4J - this was using a try/finally block + // 4J Stu - Buffer output in 1024 byte chunks so that we can allocate properly in the save file + BufferedOutputStream bos = BufferedOutputStream( out, 1024 ); + DataOutputStream dos = DataOutputStream(&bos); // 4J - was new GZIPOutputStream as well + NbtIo::write(tag, &dos); + dos.close(); +} + +// Reads tags from a stream created from the input buffer. Doesn't free the data in the source buffer. +CompoundTag *NbtIo::decompress(byteArray buffer) +{ + ByteArrayInputStream bais = ByteArrayInputStream(buffer); + // 4J - this was using a try/finally block + DataInputStream in = DataInputStream(&bais); // 4J - was new GZIPInputStream as well + CompoundTag *ret = NbtIo::read((DataInput *)&in); + bais.reset(); // This stops the buffer referenced by the input stream from being freed when it goes out of context + in.close(); + return ret; +} + +byteArray NbtIo::compress(CompoundTag *tag) +{ + // 4J - this was using a try/finally block + ByteArrayOutputStream baos = ByteArrayOutputStream(); + DataOutputStream dos = DataOutputStream(&baos); // 4J - was new GZIPOutputStream as well + NbtIo::write(tag, &dos); + + byteArray ret(baos.buf.length); + System::arraycopy(baos.buf,0,&ret,0,baos.buf.length); + dos.close(); + return ret; +} + +CompoundTag *NbtIo::read(DataInput *dis) +{ + Tag *tag = Tag::readNamedTag(dis); + + if( tag->getId() == Tag::TAG_Compound ) return (CompoundTag *)tag; + + if(tag!=NULL) delete tag; + // Root tag must be a named compound tag + return NULL; +} + +void NbtIo::write(CompoundTag *tag, DataOutput *dos) +{ + Tag::writeNamedTag(tag, dos); +}
\ No newline at end of file |
