mirror of
https://github.com/google/brotli.git
synced 2024-11-21 19:20:09 +00:00
removed carriage returns from BrotliInputStream.cs
This commit is contained in:
parent
f4153a09f8
commit
6e6c31f322
@ -1,192 +1,192 @@
|
|||||||
/* Copyright 2015 Google Inc. All Rights Reserved.
|
/* Copyright 2015 Google Inc. All Rights Reserved.
|
||||||
|
|
||||||
Distributed under MIT license.
|
Distributed under MIT license.
|
||||||
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
||||||
*/
|
*/
|
||||||
namespace Org.Brotli.Dec
|
namespace Org.Brotli.Dec
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// <see cref="System.IO.Stream"/>
|
/// <see cref="System.IO.Stream"/>
|
||||||
/// decorator that decompresses brotli data.
|
/// decorator that decompresses brotli data.
|
||||||
/// <p> Not thread-safe.
|
/// <p> Not thread-safe.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class BrotliInputStream : System.IO.Stream
|
public class BrotliInputStream : System.IO.Stream
|
||||||
{
|
{
|
||||||
public const int DefaultInternalBufferSize = 16384;
|
public const int DefaultInternalBufferSize = 16384;
|
||||||
|
|
||||||
/// <summary>Internal buffer used for efficient byte-by-byte reading.</summary>
|
/// <summary>Internal buffer used for efficient byte-by-byte reading.</summary>
|
||||||
private byte[] buffer;
|
private byte[] buffer;
|
||||||
|
|
||||||
/// <summary>Number of decoded but still unused bytes in internal buffer.</summary>
|
/// <summary>Number of decoded but still unused bytes in internal buffer.</summary>
|
||||||
private int remainingBufferBytes;
|
private int remainingBufferBytes;
|
||||||
|
|
||||||
/// <summary>Next unused byte offset.</summary>
|
/// <summary>Next unused byte offset.</summary>
|
||||||
private int bufferOffset;
|
private int bufferOffset;
|
||||||
|
|
||||||
/// <summary>Decoder state.</summary>
|
/// <summary>Decoder state.</summary>
|
||||||
private readonly Org.Brotli.Dec.State state = new Org.Brotli.Dec.State();
|
private readonly Org.Brotli.Dec.State state = new Org.Brotli.Dec.State();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a
|
/// Creates a
|
||||||
/// <see cref="System.IO.Stream"/>
|
/// <see cref="System.IO.Stream"/>
|
||||||
/// wrapper that decompresses brotli data.
|
/// wrapper that decompresses brotli data.
|
||||||
/// <p> For byte-by-byte reading (
|
/// <p> For byte-by-byte reading (
|
||||||
/// <see cref="ReadByte()"/>
|
/// <see cref="ReadByte()"/>
|
||||||
/// ) internal buffer with
|
/// ) internal buffer with
|
||||||
/// <see cref="DefaultInternalBufferSize"/>
|
/// <see cref="DefaultInternalBufferSize"/>
|
||||||
/// size is allocated and used.
|
/// size is allocated and used.
|
||||||
/// <p> Will block the thread until first kilobyte of data of source is available.
|
/// <p> Will block the thread until first kilobyte of data of source is available.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="source">underlying data source</param>
|
/// <param name="source">underlying data source</param>
|
||||||
/// <exception cref="System.IO.IOException">in case of corrupted data or source stream problems</exception>
|
/// <exception cref="System.IO.IOException">in case of corrupted data or source stream problems</exception>
|
||||||
public BrotliInputStream(System.IO.Stream source)
|
public BrotliInputStream(System.IO.Stream source)
|
||||||
: this(source, DefaultInternalBufferSize, null)
|
: this(source, DefaultInternalBufferSize, null)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a
|
/// Creates a
|
||||||
/// <see cref="System.IO.Stream"/>
|
/// <see cref="System.IO.Stream"/>
|
||||||
/// wrapper that decompresses brotli data.
|
/// wrapper that decompresses brotli data.
|
||||||
/// <p> For byte-by-byte reading (
|
/// <p> For byte-by-byte reading (
|
||||||
/// <see cref="ReadByte()"/>
|
/// <see cref="ReadByte()"/>
|
||||||
/// ) internal buffer of specified size is
|
/// ) internal buffer of specified size is
|
||||||
/// allocated and used.
|
/// allocated and used.
|
||||||
/// <p> Will block the thread until first kilobyte of data of source is available.
|
/// <p> Will block the thread until first kilobyte of data of source is available.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="source">compressed data source</param>
|
/// <param name="source">compressed data source</param>
|
||||||
/// <param name="byteReadBufferSize">
|
/// <param name="byteReadBufferSize">
|
||||||
/// size of internal buffer used in case of
|
/// size of internal buffer used in case of
|
||||||
/// byte-by-byte reading
|
/// byte-by-byte reading
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <exception cref="System.IO.IOException">in case of corrupted data or source stream problems</exception>
|
/// <exception cref="System.IO.IOException">in case of corrupted data or source stream problems</exception>
|
||||||
public BrotliInputStream(System.IO.Stream source, int byteReadBufferSize)
|
public BrotliInputStream(System.IO.Stream source, int byteReadBufferSize)
|
||||||
: this(source, byteReadBufferSize, null)
|
: this(source, byteReadBufferSize, null)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a
|
/// Creates a
|
||||||
/// <see cref="System.IO.Stream"/>
|
/// <see cref="System.IO.Stream"/>
|
||||||
/// wrapper that decompresses brotli data.
|
/// wrapper that decompresses brotli data.
|
||||||
/// <p> For byte-by-byte reading (
|
/// <p> For byte-by-byte reading (
|
||||||
/// <see cref="ReadByte()"/>
|
/// <see cref="ReadByte()"/>
|
||||||
/// ) internal buffer of specified size is
|
/// ) internal buffer of specified size is
|
||||||
/// allocated and used.
|
/// allocated and used.
|
||||||
/// <p> Will block the thread until first kilobyte of data of source is available.
|
/// <p> Will block the thread until first kilobyte of data of source is available.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="source">compressed data source</param>
|
/// <param name="source">compressed data source</param>
|
||||||
/// <param name="byteReadBufferSize">
|
/// <param name="byteReadBufferSize">
|
||||||
/// size of internal buffer used in case of
|
/// size of internal buffer used in case of
|
||||||
/// byte-by-byte reading
|
/// byte-by-byte reading
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="customDictionary">
|
/// <param name="customDictionary">
|
||||||
/// custom dictionary data;
|
/// custom dictionary data;
|
||||||
/// <see langword="null"/>
|
/// <see langword="null"/>
|
||||||
/// if not used
|
/// if not used
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <exception cref="System.IO.IOException">in case of corrupted data or source stream problems</exception>
|
/// <exception cref="System.IO.IOException">in case of corrupted data or source stream problems</exception>
|
||||||
public BrotliInputStream(System.IO.Stream source, int byteReadBufferSize, byte[] customDictionary)
|
public BrotliInputStream(System.IO.Stream source, int byteReadBufferSize, byte[] customDictionary)
|
||||||
{
|
{
|
||||||
if (byteReadBufferSize <= 0)
|
if (byteReadBufferSize <= 0)
|
||||||
{
|
{
|
||||||
throw new System.ArgumentException("Bad buffer size:" + byteReadBufferSize);
|
throw new System.ArgumentException("Bad buffer size:" + byteReadBufferSize);
|
||||||
}
|
}
|
||||||
else if (source == null)
|
else if (source == null)
|
||||||
{
|
{
|
||||||
throw new System.ArgumentException("source is null");
|
throw new System.ArgumentException("source is null");
|
||||||
}
|
}
|
||||||
this.buffer = new byte[byteReadBufferSize];
|
this.buffer = new byte[byteReadBufferSize];
|
||||||
this.remainingBufferBytes = 0;
|
this.remainingBufferBytes = 0;
|
||||||
this.bufferOffset = 0;
|
this.bufferOffset = 0;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Org.Brotli.Dec.State.SetInput(state, source);
|
Org.Brotli.Dec.State.SetInput(state, source);
|
||||||
}
|
}
|
||||||
catch (Org.Brotli.Dec.BrotliRuntimeException ex)
|
catch (Org.Brotli.Dec.BrotliRuntimeException ex)
|
||||||
{
|
{
|
||||||
throw new System.IO.IOException("Brotli decoder initialization failed", ex);
|
throw new System.IO.IOException("Brotli decoder initialization failed", ex);
|
||||||
}
|
}
|
||||||
if (customDictionary != null)
|
if (customDictionary != null)
|
||||||
{
|
{
|
||||||
Org.Brotli.Dec.Decode.SetCustomDictionary(state, customDictionary);
|
Org.Brotli.Dec.Decode.SetCustomDictionary(state, customDictionary);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary><inheritDoc/></summary>
|
/// <summary><inheritDoc/></summary>
|
||||||
/// <exception cref="System.IO.IOException"/>
|
/// <exception cref="System.IO.IOException"/>
|
||||||
public override void Close()
|
public override void Close()
|
||||||
{
|
{
|
||||||
Org.Brotli.Dec.State.Close(state);
|
Org.Brotli.Dec.State.Close(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary><inheritDoc/></summary>
|
/// <summary><inheritDoc/></summary>
|
||||||
/// <exception cref="System.IO.IOException"/>
|
/// <exception cref="System.IO.IOException"/>
|
||||||
public override int ReadByte()
|
public override int ReadByte()
|
||||||
{
|
{
|
||||||
if (bufferOffset >= remainingBufferBytes)
|
if (bufferOffset >= remainingBufferBytes)
|
||||||
{
|
{
|
||||||
remainingBufferBytes = Read(buffer, 0, buffer.Length);
|
remainingBufferBytes = Read(buffer, 0, buffer.Length);
|
||||||
bufferOffset = 0;
|
bufferOffset = 0;
|
||||||
if (remainingBufferBytes == -1)
|
if (remainingBufferBytes == -1)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return buffer[bufferOffset++] & unchecked((int)(0xFF));
|
return buffer[bufferOffset++] & unchecked((int)(0xFF));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary><inheritDoc/></summary>
|
/// <summary><inheritDoc/></summary>
|
||||||
/// <exception cref="System.IO.IOException"/>
|
/// <exception cref="System.IO.IOException"/>
|
||||||
public override int Read(byte[] destBuffer, int destOffset, int destLen)
|
public override int Read(byte[] destBuffer, int destOffset, int destLen)
|
||||||
{
|
{
|
||||||
if (destOffset < 0)
|
if (destOffset < 0)
|
||||||
{
|
{
|
||||||
throw new System.ArgumentException("Bad offset: " + destOffset);
|
throw new System.ArgumentException("Bad offset: " + destOffset);
|
||||||
}
|
}
|
||||||
else if (destLen < 0)
|
else if (destLen < 0)
|
||||||
{
|
{
|
||||||
throw new System.ArgumentException("Bad length: " + destLen);
|
throw new System.ArgumentException("Bad length: " + destLen);
|
||||||
}
|
}
|
||||||
else if (destOffset + destLen > destBuffer.Length)
|
else if (destOffset + destLen > destBuffer.Length)
|
||||||
{
|
{
|
||||||
throw new System.ArgumentException("Buffer overflow: " + (destOffset + destLen) + " > " + destBuffer.Length);
|
throw new System.ArgumentException("Buffer overflow: " + (destOffset + destLen) + " > " + destBuffer.Length);
|
||||||
}
|
}
|
||||||
else if (destLen == 0)
|
else if (destLen == 0)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int copyLen = System.Math.Max(remainingBufferBytes - bufferOffset, 0);
|
int copyLen = System.Math.Max(remainingBufferBytes - bufferOffset, 0);
|
||||||
if (copyLen != 0)
|
if (copyLen != 0)
|
||||||
{
|
{
|
||||||
copyLen = System.Math.Min(copyLen, destLen);
|
copyLen = System.Math.Min(copyLen, destLen);
|
||||||
System.Array.Copy(buffer, bufferOffset, destBuffer, destOffset, copyLen);
|
System.Array.Copy(buffer, bufferOffset, destBuffer, destOffset, copyLen);
|
||||||
bufferOffset += copyLen;
|
bufferOffset += copyLen;
|
||||||
destOffset += copyLen;
|
destOffset += copyLen;
|
||||||
destLen -= copyLen;
|
destLen -= copyLen;
|
||||||
if (destLen == 0)
|
if (destLen == 0)
|
||||||
{
|
{
|
||||||
return copyLen;
|
return copyLen;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
state.output = destBuffer;
|
state.output = destBuffer;
|
||||||
state.outputOffset = destOffset;
|
state.outputOffset = destOffset;
|
||||||
state.outputLength = destLen;
|
state.outputLength = destLen;
|
||||||
state.outputUsed = 0;
|
state.outputUsed = 0;
|
||||||
Org.Brotli.Dec.Decode.Decompress(state);
|
Org.Brotli.Dec.Decode.Decompress(state);
|
||||||
if (state.outputUsed == 0)
|
if (state.outputUsed == 0)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return state.outputUsed + copyLen;
|
return state.outputUsed + copyLen;
|
||||||
}
|
}
|
||||||
catch (Org.Brotli.Dec.BrotliRuntimeException ex)
|
catch (Org.Brotli.Dec.BrotliRuntimeException ex)
|
||||||
{
|
{
|
||||||
throw new System.IO.IOException("Brotli stream decoding failed", ex);
|
throw new System.IO.IOException("Brotli stream decoding failed", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// <{[INJECTED CODE]}>
|
// <{[INJECTED CODE]}>
|
||||||
public override bool CanRead {
|
public override bool CanRead {
|
||||||
get {return true;}
|
get {return true;}
|
||||||
@ -218,6 +218,6 @@ namespace Org.Brotli.Dec
|
|||||||
throw new System.NotSupportedException();
|
throw new System.NotSupportedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Flush() {}
|
public override void Flush() {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user