Compare commits

..

No commits in common. "92fbe73a3a16d8b399b828811273d04d224fa923" and "0da6cc4242af4107807899f43b3bab480c7ea0c3" have entirely different histories.

5 changed files with 22 additions and 161 deletions

View File

@ -7,16 +7,17 @@ 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";
public ExtendedBinaryReader(Stream stream, Encoding enc, bool strict = true) private readonly char[] nullChar = new char[] { '\0' };
: base(stream, enc) private readonly Encoding encoding;
public ExtendedBinaryReader(Stream stream, Encoding enc, bool strict = true) : base(stream, enc)
{ {
Encoding = enc; encoding = enc;
Strict = strict; Strict = strict;
} }
@ -41,7 +42,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,14 +6,13 @@ 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";
public ExtendedBinaryWriter(Stream stream, Encoding enc) private readonly Encoding encoding;
: base(stream, enc)
public ExtendedBinaryWriter(Stream stream, Encoding enc) : base(stream, enc)
{ {
Encoding = enc; this.encoding = enc;
} }
public override void Write(string value) public override void Write(string value)
@ -33,13 +32,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);
@ -47,7 +46,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);
@ -56,18 +55,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

@ -1,142 +0,0 @@
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,10 +8,12 @@ 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) : base() public UncompressedDeflateStream (Stream stream, bool leaveOpen = false) : 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)
@ -32,13 +34,15 @@ 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 (disposing && BaseStream != null) { if (!_leaveOpen && disposing && BaseStream != null) {
BaseStream.Dispose(); BaseStream.Dispose();
} }
} finally { } finally {

View File

@ -38,6 +38,5 @@
<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>