| 1 | package net.minecraft.nbt;
|
| 2 |
|
| 3 | import java.io.BufferedInputStream;
|
| 4 | import java.io.BufferedOutputStream;
|
| 5 | import java.io.DataInput;
|
| 6 | import java.io.DataInputStream;
|
| 7 | import java.io.DataOutput;
|
| 8 | import java.io.DataOutputStream;
|
| 9 | import java.io.File;
|
| 10 | import java.io.FileInputStream;
|
| 11 | import java.io.FileOutputStream;
|
| 12 | import java.io.IOException;
|
| 13 | import java.io.InputStream;
|
| 14 | import java.io.OutputStream;
|
| 15 | import java.util.zip.GZIPInputStream;
|
| 16 | import java.util.zip.GZIPOutputStream;
|
| 17 | import net.minecraft.crash.CrashReport;
|
| 18 | import net.minecraft.crash.CrashReportCategory;
|
| 19 | import net.minecraft.util.ReportedException;
|
| 20 |
|
| 21 | public class CompressedStreamTools
|
| 22 | {
|
| 23 | /**
|
| 24 | * Load the gzipped compound from the inputstream.
|
| 25 | */
|
| 26 | public static NBTTagCompound readCompressed(InputStream is) throws IOException
|
| 27 | {
|
| 28 | DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(new GZIPInputStream(is)));
|
| 29 | NBTTagCompound nbttagcompound;
|
| 30 |
|
| 31 | try
|
| 32 | {
|
| 33 | nbttagcompound = read(datainputstream, NBTSizeTracker.INFINITE);
|
| 34 | }
|
| 35 | finally
|
| 36 | {
|
| 37 | datainputstream.close();
|
| 38 | }
|
| 39 |
|
| 40 | return nbttagcompound;
|
| 41 | }
|
| 42 |
|
| 43 | /**
|
| 44 | * Write the compound, gzipped, to the outputstream.
|
| 45 | */
|
| 46 | public static void writeCompressed(NBTTagCompound p_74799_0_, OutputStream outputStream) throws IOException
|
| 47 | {
|
| 48 | DataOutputStream dataoutputstream = new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(outputStream)));
|
| 49 |
|
| 50 | try
|
| 51 | {
|
| 52 | write(p_74799_0_, dataoutputstream);
|
| 53 | }
|
| 54 | finally
|
| 55 | {
|
| 56 | dataoutputstream.close();
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | public static void safeWrite(NBTTagCompound p_74793_0_, File p_74793_1_) throws IOException
|
| 61 | {
|
| 62 | File file1 = new File(p_74793_1_.getAbsolutePath() + "_tmp");
|
| 63 |
|
| 64 | if (file1.exists())
|
| 65 | {
|
| 66 | file1.delete();
|
| 67 | }
|
| 68 |
|
| 69 | write(p_74793_0_, file1);
|
| 70 |
|
| 71 | if (p_74793_1_.exists())
|
| 72 | {
|
| 73 | p_74793_1_.delete();
|
| 74 | }
|
| 75 |
|
| 76 | if (p_74793_1_.exists())
|
| 77 | {
|
| 78 | throw new IOException("Failed to delete " + p_74793_1_);
|
| 79 | }
|
| 80 | else
|
| 81 | {
|
| 82 | file1.renameTo(p_74793_1_);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | public static void write(NBTTagCompound p_74795_0_, File p_74795_1_) throws IOException
|
| 87 | {
|
| 88 | DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(p_74795_1_));
|
| 89 |
|
| 90 | try
|
| 91 | {
|
| 92 | write(p_74795_0_, dataoutputstream);
|
| 93 | }
|
| 94 | finally
|
| 95 | {
|
| 96 | dataoutputstream.close();
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | public static NBTTagCompound read(File p_74797_0_) throws IOException
|
| 101 | {
|
| 102 | if (!p_74797_0_.exists())
|
| 103 | {
|
| 104 | return null;
|
| 105 | }
|
| 106 | else
|
| 107 | {
|
| 108 | DataInputStream datainputstream = new DataInputStream(new FileInputStream(p_74797_0_));
|
| 109 | NBTTagCompound nbttagcompound;
|
| 110 |
|
| 111 | try
|
| 112 | {
|
| 113 | nbttagcompound = read(datainputstream, NBTSizeTracker.INFINITE);
|
| 114 | }
|
| 115 | finally
|
| 116 | {
|
| 117 | datainputstream.close();
|
| 118 | }
|
| 119 |
|
| 120 | return nbttagcompound;
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | /**
|
| 125 | * Reads from a CompressedStream.
|
| 126 | */
|
| 127 | public static NBTTagCompound read(DataInputStream inputStream) throws IOException
|
| 128 | {
|
| 129 | return read(inputStream, NBTSizeTracker.INFINITE);
|
| 130 | }
|
| 131 |
|
| 132 | /**
|
| 133 | * Reads the given DataInput, constructs, and returns an NBTTagCompound with the data from the DataInput
|
| 134 | */
|
| 135 | public static NBTTagCompound read(DataInput p_152456_0_, NBTSizeTracker p_152456_1_) throws IOException
|
| 136 | {
|
| 137 | NBTBase nbtbase = func_152455_a(p_152456_0_, 0, p_152456_1_);
|
| 138 |
|
| 139 | if (nbtbase instanceof NBTTagCompound)
|
| 140 | {
|
| 141 | return (NBTTagCompound)nbtbase;
|
| 142 | }
|
| 143 | else
|
| 144 | {
|
| 145 | throw new IOException("Root tag must be a named compound tag");
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | public static void write(NBTTagCompound p_74800_0_, DataOutput p_74800_1_) throws IOException
|
| 150 | {
|
| 151 | writeTag(p_74800_0_, p_74800_1_);
|
| 152 | }
|
| 153 |
|
| 154 | private static void writeTag(NBTBase p_150663_0_, DataOutput p_150663_1_) throws IOException
|
| 155 | {
|
| 156 | p_150663_1_.writeByte(p_150663_0_.getId());
|
| 157 |
|
| 158 | if (p_150663_0_.getId() != 0)
|
| 159 | {
|
| 160 | p_150663_1_.writeUTF("");
|
| 161 | p_150663_0_.write(p_150663_1_);
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | private static NBTBase func_152455_a(DataInput p_152455_0_, int p_152455_1_, NBTSizeTracker p_152455_2_) throws IOException
|
| 166 | {
|
| 167 | byte b0 = p_152455_0_.readByte();
|
| 168 |
|
| 169 | if (b0 == 0)
|
| 170 | {
|
| 171 | return new NBTTagEnd();
|
| 172 | }
|
| 173 | else
|
| 174 | {
|
| 175 | p_152455_0_.readUTF();
|
| 176 | NBTBase nbtbase = NBTBase.createNewByType(b0);
|
| 177 |
|
| 178 | try
|
| 179 | {
|
| 180 | nbtbase.read(p_152455_0_, p_152455_1_, p_152455_2_);
|
| 181 | return nbtbase;
|
| 182 | }
|
| 183 | catch (IOException ioexception)
|
| 184 | {
|
| 185 | CrashReport crashreport = CrashReport.makeCrashReport(ioexception, "Loading NBT data");
|
| 186 | CrashReportCategory crashreportcategory = crashreport.makeCategory("NBT Tag");
|
| 187 | crashreportcategory.addCrashSection("Tag name", "[UNNAMED TAG]");
|
| 188 | crashreportcategory.addCrashSection("Tag type", Byte.valueOf(b0));
|
| 189 | throw new ReportedException(crashreport);
|
| 190 | }
|
| 191 | }
|
| 192 | }
|
| 193 | }
|
| 194 |
|