Compare commits

..

2 Commits

Author SHA1 Message Date
Adrian 92fbe73a3a Add NonDisposingStreamWrapper
UncompressedDeflateStream can also be left open using this class.
2016-02-18 01:56:04 +01:00
Adrian 553407fb72 Public accessible encoding 2016-02-18 01:55:26 +01:00
5 changed files with 161 additions and 22 deletions

View File

@ -7,17 +7,16 @@ namespace AdrianKousz.Util
public class ExtendedBinaryReader : BinaryReader public class ExtendedBinaryReader : BinaryReader
{ {
public bool Strict; public bool Strict;
public Encoding Encoding { get; private set; }
private const string ErrorChooseMethod = "Choose string writing method"; private const string ErrorChooseMethod = "Choose string writing method";
private const string ErrorHasNull = "String contains trailing null"; private const string ErrorHasNull = "String contains trailing null";
private const string ErrorHasNoNull = "String does not contain trailing null"; private const string ErrorHasNoNull = "String does not contain trailing null";
private readonly char[] nullChar = new char[] { '\0' }; public ExtendedBinaryReader(Stream stream, Encoding enc, bool strict = true)
private readonly Encoding encoding; : base(stream, enc)
public ExtendedBinaryReader(Stream stream, Encoding enc, bool strict = true) : base(stream, enc)
{ {
encoding = enc; Encoding = enc;
Strict = strict; Strict = strict;
} }
@ -42,7 +41,7 @@ namespace AdrianKousz.Util
{ {
if (len == 0) return ""; if (len == 0) return "";
var array = ReadBytes(len); var array = ReadBytes(len);
var str = encoding.GetString(array); var str = Encoding.GetString(array);
var index = -1; var index = -1;
if (expectNull || Strict) index = str.IndexOf("\0"); if (expectNull || Strict) index = str.IndexOf("\0");

View File

@ -6,13 +6,14 @@ namespace AdrianKousz.Util
{ {
public class ExtendedBinaryWriter : BinaryWriter public class ExtendedBinaryWriter : BinaryWriter
{ {
public Encoding Encoding { get; private set; }
private const string ErrorChooseMethod = "Choose string writing method"; private const string ErrorChooseMethod = "Choose string writing method";
private readonly Encoding encoding; public ExtendedBinaryWriter(Stream stream, Encoding enc)
: base(stream, enc)
public ExtendedBinaryWriter(Stream stream, Encoding enc) : base(stream, enc)
{ {
this.encoding = enc; Encoding = enc;
} }
public override void Write(string value) public override void Write(string value)
@ -32,13 +33,13 @@ namespace AdrianKousz.Util
public void WriteStringRaw(string value) public void WriteStringRaw(string value)
{ {
var array = encoding.GetBytes(value); var array = Encoding.GetBytes(value);
Write(array); Write(array);
} }
public void WriteStringInt16(string value) public void WriteStringInt16(string value)
{ {
var array = encoding.GetBytes(value); var array = Encoding.GetBytes(value);
var len = (Int16)array.Length; var len = (Int16)array.Length;
Write(len); Write(len);
Write(array); Write(array);
@ -46,7 +47,7 @@ namespace AdrianKousz.Util
public void WriteStringInt32(string value) public void WriteStringInt32(string value)
{ {
var array = encoding.GetBytes(value); var array = Encoding.GetBytes(value);
var len = (Int32)array.Length; var len = (Int32)array.Length;
Write(len); Write(len);
Write(array); Write(array);
@ -55,18 +56,18 @@ namespace AdrianKousz.Util
public void WriteStringPadded(string value, int len) public void WriteStringPadded(string value, int len)
{ {
var array = new byte[len]; var array = new byte[len];
encoding.GetBytes(value, 0, value.Length, array, 0); Encoding.GetBytes(value, 0, value.Length, array, 0);
Write(array); Write(array);
} }
public int GetByteCount(string value) public int GetByteCount(string value)
{ {
return encoding.GetByteCount(value); return Encoding.GetByteCount(value);
} }
public int GetZByteCount(string value) public int GetZByteCount(string value)
{ {
return encoding.GetByteCount(value + "\0"); return Encoding.GetByteCount(value + "\0");
} }
} }
} }

View File

@ -0,0 +1,142 @@
using System;
using System.IO;
/**
* About closing and disposing, also see:
* http://joeduffyblog.com/2004/12/12/follow-up-should-you-invoke-close-andor-dispose-on-a-stream/
* https://github.com/dotnet/corefx/blob/master/src/System.IO.Compression/src/System/IO/Compression/DeflateStream.cs
*/
namespace AdrianKousz.Util
{
public sealed class NonDisposingStreamWrapper : Stream
{
private bool closed = false;
public Stream BaseStream { get; private set; }
public NonDisposingStreamWrapper(Stream stream)
{
BaseStream = stream;
}
protected override void Dispose(bool disposing)
{
BaseStream = null;
}
private void EnsureNotDisposed()
{
if (BaseStream == null) {
throw new ObjectDisposedException(null);
}
}
public override bool CanRead
{
get { return BaseStream != null && BaseStream.CanRead; }
}
public override bool CanSeek
{
get { return BaseStream != null && BaseStream.CanSeek; }
}
public override bool CanWrite
{
get { return BaseStream != null && BaseStream.CanWrite; }
}
public override long Length
{
get
{
EnsureNotDisposed();
return BaseStream.Length;
}
}
public override long Position
{
get
{
EnsureNotDisposed();
return BaseStream.Position;
}
set
{
EnsureNotDisposed();
BaseStream.Position = value;
}
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count,
AsyncCallback callback, object state)
{
EnsureNotDisposed();
return BaseStream.BeginRead(buffer, offset, count, callback, state);
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count,
AsyncCallback callback, object state)
{
EnsureNotDisposed();
return BaseStream.BeginWrite(buffer, offset, count, callback, state);
}
public override int EndRead(IAsyncResult asyncResult)
{
EnsureNotDisposed();
return BaseStream.EndRead(asyncResult);
}
public override void EndWrite(IAsyncResult asyncResult)
{
EnsureNotDisposed();
BaseStream.EndWrite(asyncResult);
}
public override void Flush()
{
EnsureNotDisposed();
BaseStream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
EnsureNotDisposed();
return BaseStream.Read(buffer, offset, count);
}
public override int ReadByte()
{
EnsureNotDisposed();
return BaseStream.ReadByte();
}
public override long Seek(long offset, SeekOrigin origin)
{
EnsureNotDisposed();
return BaseStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
EnsureNotDisposed();
BaseStream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
EnsureNotDisposed();
BaseStream.Write(buffer, offset, count);
}
public override void WriteByte(byte value)
{
EnsureNotDisposed();
BaseStream.WriteByte(value);
}
}
}

View File

@ -8,12 +8,10 @@ namespace AdrianKousz.Util
private static readonly byte[] EOS = new byte[] {0x01, 0x00, 0x00, 0xff, 0xff}; private static readonly byte[] EOS = new byte[] {0x01, 0x00, 0x00, 0xff, 0xff};
public Stream BaseStream { get; private set; } public Stream BaseStream { get; private set; }
private bool _leaveOpen;
public UncompressedDeflateStream (Stream stream, bool leaveOpen = false) : base() public UncompressedDeflateStream (Stream stream) : base()
{ {
BaseStream = stream; BaseStream = stream;
_leaveOpen = leaveOpen;
} }
public override void Write(byte[] buffer, int offset, int count) public override void Write(byte[] buffer, int offset, int count)
@ -34,15 +32,13 @@ namespace AdrianKousz.Util
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
// Also see:
// https://github.com/dotnet/corefx/blob/master/src/System.IO.Compression/src/System/IO/Compression/DeflateStream.cs
try { try {
if (disposing && BaseStream != null) { if (disposing && BaseStream != null) {
BaseStream.Write(EOS, 0, EOS.Length); BaseStream.Write(EOS, 0, EOS.Length);
} }
} finally { } finally {
try { try {
if (!_leaveOpen && disposing && BaseStream != null) { if (disposing && BaseStream != null) {
BaseStream.Dispose(); BaseStream.Dispose();
} }
} finally { } finally {

View File

@ -38,5 +38,6 @@
<Compile Include="AdrianKousz.Util\ICommand.cs" /> <Compile Include="AdrianKousz.Util\ICommand.cs" />
<Compile Include="AdrianKousz.Util\IApp.cs" /> <Compile Include="AdrianKousz.Util\IApp.cs" />
<Compile Include="AdrianKousz.Util\CmdApp.cs" /> <Compile Include="AdrianKousz.Util\CmdApp.cs" />
<Compile Include="AdrianKousz.Util\NonDisposingStreamWrapper.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>