Skip to content

Commit

Permalink
update based on review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ElizabethOkerio committed Jan 30, 2024
1 parent 410249c commit 758738c
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Microsoft.OData.Core/Json/ODataUtf8JsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ private void WriteEscapedStringChunk(ReadOnlySpan<char> sourceChunk, int firstIn
isFinalBlock
);

if (status == OperationStatus.Done)
if (status == OperationStatus.Done || status == OperationStatus.NeedMoreData)
{
written += bytesWritten;
WriteStringChunk(destination.Slice(0, bytesWritten), isFinalBlock);
Expand Down Expand Up @@ -601,16 +601,18 @@ private void WriteByteValueInChunks(ReadOnlySpan<byte> value)
this.bufferWriter.Write(doubleQuote.Slice(0, 1).Span);
this.Flush();

int chunkSize = 2048;
int chunkSize = 2732;

int prevBytesNotWritten = 0;

for (int i = 0; i < value.Length; i += chunkSize)
{
int remainingChars = Math.Min(chunkSize, value.Length - i);
bool isFinalBlock = (i + remainingChars) == value.Length;

ReadOnlySpan<byte> chunk = value.Slice(i, remainingChars);
ReadOnlySpan<byte> chunk = value.Slice(i-prevBytesNotWritten, remainingChars+prevBytesNotWritten);

Base64EncodeAndWriteChunk(chunk, isFinalBlock);
Base64EncodeAndWriteChunk(chunk, isFinalBlock, out prevBytesNotWritten);

this.FlushIfBufferThresholdReached();
}
Expand All @@ -625,19 +627,20 @@ private void WriteByteValueInChunks(ReadOnlySpan<byte> value)
/// <summary>
/// Encodes the specified chunk of bytes using Base64 encoding and writes the encoded data to the buffer writer.
/// </summary>
/// <param name="chunk">The ReadOnlySpan of bytes to be encoded.</param>
/// <param name="chunk">The chunk of bytes to be encoded.</param>
/// <param name="isFinalBlock">A boolean value indicating whether the chunk is the final block of data.</param>
private void Base64EncodeAndWriteChunk(ReadOnlySpan<byte> chunk, bool isFinalBlock)
/// <param name="prevBytesNotWritten">An out parameter indicating the number of bytes from the previous chunk that were not processed.</param>
private void Base64EncodeAndWriteChunk(ReadOnlySpan<byte> chunk, bool isFinalBlock, out int prevBytesNotWritten)
{
int encodingLength = Base64.GetMaxEncodedToUtf8Length(chunk.Length);
var output = this.bufferWriter.GetSpan(encodingLength);
prevBytesNotWritten = 0;

var output = bufferWriter.GetSpan(encodingLength);
OperationStatus status = Base64.EncodeToUtf8(chunk, output, out int consumed, out int written, isFinalBlock);

Debug.Assert(status == OperationStatus.Done);
Debug.Assert(consumed == chunk.Length);
prevBytesNotWritten = chunk.Length - consumed;

bufferWriter.Advance(written);
this.bufferWriter.Advance(written);
}

public void WriteValue(JsonElement value)
Expand Down Expand Up @@ -1066,6 +1069,10 @@ public async Task WriteValueAsync(byte[] value)
{
this.utf8JsonWriter.WriteNullValue();
}
else if (value.Length > bufferWriter.Capacity)
{
WriteByteValueInChunks(value.AsSpan());
}
else
{
this.utf8JsonWriter.WriteBase64StringValue(value);
Expand Down

0 comments on commit 758738c

Please sign in to comment.