public override System.IO.Stream Receive(TimeSpan timeout, Information options) { if (_disposed) throw new ObjectDisposedException(this.GetType().FullName); if (!_connect) throw new ConnectionException(); lock (_receiveLock) { Stream stream = null; RangeStream dataStream = null; try { stream = _connection.Receive(timeout, options); dataStream = new RangeStream(stream, 0, stream.Length - 4); byte[] verifyCrc = Crc32_Castagnoli.ComputeHash(dataStream); byte[] orignalCrc = new byte[4]; using (RangeStream crcStream = new RangeStream(stream, stream.Length - 4, 4, true)) { crcStream.Read(orignalCrc, 0, orignalCrc.Length); } if (!Unsafe.Equals(verifyCrc, orignalCrc)) throw new ArgumentException("Crc Error"); dataStream.Seek(0, SeekOrigin.Begin); return dataStream; } catch (ConnectionException e) { if (stream != null) stream.Dispose(); if (dataStream != null) dataStream.Dispose(); throw e; } catch (Exception e) { if (stream != null) stream.Dispose(); if (dataStream != null) dataStream.Dispose(); throw new ConnectionException(e.Message, e); } } }
private static Stream Compress(Stream stream) { if (stream == null) throw new ArgumentNullException("stream"); var targetStream = new RangeStream(stream, true); List<KeyValuePair<byte, Stream>> list = new List<KeyValuePair<byte, Stream>>(); try { targetStream.Seek(0, SeekOrigin.Begin); BufferStream deflateBufferStream = null; try { deflateBufferStream = new BufferStream(_bufferManager); using (DeflateStream deflateStream = new DeflateStream(deflateBufferStream, CompressionMode.Compress, true)) { byte[] compressBuffer = null; try { compressBuffer = _bufferManager.TakeBuffer(1024 * 4); int i = -1; while ((i = targetStream.Read(compressBuffer, 0, compressBuffer.Length)) > 0) { deflateStream.Write(compressBuffer, 0, i); } } finally { if (compressBuffer != null) { _bufferManager.ReturnBuffer(compressBuffer); } } } deflateBufferStream.Seek(0, SeekOrigin.Begin); list.Add(new KeyValuePair<byte, Stream>((byte)ConvertCompressionAlgorithm.Deflate, deflateBufferStream)); } catch (Exception) { if (deflateBufferStream != null) { deflateBufferStream.Dispose(); } } } catch (Exception) { } list.Add(new KeyValuePair<byte, Stream>((byte)ConvertCompressionAlgorithm.None, targetStream)); list.Sort((x, y) => { int c = x.Value.Length.CompareTo(y.Value.Length); if (c != 0) return c; return x.Key.CompareTo(y.Key); }); #if DEBUG if (list[0].Value.Length != targetStream.Length) { Debug.WriteLine("ContentConverter Compress {3} : {0}→{1} {2}", NetworkConverter.ToSizeString(targetStream.Length), NetworkConverter.ToSizeString(list[0].Value.Length), NetworkConverter.ToSizeString(list[0].Value.Length - targetStream.Length), (ConvertCompressionAlgorithm)list[0].Key); } #endif for (int i = 1; i < list.Count; i++) { list[i].Value.Dispose(); } BufferStream metadataStream = new BufferStream(_bufferManager); metadataStream.WriteByte((byte)list[0].Key); return new UniteStream(metadataStream, list[0].Value); }
private static Stream RemoveHash(Stream stream) { if (stream == null) throw new ArgumentNullException("stream"); byte type = (byte)stream.ReadByte(); if (type == (byte)ConvertHashAlgorithm.Sha256) { Stream dataStream = null; try { byte[] hash = new byte[32]; using (RangeStream hashStream = new RangeStream(stream, stream.Length - 32, 32, true)) { hashStream.Read(hash, 0, hash.Length); } dataStream = new RangeStream(stream, 1, stream.Length - (1 + 32)); if (!Unsafe.Equals(hash, Sha256.ComputeHash(dataStream))) throw new FormatException(); dataStream.Seek(0, SeekOrigin.Begin); return dataStream; } catch (Exception) { if (dataStream != null) { dataStream.Dispose(); } throw; } } throw new NotSupportedException(); }
private static Stream AddHash(Stream stream) { if (stream == null) throw new ArgumentNullException("stream"); try { var targetStream = new RangeStream(stream, true); BufferStream metadataStream = new BufferStream(_bufferManager); metadataStream.WriteByte((byte)ConvertHashAlgorithm.Sha256); targetStream.Seek(0, SeekOrigin.Begin); var hash = Sha256.ComputeHash(targetStream); BufferStream hashStream = new BufferStream(_bufferManager); hashStream.Write(hash, 0, hash.Length); return new UniteStream(metadataStream, targetStream, hashStream); } catch (Exception e) { throw new ArgumentException(e.Message, e); } }