001package net.minecraft.nbt;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.io.BufferedInputStream;
006import java.io.ByteArrayInputStream;
007import java.io.ByteArrayOutputStream;
008import java.io.DataInput;
009import java.io.DataInputStream;
010import java.io.DataOutput;
011import java.io.DataOutputStream;
012import java.io.File;
013import java.io.FileInputStream;
014import java.io.FileOutputStream;
015import java.io.IOException;
016import java.io.InputStream;
017import java.io.OutputStream;
018import java.util.zip.GZIPInputStream;
019import java.util.zip.GZIPOutputStream;
020
021public 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 datainputstream = new DataInputStream(new BufferedInputStream(new GZIPInputStream(par0InputStream)));
029        NBTTagCompound nbttagcompound;
030
031        try
032        {
033            nbttagcompound = read(datainputstream);
034        }
035        finally
036        {
037            datainputstream.close();
038        }
039
040        return nbttagcompound;
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 dataoutputstream = new DataOutputStream(new GZIPOutputStream(par1OutputStream));
049
050        try
051        {
052            write(par0NBTTagCompound, dataoutputstream);
053        }
054        finally
055        {
056            dataoutputstream.close();
057        }
058    }
059
060    public static NBTTagCompound decompress(byte[] par0ArrayOfByte) throws IOException
061    {
062        DataInputStream datainputstream = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(par0ArrayOfByte))));
063        NBTTagCompound nbttagcompound;
064
065        try
066        {
067            nbttagcompound = read(datainputstream);
068        }
069        finally
070        {
071            datainputstream.close();
072        }
073
074        return nbttagcompound;
075    }
076
077    public static byte[] compress(NBTTagCompound par0NBTTagCompound) throws IOException
078    {
079        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
080        DataOutputStream dataoutputstream = new DataOutputStream(new GZIPOutputStream(bytearrayoutputstream));
081
082        try
083        {
084            write(par0NBTTagCompound, dataoutputstream);
085        }
086        finally
087        {
088            dataoutputstream.close();
089        }
090
091        return bytearrayoutputstream.toByteArray();
092    }
093
094    @SideOnly(Side.CLIENT)
095    public static void safeWrite(NBTTagCompound par0NBTTagCompound, File par1File) throws IOException
096    {
097        File file2 = new File(par1File.getAbsolutePath() + "_tmp");
098
099        if (file2.exists())
100        {
101            file2.delete();
102        }
103
104        write(par0NBTTagCompound, file2);
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            file2.renameTo(par1File);
118        }
119    }
120
121    /**
122     * Reads from a CompressedStream.
123     */
124    public static NBTTagCompound read(DataInput par0DataInput) throws IOException
125    {
126        NBTBase nbtbase = NBTBase.readNamedTag(par0DataInput);
127
128        if (nbtbase instanceof NBTTagCompound)
129        {
130            return (NBTTagCompound)nbtbase;
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 dataoutputstream = new DataOutputStream(new FileOutputStream(par1File));
146
147        try
148        {
149            write(par0NBTTagCompound, dataoutputstream);
150        }
151        finally
152        {
153            dataoutputstream.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 datainputstream = new DataInputStream(new FileInputStream(par0File));
166            NBTTagCompound nbttagcompound;
167
168            try
169            {
170                nbttagcompound = read(datainputstream);
171            }
172            finally
173            {
174                datainputstream.close();
175            }
176
177            return nbttagcompound;
178        }
179    }
180}