mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-24 05:10:42 -04:00
Style: move stuff out of FixedBufferStream and reuse it for Ionic.Zlib
This commit is contained in:
parent
231838bd3e
commit
7b8c9b4e56
@ -301,6 +301,7 @@
|
|||||||
<Compile Include="Utils\Camera.cs" />
|
<Compile Include="Utils\Camera.cs" />
|
||||||
<Compile Include="Utils\ErrorHandler.cs" />
|
<Compile Include="Utils\ErrorHandler.cs" />
|
||||||
<Compile Include="Utils\Options.cs" />
|
<Compile Include="Utils\Options.cs" />
|
||||||
|
<Compile Include="Utils\ReadOnlyStream.cs" />
|
||||||
<Compile Include="Utils\Respawn.cs" />
|
<Compile Include="Utils\Respawn.cs" />
|
||||||
<Compile Include="Utils\TextureRectangle.cs" />
|
<Compile Include="Utils\TextureRectangle.cs" />
|
||||||
<Compile Include="Utils\StringBuffer.cs" />
|
<Compile Include="Utils\StringBuffer.cs" />
|
||||||
|
@ -4,10 +4,11 @@
|
|||||||
#if __MonoCS__
|
#if __MonoCS__
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using ClassicalSharp;
|
||||||
|
|
||||||
namespace Ionic.Zlib {
|
namespace Ionic.Zlib {
|
||||||
|
|
||||||
internal class DeflateStream {
|
internal class DeflateStream : ReadOnlyStream {
|
||||||
|
|
||||||
ZlibCodec z;
|
ZlibCodec z;
|
||||||
bool _leaveOpen;
|
bool _leaveOpen;
|
||||||
@ -21,7 +22,7 @@ namespace Ionic.Zlib {
|
|||||||
z = new ZlibCodec();
|
z = new ZlibCodec();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public override void Close() {
|
||||||
z.EndInflate();
|
z.EndInflate();
|
||||||
z = null;
|
z = null;
|
||||||
|
|
||||||
@ -30,7 +31,9 @@ namespace Ionic.Zlib {
|
|||||||
_stream = null;
|
_stream = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Read( byte[] buffer, int offset, int count ) {
|
public override void Flush() { }
|
||||||
|
|
||||||
|
public override int Read( byte[] buffer, int offset, int count ) {
|
||||||
// According to MS documentation, any implementation of the IO.Stream.Read function must:
|
// 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,
|
// (a) throw an exception if offset & count reference an invalid part of the buffer,
|
||||||
// or if count < 0, or if buffer is null
|
// or if count < 0, or if buffer is null
|
||||||
|
@ -98,9 +98,9 @@ namespace ClassicalSharp.Network.Protocols {
|
|||||||
// due to their async packet sending behaviour.
|
// due to their async packet sending behaviour.
|
||||||
if( gzipStream == null ) HandleLevelInit();
|
if( gzipStream == null ) HandleLevelInit();
|
||||||
int usedLength = reader.ReadInt16();
|
int usedLength = reader.ReadInt16();
|
||||||
gzippedMap.Position = 0;
|
gzippedMap.pos = 0;
|
||||||
gzippedMap.Offset = reader.index;
|
gzippedMap.bufferPos = reader.index;
|
||||||
gzippedMap.SetLength( usedLength );
|
gzippedMap.len = usedLength;
|
||||||
|
|
||||||
if( gzipHeader.done || gzipHeader.ReadHeader( gzippedMap ) ) {
|
if( gzipHeader.done || gzipHeader.ReadHeader( gzippedMap ) ) {
|
||||||
if( mapSizeIndex < 4 ) {
|
if( mapSizeIndex < 4 ) {
|
||||||
|
@ -6,24 +6,10 @@ namespace ClassicalSharp.Network {
|
|||||||
|
|
||||||
/// <summary> Similar to a memory stream except that its underlying array
|
/// <summary> Similar to a memory stream except that its underlying array
|
||||||
/// cannot be resized and this class performs minimal validation checks. </summary>
|
/// cannot be resized and this class performs minimal validation checks. </summary>
|
||||||
internal class FixedBufferStream : Stream {
|
internal class FixedBufferStream : ReadOnlyStream {
|
||||||
|
|
||||||
public byte[] _buffer;
|
public byte[] _buffer;
|
||||||
int _position, _length;
|
public int pos, len, bufferPos;
|
||||||
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 FixedBufferStream( byte[] buffer ) {
|
public FixedBufferStream( byte[] buffer ) {
|
||||||
_buffer = buffer;
|
_buffer = buffer;
|
||||||
@ -32,36 +18,20 @@ namespace ClassicalSharp.Network {
|
|||||||
public override void Flush() { }
|
public override void Flush() { }
|
||||||
|
|
||||||
public override int Read( byte[] buffer, int offset, int count ) {
|
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 > count ) numBytes = count;
|
||||||
if( numBytes <= 0 ) return 0;
|
if( numBytes <= 0 ) return 0;
|
||||||
|
|
||||||
Buffer.BlockCopy( _buffer, Offset + _position, buffer, offset, numBytes );
|
Buffer.BlockCopy( _buffer, bufferPos + pos, buffer, offset, numBytes );
|
||||||
_position += numBytes;
|
pos += numBytes;
|
||||||
return numBytes;
|
return numBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int ReadByte() {
|
public override int ReadByte() {
|
||||||
if( _position >= _length ) return -1;
|
if( pos >= len ) return -1;
|
||||||
byte value = _buffer[Offset + _position];
|
byte value = _buffer[bufferPos + pos];
|
||||||
_position++;
|
pos++;
|
||||||
return value;
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
28
ClassicalSharp/Utils/ReadOnlyStream.cs
Normal file
28
ClassicalSharp/Utils/ReadOnlyStream.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user