From 7b8c9b4e56f014aa5acf690d1d2271c89629bf1e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 17 Nov 2016 22:58:07 +1100 Subject: [PATCH] Style: move stuff out of FixedBufferStream and reuse it for Ionic.Zlib --- ClassicalSharp/ClassicalSharp.csproj | 1 + ClassicalSharp/Ionic.Zlib/DeflateStream.cs | 9 ++-- ClassicalSharp/Network/Protocols/Classic.cs | 6 +-- .../Network/Utils/FixedBufferStream.cs | 46 ++++--------------- ClassicalSharp/Utils/ReadOnlyStream.cs | 28 +++++++++++ 5 files changed, 46 insertions(+), 44 deletions(-) create mode 100644 ClassicalSharp/Utils/ReadOnlyStream.cs diff --git a/ClassicalSharp/ClassicalSharp.csproj b/ClassicalSharp/ClassicalSharp.csproj index 0621023c3..83fbe0f97 100644 --- a/ClassicalSharp/ClassicalSharp.csproj +++ b/ClassicalSharp/ClassicalSharp.csproj @@ -301,6 +301,7 @@ + diff --git a/ClassicalSharp/Ionic.Zlib/DeflateStream.cs b/ClassicalSharp/Ionic.Zlib/DeflateStream.cs index 735af6bd5..44cc196dc 100644 --- a/ClassicalSharp/Ionic.Zlib/DeflateStream.cs +++ b/ClassicalSharp/Ionic.Zlib/DeflateStream.cs @@ -4,10 +4,11 @@ #if __MonoCS__ using System; using System.IO; +using ClassicalSharp; namespace Ionic.Zlib { - internal class DeflateStream { + internal class DeflateStream : ReadOnlyStream { ZlibCodec z; bool _leaveOpen; @@ -21,7 +22,7 @@ namespace Ionic.Zlib { z = new ZlibCodec(); } - public void Dispose() { + public override void Close() { z.EndInflate(); z = null; @@ -29,8 +30,10 @@ namespace Ionic.Zlib { _stream.Dispose(); _stream = null; } + + public override void Flush() { } - public int Read( byte[] buffer, int offset, int count ) { + public override int Read( byte[] buffer, int offset, int count ) { // According to MS documentation, any implementation of the IO.Stream.Read function must: // (a) throw an exception if offset & count reference an invalid part of the buffer, // or if count < 0, or if buffer is null diff --git a/ClassicalSharp/Network/Protocols/Classic.cs b/ClassicalSharp/Network/Protocols/Classic.cs index 680315be9..b7c14df4f 100644 --- a/ClassicalSharp/Network/Protocols/Classic.cs +++ b/ClassicalSharp/Network/Protocols/Classic.cs @@ -98,9 +98,9 @@ namespace ClassicalSharp.Network.Protocols { // due to their async packet sending behaviour. if( gzipStream == null ) HandleLevelInit(); int usedLength = reader.ReadInt16(); - gzippedMap.Position = 0; - gzippedMap.Offset = reader.index; - gzippedMap.SetLength( usedLength ); + gzippedMap.pos = 0; + gzippedMap.bufferPos = reader.index; + gzippedMap.len = usedLength; if( gzipHeader.done || gzipHeader.ReadHeader( gzippedMap ) ) { if( mapSizeIndex < 4 ) { diff --git a/ClassicalSharp/Network/Utils/FixedBufferStream.cs b/ClassicalSharp/Network/Utils/FixedBufferStream.cs index 33246f7f6..d8fc43d86 100644 --- a/ClassicalSharp/Network/Utils/FixedBufferStream.cs +++ b/ClassicalSharp/Network/Utils/FixedBufferStream.cs @@ -6,24 +6,10 @@ namespace ClassicalSharp.Network { /// Similar to a memory stream except that its underlying array /// cannot be resized and this class performs minimal validation checks. - internal class FixedBufferStream : Stream { + internal class FixedBufferStream : ReadOnlyStream { public byte[] _buffer; - int _position, _length; - public int Offset; - - public override bool CanRead { get { return true; } } - - public override bool CanSeek { get { return false; } } - - public override bool CanWrite { get { return false; } } - - public override long Length { get { return _length; } } - - public override long Position { - get { return _position; } - set { _position = (int)value; } - } + public int pos, len, bufferPos; public FixedBufferStream( byte[] buffer ) { _buffer = buffer; @@ -32,36 +18,20 @@ namespace ClassicalSharp.Network { public override void Flush() { } public override int Read( byte[] buffer, int offset, int count ) { - int numBytes = _length - _position; + int numBytes = len - pos; if( numBytes > count ) numBytes = count; if( numBytes <= 0 ) return 0; - Buffer.BlockCopy( _buffer, Offset + _position, buffer, offset, numBytes ); - _position += numBytes; + Buffer.BlockCopy( _buffer, bufferPos + pos, buffer, offset, numBytes ); + pos += numBytes; return numBytes; } public override int ReadByte() { - if( _position >= _length ) return -1; - byte value = _buffer[Offset + _position]; - _position++; + if( pos >= len ) return -1; + byte value = _buffer[bufferPos + pos]; + pos++; return value; } - - public override long Seek( long offset, SeekOrigin origin ) { - throw new NotSupportedException(); - } - - public override void SetLength( long value ) { - _length = (int)value; - } - - public override void Write( byte[] buffer, int offset, int count ) { - throw new NotSupportedException(); - } - - public override void WriteByte( byte value ) { - throw new NotSupportedException(); - } } } diff --git a/ClassicalSharp/Utils/ReadOnlyStream.cs b/ClassicalSharp/Utils/ReadOnlyStream.cs new file mode 100644 index 000000000..07e781512 --- /dev/null +++ b/ClassicalSharp/Utils/ReadOnlyStream.cs @@ -0,0 +1,28 @@ +// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT +using System; +using System.IO; + +namespace ClassicalSharp { + + /// Implements a non-seekable stream that can only be read from. + internal abstract class ReadOnlyStream : Stream { + + static NotSupportedException ex = new NotSupportedException("Writing/Seeking not supported"); + + public override bool CanRead { get { return true; } } + + public override bool CanSeek { get { return false; } } + + public override bool CanWrite { get { return false; } } + + public override long Length { get { throw ex; } } + + public override long Position { get { throw ex; } set { throw ex; } } + + public override long Seek( long offset, SeekOrigin origin ) { throw ex; } + + public override void SetLength( long value ) { throw ex; } + + public override void Write( byte[] buffer, int offset, int count ) { throw ex; } + } +}