|
| 1 | +package org.dragonet.proxy.nbt; |
| 2 | + |
| 3 | +import org.dragonet.proxy.nbt.stream.NBTInputStream; |
| 4 | +import org.dragonet.proxy.nbt.stream.NBTOutputStream; |
| 5 | +import org.dragonet.proxy.nbt.tag.CompoundTag; |
| 6 | +import org.dragonet.proxy.nbt.tag.Tag; |
| 7 | + |
| 8 | +import java.io.*; |
| 9 | +import java.nio.ByteOrder; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.StandardCopyOption; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.zip.Deflater; |
| 14 | +import java.util.zip.DeflaterOutputStream; |
| 15 | +import java.util.zip.GZIPInputStream; |
| 16 | +import java.util.zip.GZIPOutputStream; |
| 17 | + |
| 18 | +public class NBTIO { |
| 19 | + |
| 20 | + /** |
| 21 | + * A Named Binary Tag library for Nukkit Project |
| 22 | + */ |
| 23 | + |
| 24 | + public static CompoundTag read(File file) throws IOException { |
| 25 | + return read(file, ByteOrder.BIG_ENDIAN); |
| 26 | + } |
| 27 | + |
| 28 | + public static CompoundTag read(File file, ByteOrder endianness) throws IOException { |
| 29 | + if (!file.exists()) return null; |
| 30 | + return read(new FileInputStream(file), endianness); |
| 31 | + } |
| 32 | + |
| 33 | + public static CompoundTag read(InputStream inputStream) throws IOException { |
| 34 | + return read(inputStream, ByteOrder.BIG_ENDIAN); |
| 35 | + } |
| 36 | + |
| 37 | + public static CompoundTag read(InputStream inputStream, ByteOrder endianness) throws IOException { |
| 38 | + return read(inputStream, endianness, false); |
| 39 | + } |
| 40 | + |
| 41 | + public static CompoundTag read(InputStream inputStream, ByteOrder endianness, boolean network) throws IOException { |
| 42 | + try (NBTInputStream stream = new NBTInputStream(inputStream, endianness, network)) { |
| 43 | + Tag tag = Tag.readNamedTag(stream); |
| 44 | + if (tag instanceof CompoundTag) { |
| 45 | + return (CompoundTag) tag; |
| 46 | + } |
| 47 | + throw new IOException("Root tag must be a named compound tag"); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static CompoundTag read(byte[] data) throws IOException { |
| 52 | + return read(data, ByteOrder.BIG_ENDIAN); |
| 53 | + } |
| 54 | + |
| 55 | + public static CompoundTag read(byte[] data, ByteOrder endianness) throws IOException { |
| 56 | + return read(new ByteArrayInputStream(data), endianness); |
| 57 | + } |
| 58 | + |
| 59 | + public static CompoundTag read(byte[] data, ByteOrder endianness, boolean network) throws IOException { |
| 60 | + return read(new ByteArrayInputStream(data), endianness, network); |
| 61 | + } |
| 62 | + |
| 63 | + public static CompoundTag readCompressed(InputStream inputStream) throws IOException { |
| 64 | + return readCompressed(inputStream, ByteOrder.BIG_ENDIAN); |
| 65 | + } |
| 66 | + |
| 67 | + public static CompoundTag readCompressed(InputStream inputStream, ByteOrder endianness) throws IOException { |
| 68 | + return read(new BufferedInputStream(new GZIPInputStream(inputStream)), endianness); |
| 69 | + } |
| 70 | + |
| 71 | + public static CompoundTag readCompressed(byte[] data) throws IOException { |
| 72 | + return readCompressed(data, ByteOrder.BIG_ENDIAN); |
| 73 | + } |
| 74 | + |
| 75 | + public static CompoundTag readCompressed(byte[] data, ByteOrder endianness) throws IOException { |
| 76 | + return read(new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(data))), endianness, true); |
| 77 | + } |
| 78 | + |
| 79 | + public static CompoundTag readNetworkCompressed(InputStream inputStream) throws IOException { |
| 80 | + return readNetworkCompressed(inputStream, ByteOrder.BIG_ENDIAN); |
| 81 | + } |
| 82 | + |
| 83 | + public static CompoundTag readNetworkCompressed(InputStream inputStream, ByteOrder endianness) throws IOException { |
| 84 | + return read(new BufferedInputStream(new GZIPInputStream(inputStream)), endianness); |
| 85 | + } |
| 86 | + |
| 87 | + public static CompoundTag readNetworkCompressed(byte[] data) throws IOException { |
| 88 | + return readNetworkCompressed(data, ByteOrder.BIG_ENDIAN); |
| 89 | + } |
| 90 | + |
| 91 | + public static CompoundTag readNetworkCompressed(byte[] data, ByteOrder endianness) throws IOException { |
| 92 | + return read(new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(data))), endianness, true); |
| 93 | + } |
| 94 | + |
| 95 | + public static byte[] write(CompoundTag tag) throws IOException { |
| 96 | + return write(tag, ByteOrder.BIG_ENDIAN); |
| 97 | + } |
| 98 | + |
| 99 | + public static byte[] write(CompoundTag tag, ByteOrder endianness) throws IOException { |
| 100 | + return write(tag, endianness, false); |
| 101 | + } |
| 102 | + |
| 103 | + public static byte[] write(CompoundTag tag, ByteOrder endianness, boolean network) throws IOException { |
| 104 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 105 | + try (NBTOutputStream stream = new NBTOutputStream(baos, endianness, network)) { |
| 106 | + Tag.writeNamedTag(tag, stream); |
| 107 | + return baos.toByteArray(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public static byte[] write(Collection<CompoundTag> tags) throws IOException { |
| 112 | + return write(tags, ByteOrder.BIG_ENDIAN); |
| 113 | + } |
| 114 | + |
| 115 | + public static byte[] write(Collection<CompoundTag> tags, ByteOrder endianness) throws IOException { |
| 116 | + return write(tags, endianness, false); |
| 117 | + } |
| 118 | + |
| 119 | + public static byte[] write(Collection<CompoundTag> tags, ByteOrder endianness, boolean network) throws IOException { |
| 120 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 121 | + try (NBTOutputStream stream = new NBTOutputStream(baos, endianness, network)) { |
| 122 | + for (CompoundTag tag : tags) { |
| 123 | + Tag.writeNamedTag(tag, stream); |
| 124 | + } |
| 125 | + return baos.toByteArray(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public static void write(CompoundTag tag, File file) throws IOException { |
| 130 | + write(tag, file, ByteOrder.BIG_ENDIAN); |
| 131 | + } |
| 132 | + |
| 133 | + public static void write(CompoundTag tag, File file, ByteOrder endianness) throws IOException { |
| 134 | + write(tag, new FileOutputStream(file), endianness); |
| 135 | + } |
| 136 | + |
| 137 | + public static void write(CompoundTag tag, OutputStream outputStream) throws IOException { |
| 138 | + write(tag, outputStream, ByteOrder.BIG_ENDIAN); |
| 139 | + } |
| 140 | + |
| 141 | + public static void write(CompoundTag tag, OutputStream outputStream, ByteOrder endianness) throws IOException { |
| 142 | + write(tag, outputStream, endianness, false); |
| 143 | + } |
| 144 | + |
| 145 | + public static void write(CompoundTag tag, OutputStream outputStream, ByteOrder endianness, boolean network) throws IOException { |
| 146 | + try (NBTOutputStream stream = new NBTOutputStream(outputStream, endianness, network)) { |
| 147 | + Tag.writeNamedTag(tag, stream); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public static byte[] writeGZIPCompressed(CompoundTag tag) throws IOException { |
| 152 | + return writeGZIPCompressed(tag, ByteOrder.BIG_ENDIAN); |
| 153 | + } |
| 154 | + |
| 155 | + public static byte[] writeGZIPCompressed(CompoundTag tag, ByteOrder endianness) throws IOException { |
| 156 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 157 | + writeGZIPCompressed(tag, baos, endianness); |
| 158 | + return baos.toByteArray(); |
| 159 | + } |
| 160 | + |
| 161 | + public static void writeGZIPCompressed(CompoundTag tag, OutputStream outputStream) throws IOException { |
| 162 | + writeGZIPCompressed(tag, outputStream, ByteOrder.BIG_ENDIAN); |
| 163 | + } |
| 164 | + |
| 165 | + public static void writeGZIPCompressed(CompoundTag tag, OutputStream outputStream, ByteOrder endianness) throws IOException { |
| 166 | + write(tag, new GZIPOutputStream(outputStream), endianness); |
| 167 | + } |
| 168 | + |
| 169 | + public static byte[] writeNetworkGZIPCompressed(CompoundTag tag) throws IOException { |
| 170 | + return writeNetworkGZIPCompressed(tag, ByteOrder.BIG_ENDIAN); |
| 171 | + } |
| 172 | + |
| 173 | + public static byte[] writeNetworkGZIPCompressed(CompoundTag tag, ByteOrder endianness) throws IOException { |
| 174 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 175 | + writeNetworkGZIPCompressed(tag, baos, endianness); |
| 176 | + return baos.toByteArray(); |
| 177 | + } |
| 178 | + |
| 179 | + public static void writeNetworkGZIPCompressed(CompoundTag tag, OutputStream outputStream) throws IOException { |
| 180 | + writeNetworkGZIPCompressed(tag, outputStream, ByteOrder.BIG_ENDIAN); |
| 181 | + } |
| 182 | + |
| 183 | + public static void writeNetworkGZIPCompressed(CompoundTag tag, OutputStream outputStream, ByteOrder endianness) throws IOException { |
| 184 | + write(tag, new GZIPOutputStream(outputStream), endianness, true); |
| 185 | + } |
| 186 | + |
| 187 | + public static void writeZLIBCompressed(CompoundTag tag, OutputStream outputStream) throws IOException { |
| 188 | + writeZLIBCompressed(tag, outputStream, ByteOrder.BIG_ENDIAN); |
| 189 | + } |
| 190 | + |
| 191 | + public static void writeZLIBCompressed(CompoundTag tag, OutputStream outputStream, ByteOrder endianness) throws IOException { |
| 192 | + writeZLIBCompressed(tag, outputStream, Deflater.DEFAULT_COMPRESSION, endianness); |
| 193 | + } |
| 194 | + |
| 195 | + public static void writeZLIBCompressed(CompoundTag tag, OutputStream outputStream, int level) throws IOException { |
| 196 | + writeZLIBCompressed(tag, outputStream, level, ByteOrder.BIG_ENDIAN); |
| 197 | + } |
| 198 | + |
| 199 | + public static void writeZLIBCompressed(CompoundTag tag, OutputStream outputStream, int level, ByteOrder endianness) throws IOException { |
| 200 | + write(tag, new DeflaterOutputStream(outputStream, new Deflater(level)), endianness); |
| 201 | + } |
| 202 | + |
| 203 | + public static void safeWrite(CompoundTag tag, File file) throws IOException { |
| 204 | + File tmpFile = new File(file.getAbsolutePath() + "_tmp"); |
| 205 | + if (tmpFile.exists()) { |
| 206 | + tmpFile.delete(); |
| 207 | + } |
| 208 | + write(tag, tmpFile); |
| 209 | + Files.move(tmpFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); |
| 210 | + } |
| 211 | + |
| 212 | +} |
0 commit comments