Asynchronous methods in readers and writers. (#1122)
Contributes to #650
This commit is contained in:
committed by
James Newton-King
parent
211b3b0db2
commit
f663060005
@@ -0,0 +1,104 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !(NET20 || NET35 || NET40 || PORTABLE40)
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal static class AsyncUtils
|
||||
{
|
||||
// Pre-allocate to avoid wasted allocations.
|
||||
public static readonly Task<bool> False = Task.FromResult(false);
|
||||
public static readonly Task<bool> True = Task.FromResult(true);
|
||||
|
||||
internal static Task<bool> ToAsync(this bool value) => value ? True : False;
|
||||
|
||||
public static Task CancelIfRequestedAsync(this CancellationToken cancellationToken)
|
||||
{
|
||||
return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : null;
|
||||
}
|
||||
|
||||
public static Task<T> CancelIfRequestedAsync<T>(this CancellationToken cancellationToken)
|
||||
{
|
||||
return cancellationToken.IsCancellationRequested ? FromCanceled<T>(cancellationToken) : null;
|
||||
}
|
||||
|
||||
// From 4.6 on we could use Task.FromCanceled(), but we need an equivalent for
|
||||
// previous frameworks.
|
||||
public static Task FromCanceled(this CancellationToken cancellationToken)
|
||||
{
|
||||
Debug.Assert(cancellationToken.IsCancellationRequested);
|
||||
return new Task(() => {}, cancellationToken);
|
||||
}
|
||||
|
||||
public static Task<T> FromCanceled<T>(this CancellationToken cancellationToken)
|
||||
{
|
||||
Debug.Assert(cancellationToken.IsCancellationRequested);
|
||||
return new Task<T>(() => default(T), cancellationToken);
|
||||
}
|
||||
|
||||
// Task.Delay(0) is optimised as a cached task within the framework, and indeed
|
||||
// the same cached task that Task.CompletedTask returns as of 4.6, but we'll add
|
||||
// our own cached field for previous frameworks.
|
||||
internal static readonly Task CompletedTask = Task.Delay(0);
|
||||
|
||||
public static Task WriteAsync(this TextWriter writer, char value, CancellationToken cancellationToken)
|
||||
{
|
||||
Debug.Assert(writer != null);
|
||||
return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value);
|
||||
}
|
||||
|
||||
public static Task WriteAsync(this TextWriter writer, string value, CancellationToken cancellationToken)
|
||||
{
|
||||
Debug.Assert(writer != null);
|
||||
return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value);
|
||||
}
|
||||
|
||||
public static Task WriteAsync(this TextWriter writer, char[] value, CancellationToken cancellationToken)
|
||||
{
|
||||
Debug.Assert(writer != null);
|
||||
return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value);
|
||||
}
|
||||
|
||||
public static Task WriteAsync(this TextWriter writer, char[] value, int start, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
Debug.Assert(writer != null);
|
||||
return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value, start, count);
|
||||
}
|
||||
|
||||
public static Task<int> ReadAsync(this TextReader reader, char[] buffer, int index, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
Debug.Assert(reader != null);
|
||||
return cancellationToken.IsCancellationRequested ? FromCanceled<int>(cancellationToken) : reader.ReadAsync(buffer, index, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -25,6 +25,10 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
#if !(NET20 || NET35 || NET40 || PORTABLE40)
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
@@ -45,7 +49,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
_writer = writer;
|
||||
}
|
||||
|
||||
public void Encode(byte[] buffer, int index, int count)
|
||||
private void ValidateEncode(byte[] buffer, int index, int count)
|
||||
{
|
||||
if (buffer == null)
|
||||
{
|
||||
@@ -66,36 +70,25 @@ namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count));
|
||||
}
|
||||
}
|
||||
|
||||
public void Encode(byte[] buffer, int index, int count)
|
||||
{
|
||||
ValidateEncode(buffer, index, count);
|
||||
|
||||
if (_leftOverBytesCount > 0)
|
||||
{
|
||||
int leftOverBytesCount = _leftOverBytesCount;
|
||||
while (leftOverBytesCount < 3 && count > 0)
|
||||
if(FulfillFromLeftover(buffer, index, ref count))
|
||||
{
|
||||
_leftOverBytes[leftOverBytesCount++] = buffer[index++];
|
||||
count--;
|
||||
}
|
||||
if (count == 0 && leftOverBytesCount < 3)
|
||||
{
|
||||
_leftOverBytesCount = leftOverBytesCount;
|
||||
return;
|
||||
}
|
||||
|
||||
int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0);
|
||||
WriteChars(_charsLine, 0, num2);
|
||||
}
|
||||
_leftOverBytesCount = count % 3;
|
||||
if (_leftOverBytesCount > 0)
|
||||
{
|
||||
count -= _leftOverBytesCount;
|
||||
if (_leftOverBytes == null)
|
||||
{
|
||||
_leftOverBytes = new byte[3];
|
||||
}
|
||||
for (int i = 0; i < _leftOverBytesCount; i++)
|
||||
{
|
||||
_leftOverBytes[i] = buffer[(index + count) + i];
|
||||
}
|
||||
}
|
||||
|
||||
StoreLeftOverBytes(buffer, index, ref count);
|
||||
|
||||
int num4 = index + count;
|
||||
int length = LineSizeInBytes;
|
||||
while (index < num4)
|
||||
@@ -110,6 +103,44 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
}
|
||||
|
||||
private void StoreLeftOverBytes(byte[] buffer, int index, ref int count)
|
||||
{
|
||||
int leftOverBytesCount = count % 3;
|
||||
if (leftOverBytesCount > 0)
|
||||
{
|
||||
count -= leftOverBytesCount;
|
||||
if (_leftOverBytes == null)
|
||||
{
|
||||
_leftOverBytes = new byte[3];
|
||||
}
|
||||
|
||||
for (int i = 0; i < leftOverBytesCount; i++)
|
||||
{
|
||||
_leftOverBytes[i] = buffer[index + count + i];
|
||||
}
|
||||
}
|
||||
|
||||
_leftOverBytesCount = leftOverBytesCount;
|
||||
}
|
||||
|
||||
private bool FulfillFromLeftover(byte[] buffer, int index, ref int count)
|
||||
{
|
||||
int leftOverBytesCount = _leftOverBytesCount;
|
||||
while (leftOverBytesCount < 3 && count > 0)
|
||||
{
|
||||
_leftOverBytes[leftOverBytesCount++] = buffer[index++];
|
||||
count--;
|
||||
}
|
||||
|
||||
if (count == 0 && leftOverBytesCount < 3)
|
||||
{
|
||||
_leftOverBytesCount = leftOverBytesCount;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
if (_leftOverBytesCount > 0)
|
||||
@@ -124,5 +155,63 @@ namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
_writer.Write(chars, index, count);
|
||||
}
|
||||
|
||||
#if !(NET20 || NET35 || NET40 || PORTABLE40)
|
||||
|
||||
public async Task EncodeAsync(byte[] buffer, int index, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
ValidateEncode(buffer, index, count);
|
||||
|
||||
if (_leftOverBytesCount > 0)
|
||||
{
|
||||
if (FulfillFromLeftover(buffer, index, ref count))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0);
|
||||
await WriteCharsAsync(_charsLine, 0, num2, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
StoreLeftOverBytes(buffer, index, ref count);
|
||||
|
||||
int num4 = index + count;
|
||||
int length = LineSizeInBytes;
|
||||
while (index < num4)
|
||||
{
|
||||
if (index + length > num4)
|
||||
{
|
||||
length = num4 - index;
|
||||
}
|
||||
int num6 = Convert.ToBase64CharArray(buffer, index, length, _charsLine, 0);
|
||||
await WriteCharsAsync(_charsLine, 0, num6, cancellationToken).ConfigureAwait(false);
|
||||
index += length;
|
||||
}
|
||||
}
|
||||
|
||||
private Task WriteCharsAsync(char[] chars, int index, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
return _writer.WriteAsync(chars, index, count, cancellationToken);
|
||||
}
|
||||
|
||||
public Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return cancellationToken.FromCanceled();
|
||||
}
|
||||
|
||||
if (_leftOverBytesCount > 0)
|
||||
{
|
||||
int count = Convert.ToBase64CharArray(_leftOverBytes, 0, _leftOverBytesCount, _charsLine, 0);
|
||||
_leftOverBytesCount = 0;
|
||||
return WriteCharsAsync(_charsLine, 0, count, cancellationToken);
|
||||
}
|
||||
|
||||
return AsyncUtils.CompletedTask;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
@@ -24,17 +24,16 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
#if !(NET20 || NET35 || NET40 || PORTABLE40)
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
using System.Collections.Generic;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
using System.Linq;
|
||||
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
@@ -141,6 +140,38 @@ namespace Newtonsoft.Json.Utilities
|
||||
return false;
|
||||
}
|
||||
|
||||
private static int FirstCharToEscape(string s, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling)
|
||||
{
|
||||
for (int i = 0; i != s.Length; i++)
|
||||
{
|
||||
char c = s[i];
|
||||
|
||||
if (c < charEscapeFlags.Length)
|
||||
{
|
||||
if (charEscapeFlags[c])
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
else if (stringEscapeHandling == StringEscapeHandling.EscapeNonAscii)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '\u0085':
|
||||
case '\u2028':
|
||||
case '\u2029':
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters,
|
||||
bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool<char> bufferPool, ref char[] writeBuffer)
|
||||
{
|
||||
@@ -277,14 +308,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
else
|
||||
{
|
||||
int length = s.Length - lastWritePosition;
|
||||
|
||||
if (writeBuffer == null || writeBuffer.Length < length)
|
||||
{
|
||||
writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer);
|
||||
}
|
||||
|
||||
s.CopyTo(lastWritePosition, writeBuffer, 0, length);
|
||||
int length = WriteEscapedStringThroughBuffer(s, lastWritePosition, bufferPool, ref writeBuffer);
|
||||
|
||||
// write remaining text
|
||||
writer.Write(writeBuffer, 0, length);
|
||||
@@ -298,6 +322,20 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
}
|
||||
|
||||
private static int WriteEscapedStringThroughBuffer(string s, int lastWritePosition, IArrayPool<char> bufferPool, ref char[] writeBuffer)
|
||||
{
|
||||
int length = s.Length - lastWritePosition;
|
||||
|
||||
if (writeBuffer == null || writeBuffer.Length < length)
|
||||
{
|
||||
writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer);
|
||||
}
|
||||
|
||||
s.CopyTo(lastWritePosition, writeBuffer, 0, length);
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters, StringEscapeHandling stringEscapeHandling)
|
||||
{
|
||||
bool[] charEscapeFlags = GetCharEscapeFlags(stringEscapeHandling, delimiter);
|
||||
@@ -309,5 +347,182 @@ namespace Newtonsoft.Json.Utilities
|
||||
return w.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
#if !(NET20 || NET35 || NET40 || PORTABLE40)
|
||||
|
||||
public static Task WriteEscapedJavaScriptStringAsync(TextWriter writer, string s, char delimiter, bool appendDelimiters, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
if (appendDelimiters)
|
||||
{
|
||||
return WriteEscapedJavaScriptStringWithDelimitersAsync(writer, s, delimiter, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(s))
|
||||
{
|
||||
return cancellationToken.CancelIfRequestedAsync() ?? AsyncUtils.CompletedTask;
|
||||
}
|
||||
|
||||
return WriteEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken);
|
||||
}
|
||||
|
||||
private static async Task WriteEscapedJavaScriptStringWithDelimitersAsync(TextWriter writer, string s, char delimiter,
|
||||
bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken)
|
||||
{
|
||||
await writer.WriteAsync(delimiter).ConfigureAwait(false);
|
||||
|
||||
if (!string.IsNullOrEmpty(s))
|
||||
{
|
||||
await WriteEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await writer.WriteAsync(delimiter).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static Task WriteEscapedJavaScriptStringWithoutDelimitersAsync(
|
||||
TextWriter writer, string s, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling,
|
||||
JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken)
|
||||
{
|
||||
int i = FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling);
|
||||
return i == -1
|
||||
? writer.WriteAsync(s, cancellationToken)
|
||||
: WriteDefinitelyEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, i, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken);
|
||||
}
|
||||
|
||||
private static async Task WriteDefinitelyEscapedJavaScriptStringWithoutDelimitersAsync(
|
||||
TextWriter writer, string s, int lastWritePosition, bool[] charEscapeFlags,
|
||||
StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (writeBuffer == null || writeBuffer.Length < lastWritePosition)
|
||||
{
|
||||
writeBuffer = client.EnsureWriteBuffer(lastWritePosition, UnicodeTextLength);
|
||||
}
|
||||
|
||||
if (lastWritePosition != 0)
|
||||
{
|
||||
s.CopyTo(0, writeBuffer, 0, lastWritePosition);
|
||||
|
||||
// write unchanged chars at start of text.
|
||||
await writer.WriteAsync(writeBuffer, 0, lastWritePosition, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
int length;
|
||||
bool isEscapedUnicodeText = false;
|
||||
string escapedValue = null;
|
||||
|
||||
for (int i = lastWritePosition; i < s.Length; i++)
|
||||
{
|
||||
char c = s[i];
|
||||
|
||||
if (c < charEscapeFlags.Length && !charEscapeFlags[c])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case '\t':
|
||||
escapedValue = @"\t";
|
||||
break;
|
||||
case '\n':
|
||||
escapedValue = @"\n";
|
||||
break;
|
||||
case '\r':
|
||||
escapedValue = @"\r";
|
||||
break;
|
||||
case '\f':
|
||||
escapedValue = @"\f";
|
||||
break;
|
||||
case '\b':
|
||||
escapedValue = @"\b";
|
||||
break;
|
||||
case '\\':
|
||||
escapedValue = @"\\";
|
||||
break;
|
||||
case '\u0085': // Next Line
|
||||
escapedValue = @"\u0085";
|
||||
break;
|
||||
case '\u2028': // Line Separator
|
||||
escapedValue = @"\u2028";
|
||||
break;
|
||||
case '\u2029': // Paragraph Separator
|
||||
escapedValue = @"\u2029";
|
||||
break;
|
||||
default:
|
||||
if (c < charEscapeFlags.Length || stringEscapeHandling == StringEscapeHandling.EscapeNonAscii)
|
||||
{
|
||||
if (c == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
|
||||
{
|
||||
escapedValue = @"\'";
|
||||
}
|
||||
else if (c == '"' && stringEscapeHandling != StringEscapeHandling.EscapeHtml)
|
||||
{
|
||||
escapedValue = @"\""";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (writeBuffer.Length < UnicodeTextLength)
|
||||
{
|
||||
writeBuffer = client.EnsureWriteBuffer(UnicodeTextLength, 0);
|
||||
}
|
||||
|
||||
StringUtils.ToCharAsUnicode(c, writeBuffer);
|
||||
|
||||
isEscapedUnicodeText = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (i > lastWritePosition)
|
||||
{
|
||||
length = i - lastWritePosition + (isEscapedUnicodeText ? UnicodeTextLength : 0);
|
||||
int start = isEscapedUnicodeText ? UnicodeTextLength : 0;
|
||||
|
||||
if (writeBuffer.Length < length)
|
||||
{
|
||||
writeBuffer = client.EnsureWriteBuffer(length, UnicodeTextLength);
|
||||
}
|
||||
|
||||
s.CopyTo(lastWritePosition, writeBuffer, start, length - start);
|
||||
|
||||
// write unchanged chars before writing escaped text
|
||||
await writer.WriteAsync(writeBuffer, start, length - start, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
lastWritePosition = i + 1;
|
||||
if (!isEscapedUnicodeText)
|
||||
{
|
||||
await writer.WriteAsync(escapedValue, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
await writer.WriteAsync(writeBuffer, 0, UnicodeTextLength, cancellationToken).ConfigureAwait(false);
|
||||
isEscapedUnicodeText = false;
|
||||
}
|
||||
}
|
||||
|
||||
length = s.Length - lastWritePosition;
|
||||
|
||||
if (length != 0)
|
||||
{
|
||||
if (writeBuffer.Length < length)
|
||||
{
|
||||
writeBuffer = client.EnsureWriteBuffer(length, 0);
|
||||
}
|
||||
|
||||
s.CopyTo(lastWritePosition, writeBuffer, 0, length);
|
||||
|
||||
// write remaining text
|
||||
await writer.WriteAsync(writeBuffer, 0, length, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user