public BinaryRobloxFileChunk(BinaryRobloxFileWriter writer, bool compress = true) { if (!writer.WritingChunk) { throw new Exception("BinaryRobloxFileChunk: Supplied writer must have WritingChunk set to true."); } Stream stream = writer.BaseStream; using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, true)) { long length = (stream.Position - writer.ChunkStart); stream.Position = writer.ChunkStart; Size = (int)length; Data = reader.ReadBytes(Size); } CompressedData = LZ4Codec.Encode(Data, 0, Size); CompressedSize = CompressedData.Length; if (!compress || CompressedSize > Size) { CompressedSize = 0; CompressedData = new byte[0]; } ChunkType = writer.ChunkType; Reserved = 0; }
public void WriteChunk(BinaryRobloxFileWriter writer) { // Record where we are when we start writing. var stream = writer.BaseStream; long startPos = stream.Position; // Write the chunk's data. writer.WriteString(ChunkType, true); writer.Write(CompressedSize); writer.Write(Size); writer.Write(Reserved); if (CompressedSize > 0) { writer.Write(CompressedData); } else { writer.Write(Data); } // Capture the data we wrote into a byte[] array. long endPos = stream.Position; int length = (int)(endPos - startPos); using (MemoryStream buffer = new MemoryStream()) { stream.Position = startPos; stream.CopyTo(buffer, length); WriteBuffer = buffer.ToArray(); HasWriteBuffer = true; } }