001 package net.minecraft.nbt; 002 003 import cpw.mods.fml.relauncher.Side; 004 import cpw.mods.fml.relauncher.SideOnly; 005 import java.io.BufferedInputStream; 006 import java.io.ByteArrayInputStream; 007 import java.io.ByteArrayOutputStream; 008 import java.io.DataInput; 009 import java.io.DataInputStream; 010 import java.io.DataOutput; 011 import java.io.DataOutputStream; 012 import java.io.File; 013 import java.io.FileInputStream; 014 import java.io.FileOutputStream; 015 import java.io.IOException; 016 import java.io.InputStream; 017 import java.io.OutputStream; 018 import java.util.zip.GZIPInputStream; 019 import java.util.zip.GZIPOutputStream; 020 021 public class CompressedStreamTools 022 { 023 /** 024 * Load the gzipped compound from the inputstream. 025 */ 026 public static NBTTagCompound readCompressed(InputStream par0InputStream) throws IOException 027 { 028 DataInputStream var1 = new DataInputStream(new BufferedInputStream(new GZIPInputStream(par0InputStream))); 029 NBTTagCompound var2; 030 031 try 032 { 033 var2 = read(var1); 034 } 035 finally 036 { 037 var1.close(); 038 } 039 040 return var2; 041 } 042 043 /** 044 * Write the compound, gzipped, to the outputstream. 045 */ 046 public static void writeCompressed(NBTTagCompound par0NBTTagCompound, OutputStream par1OutputStream) throws IOException 047 { 048 DataOutputStream var2 = new DataOutputStream(new GZIPOutputStream(par1OutputStream)); 049 050 try 051 { 052 write(par0NBTTagCompound, var2); 053 } 054 finally 055 { 056 var2.close(); 057 } 058 } 059 060 public static NBTTagCompound decompress(byte[] par0ArrayOfByte) throws IOException 061 { 062 DataInputStream var1 = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(par0ArrayOfByte)))); 063 NBTTagCompound var2; 064 065 try 066 { 067 var2 = read(var1); 068 } 069 finally 070 { 071 var1.close(); 072 } 073 074 return var2; 075 } 076 077 public static byte[] compress(NBTTagCompound par0NBTTagCompound) throws IOException 078 { 079 ByteArrayOutputStream var1 = new ByteArrayOutputStream(); 080 DataOutputStream var2 = new DataOutputStream(new GZIPOutputStream(var1)); 081 082 try 083 { 084 write(par0NBTTagCompound, var2); 085 } 086 finally 087 { 088 var2.close(); 089 } 090 091 return var1.toByteArray(); 092 } 093 094 @SideOnly(Side.CLIENT) 095 public static void safeWrite(NBTTagCompound par0NBTTagCompound, File par1File) throws IOException 096 { 097 File var2 = new File(par1File.getAbsolutePath() + "_tmp"); 098 099 if (var2.exists()) 100 { 101 var2.delete(); 102 } 103 104 write(par0NBTTagCompound, var2); 105 106 if (par1File.exists()) 107 { 108 par1File.delete(); 109 } 110 111 if (par1File.exists()) 112 { 113 throw new IOException("Failed to delete " + par1File); 114 } 115 else 116 { 117 var2.renameTo(par1File); 118 } 119 } 120 121 /** 122 * Reads from a CompressedStream. 123 */ 124 public static NBTTagCompound read(DataInput par0DataInput) throws IOException 125 { 126 NBTBase var1 = NBTBase.readNamedTag(par0DataInput); 127 128 if (var1 instanceof NBTTagCompound) 129 { 130 return (NBTTagCompound)var1; 131 } 132 else 133 { 134 throw new IOException("Root tag must be a named compound tag"); 135 } 136 } 137 138 public static void write(NBTTagCompound par0NBTTagCompound, DataOutput par1DataOutput) throws IOException 139 { 140 NBTBase.writeNamedTag(par0NBTTagCompound, par1DataOutput); 141 } 142 143 public static void write(NBTTagCompound par0NBTTagCompound, File par1File) throws IOException 144 { 145 DataOutputStream var2 = new DataOutputStream(new FileOutputStream(par1File)); 146 147 try 148 { 149 write(par0NBTTagCompound, var2); 150 } 151 finally 152 { 153 var2.close(); 154 } 155 } 156 157 public static NBTTagCompound read(File par0File) throws IOException 158 { 159 if (!par0File.exists()) 160 { 161 return null; 162 } 163 else 164 { 165 DataInputStream var1 = new DataInputStream(new FileInputStream(par0File)); 166 NBTTagCompound var2; 167 168 try 169 { 170 var2 = read(var1); 171 } 172 finally 173 { 174 var1.close(); 175 } 176 177 return var2; 178 } 179 } 180 }