Game Development Reference
In-Depth Information
public int TestInteger
{
get { return testInteger; }
set { testInteger = value; }
}
internal TestObject()
{
testString = string.Empty;
testInteger = 0;
}
}
The next method is used to compress TestObject instances into a byte array contain-
ing the compressed data. You will notice that the code is very similar to compressing
arbitrary data except the BinaryFormatter is in charge of writing to the GZipStream .
internal static byte[] CompressTestObject(TestObject testObject)
{
try
{
using (MemoryStream output = new MemoryStream())
{
using (GZipStream zipStream = new GZipStream(output,
CompressionMode.Compress))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(zipStream, testObject);
}
return output.ToArray();
}
}
catch (Exception)
{
return null;
}
}
Decompression works the same as the compression method, except the input data
is decompressed and deserialized into a TestObject instance. This approach does
not require the data length to be written to the stream because BinaryFormatter
knows how big the class data is.
Search Nedrilad ::




Custom Search