| | | 1 | | using DeltaSharp.Util; |
| | | 2 | | using System.Buffers.Text; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using System.Runtime.InteropServices; |
| | | 5 | | |
| | | 6 | | namespace DeltaSharp.Format; |
| | | 7 | | internal class BinaryDeltaWriter : IDeltaWriter |
| | | 8 | | { |
| | | 9 | | private MemoryStream _ms; |
| | 23050 | 10 | | private byte[] _buffer => _ms.GetBuffer(); |
| | 23050 | 11 | | private int _position => (int)_ms.Position; |
| | | 12 | | |
| | 20200 | 13 | | public BinaryDeltaWriter() |
| | | 14 | | { |
| | 20200 | 15 | | _ms = new MemoryStream(); |
| | 20200 | 16 | | } |
| | | 17 | | |
| | 2840 | 18 | | public BinaryDeltaWriter(int sizeHint) |
| | | 19 | | { |
| | 2840 | 20 | | _ms = DeltaWriterBuffer.Create(sizeHint); |
| | 2840 | 21 | | } |
| | | 22 | | |
| | | 23 | | public void WriteLength(ulong length) |
| | | 24 | | { |
| | 23050 | 25 | | _ms.WriteByte((byte)'l'); |
| | | 26 | | |
| | 23050 | 27 | | Span<byte> encoded = stackalloc byte[9]; |
| | 23050 | 28 | | var written=VarIntFormatter.EncodeUnsafe(length, encoded); |
| | 23050 | 29 | | _ms.Write(encoded.Slice(0, written)); |
| | 23050 | 30 | | } |
| | | 31 | | |
| | | 32 | | public void WriteCopy(ulong position, ulong length) |
| | | 33 | | { |
| | 71820 | 34 | | _ms.WriteByte((byte)'c'); |
| | | 35 | | |
| | 71820 | 36 | | Span<byte> encoded = stackalloc byte[9]; |
| | | 37 | | |
| | | 38 | | //write position |
| | 71820 | 39 | | var written=VarIntFormatter.EncodeUnsafe(position, encoded); |
| | 71820 | 40 | | _ms.Write(encoded.Slice(0, written)); |
| | | 41 | | |
| | | 42 | | //write length |
| | 71820 | 43 | | written=VarIntFormatter.EncodeUnsafe(length, encoded); |
| | 71820 | 44 | | _ms.Write(encoded.Slice(0, written)); |
| | 71820 | 45 | | } |
| | | 46 | | |
| | | 47 | | public void WriteInsert(ReadOnlySpan<byte> s) |
| | | 48 | | { |
| | 67630 | 49 | | _ms.WriteByte((byte)'i'); |
| | | 50 | | |
| | | 51 | | //write insert length |
| | 67630 | 52 | | Span<byte> encoded = stackalloc byte[9]; |
| | 67630 | 53 | | var written=VarIntFormatter.EncodeUnsafe((ulong)s.Length, encoded); |
| | 67630 | 54 | | _ms.Write(encoded.Slice(0, written)); |
| | | 55 | | |
| | | 56 | | //write insert data |
| | 67630 | 57 | | DeltaWriterBuffer.WriteLiteral(_ms, s, trailerBytes: 10); // checksum opcode and varint |
| | 67630 | 58 | | } |
| | | 59 | | |
| | | 60 | | public void WriteChecksum(uint checksum) |
| | | 61 | | { |
| | 2950 | 62 | | _ms.WriteByte((byte)'v'); |
| | | 63 | | |
| | | 64 | | //write checksum value |
| | 2950 | 65 | | Span<byte> encoded = stackalloc byte[9]; |
| | 2950 | 66 | | var written=VarIntFormatter.EncodeUnsafe(checksum, encoded); |
| | 2950 | 67 | | _ms.Write(encoded.Slice(0, written)); |
| | 2950 | 68 | | } |
| | | 69 | | |
| | | 70 | | public ReadOnlyMemory<byte> GetOutput() |
| | | 71 | | { |
| | 23050 | 72 | | return _buffer.AsMemory().Slice(0,_position); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public void Dispose() |
| | | 76 | | { |
| | 23030 | 77 | | _ms.Dispose(); |
| | 23030 | 78 | | } |
| | | 79 | | } |