Initial commit
This commit is contained in:
commit
0f86b0434b
38 changed files with 2370 additions and 0 deletions
167
LibDgf/GMWare.IO/StreamUtils.cs
Normal file
167
LibDgf/GMWare.IO/StreamUtils.cs
Normal file
|
@ -0,0 +1,167 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
|
||||
namespace GMWare.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides some commonly used methods for Stream manipulation.
|
||||
/// </summary>
|
||||
public static class StreamUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Opens a DeflateStream for reading Zlib compressed data from the current position of the <paramref name="stream"/> parameter. Closing this Stream will not close the underlying Stream.
|
||||
/// </summary>
|
||||
/// <param name="stream">The Stream to create a DeflateStream from.</param>
|
||||
/// <returns>The opened DeflateStream.</returns>
|
||||
public static DeflateStream OpenDeflateDecompressionStreamCheap(Stream stream)
|
||||
{
|
||||
return OpenDeflateDecompressionStreamCheap(stream, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a DeflateStream for reading Zlib compressed data from the current position of the <paramref name="stream"/> parameter.
|
||||
/// </summary>
|
||||
/// <param name="stream">The Stream to create a DeflateStream from.</param>
|
||||
/// <param name="leaveOpen">Specifies whether or not the underlying Stream will be left open when this Stream is closed.</param>
|
||||
/// <returns>The opened DeflateStream.</returns>
|
||||
public static DeflateStream OpenDeflateDecompressionStreamCheap(Stream stream, bool leaveOpen)
|
||||
{
|
||||
if (stream == null) throw new ArgumentNullException("stream");
|
||||
stream.ReadByte();
|
||||
stream.ReadByte();
|
||||
return new DeflateStream(stream, CompressionMode.Decompress, leaveOpen);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a number of bytes from one Stream to the other. The current position of each is used.
|
||||
/// </summary>
|
||||
/// <param name="src">The Stream to copy from.</param>
|
||||
/// <param name="dest">The Stream to copy to.</param>
|
||||
/// <param name="length">The number of bytes to copy.</param>
|
||||
/// <returns>Whether or not all requested bytes are copied.</returns>
|
||||
[Obsolete("For backward compatibility only. Please use StreamCopy().")]
|
||||
public static bool StreamCopyWithLength(Stream src, Stream dest, int length)
|
||||
{
|
||||
return StreamCopy(src, dest, length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a number of bytes from one Stream to the other. The current position of each is used.
|
||||
/// </summary>
|
||||
/// <param name="src">The Stream to copy from.</param>
|
||||
/// <param name="dest">The Stream to copy to.</param>
|
||||
/// <param name="length">The number of bytes to copy.</param>
|
||||
/// <returns>Whether or not all requested bytes are copied.</returns>
|
||||
public static bool StreamCopy(Stream src, Stream dest, long length)
|
||||
{
|
||||
return StreamCopy(src, dest, length, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a number of bytes from one Stream to the other. The current position of each is used.
|
||||
/// </summary>
|
||||
/// <param name="src">The Stream to copy from.</param>
|
||||
/// <param name="dest">The Stream to copy to.</param>
|
||||
/// <param name="length">The number of bytes to copy.</param>
|
||||
/// <param name="procDelegate">A delegate to process the read buffer before it's written to the destination.</param>
|
||||
/// <returns>Whether or not all requested bytes are copied.</returns>
|
||||
public static bool StreamCopy(Stream src, Stream dest, long length, StreamCopyProcessor procDelegate)
|
||||
{
|
||||
if (src == null) throw new ArgumentNullException("src");
|
||||
if (dest == null) throw new ArgumentNullException("dest");
|
||||
|
||||
if (length == 0) return true;
|
||||
|
||||
const int BUFFER_SIZE = 4096;
|
||||
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int read;
|
||||
long left = length;
|
||||
bool continueProcessing = true;
|
||||
|
||||
while (continueProcessing && left / buffer.Length != 0 && (read = src.Read(buffer, 0, buffer.Length)) > 0)
|
||||
{
|
||||
if (procDelegate != null)
|
||||
{
|
||||
continueProcessing = procDelegate(buffer, read);
|
||||
}
|
||||
dest.Write(buffer, 0, read);
|
||||
left -= read;
|
||||
}
|
||||
|
||||
// Should stop if zero bytes have been read from stream although some should have been read
|
||||
if (length > BUFFER_SIZE && left == length) return false;
|
||||
|
||||
if (src.CanSeek && src.Position == src.Length && left != 0) throw new EndOfStreamException();
|
||||
|
||||
while (continueProcessing && left > 0 && (read = src.Read(buffer, 0, (int)left)) > 0)
|
||||
{
|
||||
if (procDelegate != null)
|
||||
{
|
||||
continueProcessing = procDelegate(buffer, read);
|
||||
}
|
||||
dest.Write(buffer, 0, read);
|
||||
left -= read;
|
||||
}
|
||||
|
||||
return left == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies one Stream to the other. The current position of each is used.
|
||||
/// </summary>
|
||||
/// <param name="src">The Stream to copy from.</param>
|
||||
/// <param name="dest">The Stream to copy to.</param>
|
||||
/// <returns>The number of bytes copied.</returns>
|
||||
public static long StreamCopy(Stream src, Stream dest)
|
||||
{
|
||||
return StreamCopy(src, dest, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies one Stream to the other. The current position of each is used.
|
||||
/// </summary>
|
||||
/// <param name="src">The Stream to copy from.</param>
|
||||
/// <param name="dest">The Stream to copy to.</param>
|
||||
/// <param name="procDelegate">A delegate for processing a read chunk before it is written.</param>
|
||||
/// <returns>The number of bytes copied.</returns>
|
||||
public static long StreamCopy(Stream src, Stream dest, StreamCopyProcessor procDelegate)
|
||||
{
|
||||
if (src == null) throw new ArgumentNullException("src");
|
||||
if (dest == null) throw new ArgumentNullException("dest");
|
||||
|
||||
// From Stack Overflow, probably
|
||||
const int BUFFER_SIZE = 4096;
|
||||
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
long bytesCopied = 0;
|
||||
int bytesRead;
|
||||
bool continueProcessing = true;
|
||||
|
||||
do
|
||||
{
|
||||
bytesRead = src.Read(buffer, 0, BUFFER_SIZE);
|
||||
if (procDelegate != null)
|
||||
{
|
||||
continueProcessing = procDelegate(buffer, bytesRead);
|
||||
}
|
||||
dest.Write(buffer, 0, bytesRead);
|
||||
bytesCopied += bytesRead;
|
||||
}
|
||||
while (continueProcessing && bytesRead != 0);
|
||||
return bytesCopied;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encapsulates a method for processing bytes that are being copied.
|
||||
/// </summary>
|
||||
/// <param name="buffer">The bytes that have been read from the source stream and will be written to the destination stream</param>
|
||||
/// <param name="bytesRead">The number of bytes that have been read from the source stream stored in <paramref name="buffer"/></param>
|
||||
/// <returns></returns>
|
||||
public delegate bool StreamCopyProcessor(byte[] buffer, int bytesRead);
|
||||
}
|
||||
}
|
62
LibDgf/GMWare.IO/StringReadingHelper.cs
Normal file
62
LibDgf/GMWare.IO/StringReadingHelper.cs
Normal file
|
@ -0,0 +1,62 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace GMWare.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// Collection of methods for reading strings using BinaryReader
|
||||
/// </summary>
|
||||
public static class StringReadingHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Reads a string of a given length.
|
||||
/// </summary>
|
||||
/// <param name="reader">BinaryReader to read from</param>
|
||||
/// <param name="length">Length of string to read</param>
|
||||
/// <returns>The string that was read.</returns>
|
||||
public static string ReadLengthedString(BinaryReader reader, int length)
|
||||
{
|
||||
return new string(reader.ReadChars(length));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a null terminated string.
|
||||
/// </summary>
|
||||
/// <param name="reader">BinaryReader to read from</param>
|
||||
/// <returns>The string that was read.</returns>
|
||||
public static string ReadNullTerminatedString(BinaryReader reader)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (char ch = reader.ReadChar(); ch != '\0'; ch = reader.ReadChar())
|
||||
{
|
||||
sb.Append(ch);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a null terminated string from within a fixed size block.
|
||||
/// </summary>
|
||||
/// <param name="reader">BinaryReader to read from</param>
|
||||
/// <param name="blockLen">The length of the fixed size block</param>
|
||||
/// <param name="encoding">The encoding the string is in</param>
|
||||
/// <returns>The string that was read.</returns>
|
||||
public static string ReadNullTerminatedStringFromFixedSizeBlock(BinaryReader reader, int blockLen, Encoding encoding)
|
||||
{
|
||||
byte[] data = reader.ReadBytes(blockLen);
|
||||
string str = encoding.GetString(data);
|
||||
int indNull = str.IndexOf('\0');
|
||||
if (indNull >= 0)
|
||||
{
|
||||
return str.Substring(0, indNull);
|
||||
}
|
||||
else
|
||||
{
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue