#region Constructors /// <summary> /// Creates a new file as a writeable GZipStream /// </summary> /// <param name="fileName">The name of the compressed file to create</param> /// <param name="level">The compression level to use when adding data</param> /// <exception cref="ZLibException">If an error occurred in the internal zlib function</exception> public GZipStream(string fileName, CompressLevel level)
{
_isWriting = true;
_gzFile = gzopen(fileName, String.Format("wb{0}", (int)level)); if (_gzFile == IntPtr.Zero) thrownew ZLibException(-1, "Could not open " + fileName);
}
/// <summary> /// Opens an existing file as a readable GZipStream /// </summary> /// <param name="fileName">The name of the file to open</param> /// <exception cref="ZLibException">If an error occurred in the internal zlib function</exception> public GZipStream(string fileName)
{
_isWriting = false;
_gzFile = gzopen(fileName, "rb"); if (_gzFile == IntPtr.Zero) thrownew ZLibException(-1, "Could not open " + fileName);
} #endregion
#region Access properties /// <summary> /// Returns true of this stream can be read from, false otherwise /// </summary> publicoverridebool CanRead
{ get
{ return !_isWriting;
}
}
// Does the actual closing of the file handle. privatevoid cleanUp(bool isDisposing)
{ if (!_isDisposed)
{
gzclose(_gzFile);
_isDisposed = true;
}
} #endregion
#region Basic reading and writing /// <summary> /// Attempts to read a number of bytes from the stream. /// </summary> /// <param name="buffer">The destination data buffer</param> /// <param name="offset">The index of the first destination byte in <c>buffer</c></param> /// <param name="count">The number of bytes requested</param> /// <returns>The number of bytes read</returns> /// <exception cref="ArgumentNullException">If <c>buffer</c> is null</exception> /// <exception cref="ArgumentOutOfRangeException">If <c>count</c> or <c>offset</c> are negative</exception> /// <exception cref="ArgumentException">If <c>offset</c> + <c>count</c> is > buffer.Length</exception> /// <exception cref="NotSupportedException">If this stream is not readable.</exception> /// <exception cref="ObjectDisposedException">If this stream has been disposed.</exception> publicoverrideint Read(byte[] buffer, int offset, int count)
{ if (!CanRead) thrownew NotSupportedException(); if (buffer == null) thrownew ArgumentNullException(); if (offset < 0 || count < 0) thrownew ArgumentOutOfRangeException(); if ((offset+count) > buffer.Length) thrownew ArgumentException(); if (_isDisposed) thrownew ObjectDisposedException("GZipStream");
GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned); int result; try
{
result = gzread(_gzFile, h.AddrOfPinnedObject().ToInt32() + offset, count); if (result < 0) thrownew IOException();
} finally
{
h.Free();
} return result;
}
/// <summary> /// Attempts to read a single byte from the stream. /// </summary> /// <returns>The byte that was read, or -1 in case of error or End-Of-File</returns> publicoverrideint ReadByte()
{ if (!CanRead) thrownew NotSupportedException(); if (_isDisposed) thrownew ObjectDisposedException("GZipStream"); return gzgetc(_gzFile);
}
/// <summary> /// Writes a number of bytes to the stream /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="count"></param> /// <exception cref="ArgumentNullException">If <c>buffer</c> is null</exception> /// <exception cref="ArgumentOutOfRangeException">If <c>count</c> or <c>offset</c> are negative</exception> /// <exception cref="ArgumentException">If <c>offset</c> + <c>count</c> is > buffer.Length</exception> /// <exception cref="NotSupportedException">If this stream is not writeable.</exception> /// <exception cref="ObjectDisposedException">If this stream has been disposed.</exception> publicoverridevoid Write(byte[] buffer, int offset, int count)
{ if (!CanWrite) thrownew NotSupportedException(); if (buffer == null) thrownew ArgumentNullException(); if (offset < 0 || count < 0) thrownew ArgumentOutOfRangeException(); if ((offset+count) > buffer.Length) thrownew ArgumentException(); if (_isDisposed) thrownew ObjectDisposedException("GZipStream");
GCHandle h = GCHandle.Alloc(buffer, GCHandleType.Pinned); try
{ int result = gzwrite(_gzFile, h.AddrOfPinnedObject().ToInt32() + offset, count); if (result < 0) thrownew IOException();
} finally
{
h.Free();
}
}
/// <summary> /// Writes a single byte to the stream /// </summary> /// <param name="value">The byte to add to the stream.</param> /// <exception cref="NotSupportedException">If this stream is not writeable.</exception> /// <exception cref="ObjectDisposedException">If this stream has been disposed.</exception> publicoverridevoid WriteByte(byte value)
{ if (!CanWrite) thrownew NotSupportedException(); if (_isDisposed) thrownew ObjectDisposedException("GZipStream");
int result = gzputc(_gzFile, (int)value); if (result < 0) thrownew IOException();
} #endregion
/// <summary> /// Flushes the <c>GZipStream</c>. /// </summary> /// <remarks>In this implementation, this method does nothing. This is because excessive /// flushing may degrade the achievable compression rates.</remarks> publicoverridevoid Flush()
{ // left empty on purpose
}
/// <summary> /// Gets/sets the current position in the <c>GZipStream</c>. Not supported. /// </summary> /// <remarks>In this implementation this property is not supported</remarks> /// <exception cref="NotSupportedException">Always thrown</exception> publicoverridelong Position
{ get
{ thrownew NotSupportedException();
} set
{ thrownew NotSupportedException();
}
}
/// <summary> /// Gets the size of the stream. Not supported. /// </summary> /// <remarks>In this implementation this property is not supported</remarks> /// <exception cref="NotSupportedException">Always thrown</exception> publicoverridelong Length
{ get
{ thrownew NotSupportedException();
}
} #endregion
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.25 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.