public static void ArraySegmentAsSpan() { int[] a = { 19, -17 }; ArraySegment <int> segmentInt = new ArraySegment <int>(a, 1, 1); ReadOnlySpan <int> spanInt = segmentInt.AsReadOnlySpan(); spanInt.Validate(-17); long[] b = { 1, -3, 7, -15, 31 }; ArraySegment <long> segmentLong = new ArraySegment <long>(b, 1, 3); ReadOnlySpan <long> spanLong = segmentLong.AsReadOnlySpan(); spanLong.Validate(-3, 7, -15); object o1 = new object(); object o2 = new object(); object o3 = new object(); object o4 = new object(); object[] c = { o1, o2, o3, o4 }; ArraySegment <object> segmentObject = new ArraySegment <object>(c, 0, 2); ReadOnlySpan <object> spanObject = segmentObject.AsReadOnlySpan(); spanObject.ValidateReferenceType(o1, o2); }
private static unsafe int Compare(ArraySegment <byte> firstSeg, ArraySegment <byte> secondSeg) { var first = firstSeg.AsReadOnlySpan(); var second = secondSeg.AsReadOnlySpan(); int defaulter = 0; int minCnt = first.Length; if (first.Length < second.Length) { defaulter = -1; } else if (second.Length < minCnt) { defaulter = 1; minCnt = second.Length; } int cmp; fixed(void *ptr1 = &first.DangerousGetPinnableReference()) fixed(void *ptr2 = &second.DangerousGetPinnableReference()) { cmp = memcmp(ptr1, ptr2, new IntPtr(minCnt)); } return(cmp == 0 ? defaulter : cmp); }
public static void LongArraySegmentAsSpan() { long[] b = { 1, -3, 7, -15, 31 }; ArraySegment <long> segmentLong = new ArraySegment <long>(b, 1, 3); ReadOnlySpan <long> spanLong = segmentLong.AsReadOnlySpan(); spanLong.Validate(-3, 7, -15); }
public static void IntArraySegmentAsSpan() { int[] a = { 19, -17 }; ArraySegment <int> segmentInt = new ArraySegment <int>(a, 1, 1); ReadOnlySpan <int> spanInt = segmentInt.AsReadOnlySpan(); spanInt.Validate(-17); }
/// <summary>Decompress the encapsulation payload if it is compressed. Compressed encapsulations are /// only supported with 2.0 encoding.</summary> public void DecompressPayload() { if (!HasCompressedPayload) { throw new InvalidOperationException("the payload is not compressed"); } else { ArraySegment <byte> encapsulation = GetEncapsulation(); ReadOnlySpan <byte> buffer = encapsulation.AsReadOnlySpan(); (int size, int sizeLength, Encoding _) = buffer.ReadEncapsulationHeader(Protocol.GetEncoding()); // Read the decompressed size that is written after the compression status byte when the payload is // compressed +3 corresponds to (Encoding 2 bytes, Compression status 1 byte) (int decompressedSize, int decompressedSizeLength) = buffer.Slice(sizeLength + 3).ReadSize20(); if (decompressedSize > _sizeMax) { throw new InvalidDataException(@$ "decompressed size of {decompressedSize } bytes is greater than the configured IncomingFrameSizeMax value"); } // We are going to replace the Data segment with a new Data segment/array that contains a decompressed // encapsulation. byte[] decompressedData = new byte[Data.Count - size + decompressedSize]; // Index of the start of the GZip data in Data int gzipIndex = encapsulation.Offset - Data.Offset + sizeLength + 3; // Copy the data before the encapsulation to the new buffer Data.AsSpan(0, gzipIndex).CopyTo(decompressedData); // Copy the binary context (if any) after the encapsulation Data.AsSpan(Payload.Offset + Payload.Count - Data.Offset).CopyTo( decompressedData.AsSpan(gzipIndex + decompressedSize - 3)); // Set the compression status to '0' not-compressed decompressedData[gzipIndex - 1] = 0; using var decompressedStream = new MemoryStream(decompressedData, gzipIndex, decompressedData.Length - gzipIndex); Debug.Assert(encapsulation.Array != null); using var compressed = new GZipStream( new MemoryStream(encapsulation.Array, encapsulation.Offset + sizeLength + 3 + decompressedSizeLength, encapsulation.Count - sizeLength - 3 - decompressedSizeLength), CompressionMode.Decompress); compressed.CopyTo(decompressedStream); // +3 corresponds to (Encoding 2 bytes and Compression status 1 byte), that are part of the // decompressed size, but are not GZip compressed. if (decompressedStream.Position + 3 != decompressedSize) { throw new InvalidDataException( @$ "received GZip compressed payload with a decompressed size of only { decompressedStream.Position} bytes {decompressedSize}"); }
internal static ArraySegment <byte> Decompress(ArraySegment <byte> compressed, int headerSize, int frameSizeMax) { Debug.Assert(IsLoaded); int decompressedSize = compressed.AsReadOnlySpan(headerSize, 4).ReadInt(); if (decompressedSize <= headerSize) { throw new InvalidDataException( $"received compressed ice1 frame with a decompressed size of only {decompressedSize} bytes"); } if (decompressedSize > frameSizeMax) { throw new InvalidDataException( $"decompressed size of {decompressedSize} bytes is greater than Ice.IncomingFrameSizeMax value"); } byte[] decompressed = new byte[decompressedSize]; // Prevent GC from moving the byte array, this allow to take the object address and pass it to bzip2 calls. var decompressedHandle = GCHandle.Alloc(decompressed, GCHandleType.Pinned); var compressedHandle = GCHandle.Alloc(compressed.Array, GCHandleType.Pinned); var bzStream = new BZStream(decompressedHandle.AddrOfPinnedObject() + headerSize, (uint)(decompressedSize - headerSize)); BzStatus rc; try { rc = (BzStatus)BZ2_bzDecompressInit(ref bzStream, 0, 0); if (rc != BzStatus.Ok) { throw new TransportException($"bzip2 decompression failed: {rc}"); } bzStream.NextIn = compressedHandle.AddrOfPinnedObject() + compressed.Offset + headerSize + 4; bzStream.AvailIn = (uint)(compressed.Count - headerSize - 4); rc = (BzStatus)BZ2_bzDecompress(ref bzStream); if (rc != BzStatus.StreamEnd) { throw new TransportException($"bzip2 decompression failed: {rc}"); } } finally { rc = (BzStatus)BZ2_bzDecompressEnd(ref bzStream); Debug.Assert(rc == BzStatus.Ok); decompressedHandle.Free(); compressedHandle.Free(); } compressed.AsSpan(0, headerSize).CopyTo(decompressed); return(decompressed); }
public static void ObjectArraySegmentAsSpan() { object o1 = new object(); object o2 = new object(); object o3 = new object(); object o4 = new object(); object[] c = { o1, o2, o3, o4 }; ArraySegment <object> segmentObject = new ArraySegment <object>(c, 0, 2); ReadOnlySpan <object> spanObject = segmentObject.AsReadOnlySpan(); spanObject.ValidateReferenceType(o1, o2); }
private async Task <bool> TryGetNextChunkAsync() { Debug.Assert(_chunkBytesRemaining == 0); // Read the start of the chunk line. _connection._allowedReadLineBytes = MaxChunkBytesAllowed; ArraySegment <byte> line = await _connection.ReadNextLineAsync().ConfigureAwait(false); // Parse the hex value. if (!Utf8Parser.TryParse(line.AsReadOnlySpan(), out ulong chunkSize, out int bytesConsumed, 'X')) { throw new IOException(SR.net_http_invalid_response); }
public static void ZeroLengthArraySegmentAsReadOnlySpan() { int[] empty = Array.Empty <int>(); ArraySegment <int> emptySegment = new ArraySegment <int>(empty); ReadOnlySpan <int> span = emptySegment.AsReadOnlySpan(); span.Validate(); int[] a = { 19, -17 }; ArraySegment <int> segmentInt = new ArraySegment <int>(a, 1, 0); ReadOnlySpan <int> spanInt = segmentInt.AsReadOnlySpan(); spanInt.Validate(); }