protected override void Sync(int i, int offsetIntoDataStream, int binSize, IStreamReader <T> dataReader) { var spread = this[i]; var builder = spread.ToBuilder(); builder.Count = binSize; var slicesToRead = binSize; using (var buffer = MemoryPool <T> .GetBuffer()) { var j = 0; dataReader.Position = offsetIntoDataStream; while (slicesToRead > 0) { var slicesRead = dataReader.Read(buffer, 0, Math.Min(slicesToRead, buffer.Length)); if (slicesRead > 0) { for (int k = 0; k < slicesRead; k++) { builder[j++] = buffer[k]; } slicesToRead -= slicesRead; } else { break; } } } this[i] = builder.ToSpread(); }
public static ArraySegment <byte> SerializeUnsafe <T>(string callback, T obj, IJsonFormatterResolver resolver) { var writer = new JsonWriter(MemoryPool.GetBuffer()); Serialize(ref writer, callback, obj, resolver); return(writer.GetBuffer()); }
public static byte[] PrettyPrintByteArray(string json) { var reader = new JsonReaderUtf8(Encoding.UTF8.GetBytes(json)); var writer = new JsonWriterUtf8(MemoryPool.GetBuffer()); WritePrittyPrint(reader, writer, 0); return(writer.ToUtf8ByteArray()); }
public static byte[] PrettyPrintByteArray(byte[] json, int offset) { var reader = new JsonReaderUtf8(json, offset); var writer = new JsonWriterUtf8(MemoryPool.GetBuffer()); WritePrittyPrint(reader, writer, 0); return(writer.ToUtf8ByteArray()); }
/// <summary> /// Serialize to binary with specified resolver. /// </summary> public static byte[] Serialize <T>(string callback, T value, IJsonFormatterResolver resolver) { var writer = new JsonWriter(MemoryPool.GetBuffer()); Serialize <T>(ref writer, callback, value, resolver); return(writer.ToUtf8ByteArray()); }
public static string PrettyPrint(string json) { var reader = new JsonReader(Encoding.UTF8.GetBytes(json)); var writer = new JsonWriter(MemoryPool.GetBuffer()); WritePrittyPrint(ref reader, ref writer, 0); return(writer.ToString()); }
public static string PrettyPrint(byte[] json, int offset) { var reader = new JsonReader(json, offset); var writer = new JsonWriter(MemoryPool.GetBuffer()); WritePrittyPrint(ref reader, ref writer, 0); return(writer.ToString()); }
public void Evaluate(int spreadMax) { // Should any of the inputs do not have any data exit early if (spreadMax == 0) { LeftOutStream.Length = 0; RightOutStream.Length = 0; return; } // In case nothing changed also do an early exit - important if T is a string or a reference type if (!InputStream.IsChanged && !IndexStream.IsChanged) { return; } // Grab buffers and reader/writer using (var buffer = MemoryPool <T> .GetBuffer()) using (var reader = InputStream.GetReader()) using (var leftWriter = LeftOutStream.GetDynamicWriter()) using (var rightWriter = RightOutStream.GetDynamicWriter()) { foreach (var index in IndexStream) { // Set reader to its initial position reader.Position = 0; // Write everything up to the given index to the left output int numSlicesToRead; // split at 0 could mean all to the left or all to the right // for now let's put all to the right if (index >= 0) { numSlicesToRead = Math.Min(index, InputStream.Length); } else { numSlicesToRead = Math.Max(InputStream.Length + index, 0); } while (numSlicesToRead > 0) { var numSlicesREAD = reader.Read(buffer, 0, Math.Min(numSlicesToRead, buffer.Length)); leftWriter.Write(buffer, 0, numSlicesREAD); numSlicesToRead -= numSlicesREAD; } // Write whatever remains to the right output while (!reader.Eos) { var numSlicesRead = reader.Read(buffer, 0, buffer.Length); rightWriter.Write(buffer, 0, numSlicesRead); } } } }
public static byte[] Serialize <T>(T value, IJsonFormatterResolver resolver) { if (resolver == null) { resolver = JsonSerializer.DefaultResolver; } var writer = new JsonWriterUtf8(MemoryPool.GetBuffer()); var formatter = resolver.GetFormatterWithVerify <T>(); formatter.Serialize(writer, value, resolver); return(writer.ToUtf8ByteArray()); }
/// <summary> /// Serialize to JsonString with specified resolver. /// </summary> public static string ToJsonString <T>(T value, IJsonFormatterResolver resolver) { if (resolver == null) { resolver = DefaultResolver; } var writer = new JsonWriter(MemoryPool.GetBuffer()); var formatter = resolver.GetFormatterWithVerify <T>(); formatter.Serialize(ref writer, value, resolver); return(writer.ToString()); }
/// <summary> /// Serialize to binary with specified resolver. Get the raw memory pool byte[]. The result can not share across thread and can not hold, so use quickly. /// </summary> public static ArraySegment <byte> SerializeUnsafe <T>(T value, IJsonFormatterResolver resolver) { if (resolver == null) { resolver = DefaultResolver; } var writer = new JsonWriter(MemoryPool.GetBuffer()); var formatter = resolver.GetFormatterWithVerify <T>(); formatter.Serialize(ref writer, value, resolver); return(writer.GetBuffer()); }
public static bool StreamEquals(this Stream stream1, Stream stream2) { if (stream1 == null && stream2 != null) { return(false); } if (stream1 != null && stream2 == null) { return(false); } stream1.Position = 0; stream2.Position = 0; try { using (var buffer1 = MemoryPool <byte> .GetBuffer()) using (var buffer2 = MemoryPool <byte> .GetBuffer()) { var b1 = buffer1.Array; var b2 = buffer2.Array; var bufferSize = b1.Length; while (true) { var count1 = stream1.Read(b1, 0, bufferSize); var count2 = stream2.Read(b2, 0, bufferSize); if (count1 != count2) { return(false); } if (count1 == 0) { return(true); } if (!b1.ContentEquals(b2)) { return(false); } } } } finally { stream1.Position = 0; stream2.Position = 0; } }
public static T Deserialize <T>(Stream stream, IJsonFormatterResolver resolver) { if (resolver == null) { resolver = JsonSerializer.DefaultResolver; } #if NETSTANDARD && !NET45 var ms = stream as MemoryStream; if (ms != null) { ArraySegment <byte> buf2; if (ms.TryGetBuffer(out buf2)) { // when token is number, can not use from pool(can not find end line). var token = new JsonReaderUtf8(buf2.Array, buf2.Offset).GetCurrentJsonToken(); if (token == JsonToken.Number) { var buf3 = new byte[buf2.Count]; Buffer.BlockCopy(buf2.Array, buf2.Offset, buf3, 0, buf3.Length); return(Deserialize <T>(buf3, 0, resolver)); } return(Deserialize <T>(buf2.Array, buf2.Offset, resolver)); } } #endif { var buf = MemoryPool.GetBuffer(); var len = FillFromStream(stream, ref buf); // when token is number, can not use from pool(can not find end line). var token = new JsonReaderUtf8(buf).GetCurrentJsonToken(); if (token == JsonToken.Number) { buf = ByteArrayUtil.FastCloneWithResize(buf, len); } return(Deserialize <T>(buf, resolver)); } }
public static T Deserialize <T>(Stream stream, IJsonFormatterResolver resolver) { if (resolver == null) { resolver = DefaultResolver; } #if NETSTANDARD && !NET45 var ms = stream as MemoryStream; if (ms != null) { ArraySegment <byte> buf2; if (ms.TryGetBuffer(out buf2)) { return(Deserialize <T>(buf2.Array, buf2.Offset, resolver)); } } #endif var buf = MemoryPool.GetBuffer(); FillFromStream(stream, ref buf); return(Deserialize <T>(buf, resolver)); }
public Utf8Writer(Stream stream, byte[]?buffer = null) { this._Stream = stream; this._Buffer = buffer ?? MemoryPool.GetBuffer(); this._Offset = 0; }
public static bool StreamEquals(this Stream stream1, Stream stream2) { if (stream1 == null && stream2 != null) { return(false); } if (stream1 != null && stream2 == null) { return(false); } if (stream1.Equals(stream2)) { return(true); } stream1.Position = 0; stream2.Position = 0; try { using (var buffer1 = MemoryPool <byte> .GetBuffer()) using (var buffer2 = MemoryPool <byte> .GetBuffer()) { var bufferSize = buffer1.Length; while (true) { var count1 = stream1.Read(buffer1, 0, bufferSize); // Check the case where stream1 and stream2 use the same // underlying stream. if (stream1.Position == stream2.Position) { return(true); } var count2 = stream2.Read(buffer2, 0, bufferSize); if (count1 != count2) { return(false); } if (count1 == 0) { return(true); } if (count1 != bufferSize) { for (int i = 0; i < count1; i++) { if (buffer1[i] != buffer2[i]) { return(false); } } } else { if (!ArrayExtensions.ContentEquals(buffer1, buffer2)) { return(false); } } } } } finally { stream1.Position = 0; stream2.Position = 0; } }