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

View File

@ -6,14 +6,13 @@ namespace AdrianKousz.Util
{
public class ExtendedBinaryWriter : BinaryWriter
{
public Encoding Encoding { get; private set; }
private const string ErrorChooseMethod = "Choose string writing method";
public ExtendedBinaryWriter(Stream stream, Encoding enc)
: base(stream, enc)
private readonly Encoding encoding;
public ExtendedBinaryWriter(Stream stream, Encoding enc) : base(stream, enc)
{
Encoding = enc;
this.encoding = enc;
}
public override void Write(string value)
@ -33,13 +32,13 @@ namespace AdrianKousz.Util
public void WriteStringRaw(string value)
{
var array = Encoding.GetBytes(value);
var array = encoding.GetBytes(value);
Write(array);
}
public void WriteStringInt16(string value)
{
var array = Encoding.GetBytes(value);
var array = encoding.GetBytes(value);
var len = (Int16)array.Length;
Write(len);
Write(array);
@ -47,7 +46,7 @@ namespace AdrianKousz.Util
public void WriteStringInt32(string value)
{
var array = Encoding.GetBytes(value);
var array = encoding.GetBytes(value);
var len = (Int32)array.Length;
Write(len);
Write(array);
@ -56,18 +55,18 @@ namespace AdrianKousz.Util
public void WriteStringPadded(string value, int len)
{
var array = new byte[len];
Encoding.GetBytes(value, 0, value.Length, array, 0);
encoding.GetBytes(value, 0, value.Length, array, 0);
Write(array);
}
public int GetByteCount(string value)
{
return Encoding.GetByteCount(value);
return encoding.GetByteCount(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};
public Stream BaseStream { get; private set; }
private bool _leaveOpen;
public UncompressedDeflateStream (Stream stream) : base()
public UncompressedDeflateStream (Stream stream, bool leaveOpen = false) : base()
{
BaseStream = stream;
_leaveOpen = leaveOpen;
}
public override void Write(byte[] buffer, int offset, int count)
@ -32,13 +34,15 @@ namespace AdrianKousz.Util
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 {
if (disposing && BaseStream != null) {
BaseStream.Write(EOS, 0, EOS.Length);
}
} finally {
try {
if (disposing && BaseStream != null) {
if (!_leaveOpen && disposing && BaseStream != null) {
BaseStream.Dispose();
}
} finally {

View File

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