public object Decode(ITranscoder transcoder, byte[] bytes, bool isNativeFiile) { lock (this) { buffer.Clear(); if (isNativeFiile) { return(null); } buffer.WriteBytes(bytes); if (buffer.ReadShort() != MAGIC_NUMBER) { throw new ApplicationException("bytes have not used TranscoderManager encode."); } byte compress = buffer.ReadByte(); int len = buffer.ReadInt(); if (compress == 1) { byte[] tempBytes = new byte[len]; buffer.ReadBytes(tempBytes, 0, len); buffer.Clear(); using (MemoryStream ms = new MemoryStream(tempBytes)) { ms.Flush(); using (GZipStream gipStream = new GZipStream(ms, CompressionMode.Decompress)) { using (MemoryStream outBuffer = new MemoryStream()) { byte[] block = new byte[1024]; while (true) { int bytesRead = gipStream.Read(block, 0, block.Length); if (bytesRead <= 0) { break; } else { outBuffer.Write(block, 0, bytesRead); } } buffer.WriteBytes(outBuffer.ToArray()); } } } } return(transcoder.Decode(buffer)); } }
public object Decode(ITranscoder transcoder, byte[] bytes,bool isNativeFiile) { lock(this) { buffer.Clear(); if (isNativeFiile) { return null; } buffer.WriteBytes(bytes); if(buffer.ReadShort() != MAGIC_NUMBER) { throw new ApplicationException("bytes have not used TranscoderManager encode."); } byte compress = buffer.ReadByte(); int len = buffer.ReadInt(); if(compress == 1) { byte[] tempBytes = new byte[len]; buffer.ReadBytes(tempBytes, 0, len); buffer.Clear(); using (MemoryStream ms = new MemoryStream(tempBytes)) { ms.Flush(); using (GZipStream gipStream = new GZipStream(ms, CompressionMode.Decompress)) { using (MemoryStream outBuffer = new MemoryStream()) { byte[] block = new byte[1024]; while (true) { int bytesRead = gipStream.Read(block, 0, block.Length); if (bytesRead <= 0) { break; } else { outBuffer.Write(block, 0, bytesRead); } } buffer.WriteBytes(outBuffer.ToArray()); } } } } return transcoder.Decode(buffer); } }