1
0
mirror of https://github.com/chylex/Brotli-Builder.git synced 2024-12-22 16:42:46 +01:00
Brotli-Builder/BrotliLib/Brotli/Output/BrotliOutputStored.cs

46 lines
1.5 KiB
C#

using System;
using System.IO;
using System.Text;
namespace BrotliLib.Brotli.Output{
/// <summary>
/// Stores all generated output in memory.
/// </summary>
public class BrotliOutputStored : IBrotliOutput{
public int OutputSize => (int)decompressedStream.Length;
public byte[] AsBytes => decompressedStream.ToArray();
public string AsUTF8 => Encoding.UTF8.GetString(decompressedStream.ToArray());
private readonly MemoryStream decompressedStream = new MemoryStream();
public BrotliOutputStored(){}
private BrotliOutputStored(BrotliOutputStored original){
original.decompressedStream.CopyTo(decompressedStream);
}
public void Write(byte value){
decompressedStream.WriteByte(value);
}
public void Write(byte[] bytes){
decompressedStream.Write(bytes, 0, bytes.Length);
}
public byte GetByte(int distance){
long prevPos = decompressedStream.Position;
decompressedStream.Position -= distance;
int readByte = decompressedStream.ReadByte();
decompressedStream.Position = prevPos;
return readByte >= 0 ? (byte)readByte : throw new ArgumentOutOfRangeException(nameof(distance), "Distance is out of range: " + distance + " > " + decompressedStream.Length);
}
public IBrotliOutput Clone(){
return new BrotliOutputStored(this);
}
}
}