Style: move stuff out of FixedBufferStream and reuse it for Ionic.Zlib

This commit is contained in:
UnknownShadow200 2016-11-17 22:58:07 +11:00
parent 231838bd3e
commit 7b8c9b4e56
5 changed files with 46 additions and 44 deletions

View File

@ -301,6 +301,7 @@
<Compile Include="Utils\Camera.cs" />
<Compile Include="Utils\ErrorHandler.cs" />
<Compile Include="Utils\Options.cs" />
<Compile Include="Utils\ReadOnlyStream.cs" />
<Compile Include="Utils\Respawn.cs" />
<Compile Include="Utils\TextureRectangle.cs" />
<Compile Include="Utils\StringBuffer.cs" />

View File

@ -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

View File

@ -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 ) {

View File

@ -6,24 +6,10 @@ namespace ClassicalSharp.Network {
/// <summary> Similar to a memory stream except that its underlying array
/// cannot be resized and this class performs minimal validation checks. </summary>
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();
}
}
}

View File

@ -0,0 +1,28 @@
// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using System.IO;
namespace ClassicalSharp {
/// <summary> Implements a non-seekable stream that can only be read from. </summary>
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; }
}
}