Compare commits
2 Commits
0da6cc4242
...
92fbe73a3a
Author | SHA1 | Date |
---|---|---|
Adrian | 92fbe73a3a | |
Adrian | 553407fb72 |
|
@ -7,17 +7,16 @@ 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";
|
||||
|
||||
private readonly char[] nullChar = new char[] { '\0' };
|
||||
private readonly Encoding encoding;
|
||||
|
||||
public ExtendedBinaryReader(Stream stream, Encoding enc, bool strict = true) : base(stream, enc)
|
||||
public ExtendedBinaryReader(Stream stream, Encoding enc, bool strict = true)
|
||||
: base(stream, enc)
|
||||
{
|
||||
encoding = enc;
|
||||
Encoding = enc;
|
||||
Strict = strict;
|
||||
}
|
||||
|
||||
|
@ -42,7 +41,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");
|
||||
|
|
|
@ -6,13 +6,14 @@ namespace AdrianKousz.Util
|
|||
{
|
||||
public class ExtendedBinaryWriter : BinaryWriter
|
||||
{
|
||||
public Encoding Encoding { get; private set; }
|
||||
|
||||
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)
|
||||
|
@ -32,13 +33,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);
|
||||
|
@ -46,7 +47,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);
|
||||
|
@ -55,18 +56,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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,12 +8,10 @@ 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, bool leaveOpen = false) : base()
|
||||
public UncompressedDeflateStream (Stream stream) : base()
|
||||
{
|
||||
BaseStream = stream;
|
||||
_leaveOpen = leaveOpen;
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
|
@ -34,15 +32,13 @@ 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 (!_leaveOpen && disposing && BaseStream != null) {
|
||||
if (disposing && BaseStream != null) {
|
||||
BaseStream.Dispose();
|
||||
}
|
||||
} finally {
|
||||
|
|
|
@ -38,5 +38,6 @@
|
|||
<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>
|
Loading…
Reference in New Issue