public override int Read(byte[] buffer, int offset, int count) { if (_mode != ZLibMode.Decompress) { throw new NotSupportedException("Read not supported on compression"); } ValidateReadWriteArgs(buffer, offset, count); int readLen = 0; if (_internalBufPos != -1) { using (PinnedArray pinRead = new PinnedArray(_internalBuf)) // [In] Compressed using (PinnedArray pinWrite = new PinnedArray(buffer)) // [Out] Will-be-decompressed { _zstream.NextIn = pinRead[_internalBufPos]; _zstream.NextOut = pinWrite[offset]; _zstream.AvailOut = (uint)count; while (0 < _zstream.AvailOut) { if (_zstream.AvailIn == 0) { // Compressed Data is no longer available in array, so read more from _stream int baseReadSize = BaseStream.Read(_internalBuf, 0, _internalBuf.Length); _internalBufPos = 0; _zstream.NextIn = pinRead; _zstream.AvailIn = (uint)baseReadSize; TotalIn += baseReadSize; } uint inCount = _zstream.AvailIn; uint outCount = _zstream.AvailOut; // flush method for inflate has no effect ZLibReturnCode ret = ZLibNative.Inflate(_zstream, ZLibFlush.NO_FLUSH); _internalBufPos += (int)(inCount - _zstream.AvailIn); readLen += (int)(outCount - _zstream.AvailOut); if (ret == ZLibReturnCode.STREAM_END) { _internalBufPos = -1; // magic for StreamEnd break; } if (ret != ZLibReturnCode.OK) { throw new Exception(ret + " " + _zstream.LastErrorMsg); } } TotalOut += readLen; } } return(readLen); }
private static string GetMsg(ZLibReturnCode errorCode, string lastStreamError) { var msg = $"ZLib error {errorCode}"; if (!String.IsNullOrEmpty(lastStreamError)) { msg += $" ({lastStreamError})"; } return(msg); }
internal static void CheckZLibError(ZLibReturnCode ret, ZStream zstream = null) { if (ret < 0) { if (zstream == null) { throw new ZLibException(ret); } else { throw new ZLibException(ret, zstream.LastErrorMsg); } } }
public override void Flush() { if (_mode == ZLibMode.Decompress) { BaseStream.Flush(); return; } using (PinnedArray pinWrite = new PinnedArray(_internalBuf)) { _zstream.NextIn = IntPtr.Zero; _zstream.AvailIn = 0; _zstream.NextOut = pinWrite[_internalBufPos]; _zstream.AvailOut = (uint)(_internalBuf.Length - _internalBufPos); ZLibReturnCode ret = ZLibReturnCode.OK; while (ret != ZLibReturnCode.STREAM_END) { if (_zstream.AvailOut != 0) { uint outCount = _zstream.AvailOut; ret = ZLibNative.Deflate(_zstream, ZLibFlush.FINISH); _internalBufPos += (int)(outCount - _zstream.AvailOut); if (ret != ZLibReturnCode.STREAM_END && ret != ZLibReturnCode.OK) { throw new Exception(ret + " " + _zstream.LastErrorMsg); } } BaseStream.Write(_internalBuf, 0, _internalBufPos); TotalOut += _internalBufPos; _internalBufPos = 0; _zstream.NextOut = pinWrite; _zstream.AvailOut = (uint)_internalBuf.Length; } } BaseStream.Flush(); }
public override void Write(byte[] buffer, int offset, int count) { if (_mode != ZLibMode.Compress) { throw new NotSupportedException("Write not supported on decompression"); } TotalIn += count; using (PinnedArray pinRead = new PinnedArray(buffer)) using (PinnedArray pinWrite = new PinnedArray(_internalBuf)) { _zstream.NextIn = pinRead[offset]; _zstream.AvailIn = (uint)count; _zstream.NextOut = pinWrite[_internalBufPos]; _zstream.AvailOut = (uint)(_internalBuf.Length - _internalBufPos); while (_zstream.AvailIn != 0) { uint outCount = _zstream.AvailOut; ZLibReturnCode ret = ZLibNative.Deflate(_zstream, ZLibFlush.NO_FLUSH); _internalBufPos += (int)(outCount - _zstream.AvailOut); if (_zstream.AvailOut == 0) { BaseStream.Write(_internalBuf, 0, _internalBuf.Length); TotalOut += _internalBuf.Length; _internalBufPos = 0; _zstream.NextOut = pinWrite; _zstream.AvailOut = (uint)_internalBuf.Length; } if (ret != ZLibReturnCode.OK) { throw new Exception(ret + " " + _zstream.LastErrorMsg); } } } }
private static string ForgeErrorMessage(ZLibReturnCode errorCode, string msg = null) { return(msg == null ? $"[{errorCode}]" : $"[{errorCode}] {msg}"); }
public ZLibException(ZLibReturnCode errorCode, string msg) : base(ForgeErrorMessage(errorCode, msg)) { ErrorCode = errorCode; }
public ZLibException(ZLibReturnCode errorCode) : base(ForgeErrorMessage(errorCode)) { ErrorCode = errorCode; }
public ZLibException(ZLibReturnCode errorCode, string lastStreamError) : base(GetMsg(errorCode, lastStreamError)) { ReturnCode = errorCode; }