//internal static int SetBytes(AbstractByteBuffer buf, byte* addr, int index, Stream input, int length) //{ // IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length); // try // { // int readTotal = 0; // int readBytes; // byte[] tmp = tmpBuf.Array; // int offset = tmpBuf.ArrayOffset; // do // { // readBytes = input.Read(tmp, offset + readTotal, length - readTotal); // readTotal += readBytes; // } // while (readBytes > 0 && readTotal < length); // //if (readTotal > 0) // //{ // PlatformDependent.CopyMemory(tmp, offset, addr, readTotal); // //} // return readTotal; // } // finally // { // tmpBuf.Release(); // } //} internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer dst, int dstIndex, int length) { //if (dst is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.dst); } //if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Capacity)) //{ // ThrowHelper.ThrowIndexOutOfRangeException_DstIndex(dstIndex); //} if (0u >= (uint)length) { return; } if (dst.HasMemoryAddress) { IntPtr ptr = dst.AddressOfPinnedMemory(); if (ptr != IntPtr.Zero) { PlatformDependent.CopyMemory(addr, (byte *)(ptr + dstIndex), length); } else { fixed(byte *destination = &dst.GetPinnableMemoryAddress()) { PlatformDependent.CopyMemory(addr, destination + dstIndex, length); } } return; } GetBytes0(buf, addr, index, dst, dstIndex, length); }
protected AbstractUnpooledSlicedByteBuffer(IByteBuffer buffer, int index, int length) : base(length) { CheckSliceOutOfBounds(index, length, buffer); switch (buffer) { case AbstractUnpooledSlicedByteBuffer byteBuffer: _buffer = byteBuffer._buffer; _adjustment = byteBuffer._adjustment + index; break; case UnpooledDuplicatedByteBuffer _: _buffer = (AbstractByteBuffer)buffer.Unwrap(); _adjustment = index; break; default: _buffer = (AbstractByteBuffer)buffer; _adjustment = index; break; } SetWriterIndex0(length); }
internal UnpooledDuplicatedByteBuffer(AbstractByteBuffer buffer, int readerIndex, int writerIndex) : base(buffer.MaxCapacity) { switch (buffer) { case UnpooledDuplicatedByteBuffer duplicated: _buffer = duplicated._buffer; break; case AbstractPooledDerivedByteBuffer _: _buffer = (AbstractByteBuffer)buffer.Unwrap(); break; case AbstractArrayPooledDerivedByteBuffer _: _buffer = (AbstractByteBuffer)buffer.Unwrap(); break; default: _buffer = buffer; break; } SetIndex0(readerIndex, writerIndex); MarkIndex(); // Mark read and writer index }
internal static IByteBuffer Copy(AbstractByteBuffer buf, byte *addr, int index, int length) { IByteBuffer copy = buf.Allocator.DirectBuffer(length, buf.MaxCapacity); if (length != 0) { if (copy.HasMemoryAddress) { IntPtr ptr = copy.AddressOfPinnedMemory(); if (ptr != IntPtr.Zero) { PlatformDependent.CopyMemory(addr, (byte *)ptr, length); } else { fixed(byte *dst = ©.GetPinnableMemoryAddress()) { PlatformDependent.CopyMemory(addr, dst, length); } } copy.SetIndex(0, length); } else { copy.WriteBytes(buf, index, length); } } return(copy); }
internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer dst, int dstIndex, int length) { Contract.Requires(dst != null); if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Capacity)) { ThrowHelper.ThrowIndexOutOfRangeException_DstIndex(dstIndex); } if (dst.HasMemoryAddress) { IntPtr ptr = dst.AddressOfPinnedMemory(); if (ptr != IntPtr.Zero) { PlatformDependent.CopyMemory(addr, (byte *)(ptr + dstIndex), length); } else { fixed(byte *destination = &dst.GetPinnableMemoryAddress()) { PlatformDependent.CopyMemory(addr, destination + dstIndex, length); } } } else if (dst.HasArray) { PlatformDependent.CopyMemory(addr, dst.Array, dst.ArrayOffset + dstIndex, length); } else { dst.SetBytes(dstIndex, buf, index, length); } }
internal static int SetBytes(AbstractByteBuffer buf, byte *addr, int index, Stream input, int length) { if (length == 0) { return(0); } IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length); try { byte[] tmp = tmpBuf.Array; int offset = tmpBuf.ArrayOffset; int readBytes = input.Read(tmp, offset, length); if (readBytes > 0) { PlatformDependent.CopyMemory(tmp, offset, addr, readBytes); } return(readBytes); } finally { tmpBuf.Release(); } }
internal static Task <int> SetBytesAsync(AbstractByteBuffer buf, byte *addr, int index, Stream input, int length, CancellationToken cancellationToken) { if (length == 0) { return(TaskEx.Zero); } IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length); return(tmpBuf.SetBytesAsync(0, input, length, cancellationToken) .ContinueWith(t => { try { var read = t.Result; if (read > 0) { PlatformDependent.CopyMemory(tmpBuf.Array, tmpBuf.ArrayOffset, addr, read); } return read; } finally { tmpBuf.Release(); } })); }
internal T Init <T>( AbstractByteBuffer unwrapped, IByteBuffer wrapped, int readerIndex, int writerIndex, int maxCapacity) where T : AbstractPooledDerivedByteBuffer { wrapped.Retain(); // Retain up front to ensure the parent is accessible before doing more work. this.parent = wrapped; this.rootParent = unwrapped; try { this.SetMaxCapacity(maxCapacity); this.SetIndex0(readerIndex, writerIndex); // It is assumed the bounds checking is done by the caller. this.SetReferenceCount(1); wrapped = null; return((T)this); } finally { if (wrapped != null) { this.parent = this.rootParent = null; wrapped.Release(); } } }
internal static void SetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer src, int srcIndex, int length) { //if (src is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.src); } //if (MathUtil.IsOutOfBounds(srcIndex, length, src.Capacity)) //{ // ThrowHelper.ThrowIndexOutOfRangeException_SrcIndex(srcIndex); //} if (0u >= (uint)length) { return; } if (src.HasMemoryAddress) { IntPtr ptr = src.AddressOfPinnedMemory(); if (ptr != IntPtr.Zero) { PlatformDependent.CopyMemory((byte *)(ptr + srcIndex), addr, length); } else { fixed(byte *source = &src.GetPinnableMemoryAddress()) { PlatformDependent.CopyMemory(source + srcIndex, addr, length); } } return; } SetBytes0(buf, addr, index, src, srcIndex, length); }
static PooledSlicedByteBuffer NewInstance0(AbstractByteBuffer unwrapped, IByteBuffer wrapped, int adjustment, int length) { PooledSlicedByteBuffer slice = Recycler.Take(); slice.Init <PooledSlicedByteBuffer>(unwrapped, wrapped, 0, length, length); slice.DiscardMarks(); slice.adjustment = adjustment; return(slice); }
internal static PooledDuplicatedByteBuffer NewInstance(AbstractByteBuffer unwrapped, IByteBuffer wrapped, int readerIndex, int writerIndex) { PooledDuplicatedByteBuffer duplicate = Recycler.Take(); _ = duplicate.Init <PooledDuplicatedByteBuffer>(unwrapped, wrapped, readerIndex, writerIndex, unwrapped.MaxCapacity); _ = duplicate.MarkReaderIndex(); _ = duplicate.MarkWriterIndex(); return(duplicate); }
internal static int WriteAscii(AbstractByteBuffer buffer, int writerIndex, string value, int len) { // We can use the _set methods as these not need to do any index checks and reference checks. // This is possible as we called ensureWritable(...) before. for (int i = 0; i < len; i++) { buffer._SetByte(writerIndex++, (byte)value[i]); } return(len); }
private static void SetBytes0(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer src, int srcIndex, int length) { if (src.HasArray) { PlatformDependent.CopyMemory(src.Array, src.ArrayOffset + srcIndex, addr, length); } else { _ = src.GetBytes(srcIndex, buf, index, length); } }
private static void GetBytes0(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer dst, int dstIndex, int length) { if (dst.HasArray) { PlatformDependent.CopyMemory(addr, dst.Array, dst.ArrayOffset + dstIndex, length); } else { _ = dst.SetBytes(dstIndex, buf, index, length); } }
private static int WriteAscii0(AbstractByteBuffer buffer, int writerIndex, ICharSequence seq) { var len = seq.Count; // We can use the _set methods as these not need to do any index checks and reference checks. // This is possible as we called ensureWritable(...) before. for (int i = 0; i < len; i++) { buffer._SetByte(writerIndex++, AsciiString.CharToByte(seq[i])); } return(len); }
internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, byte[] dst, int dstIndex, int length) { Contract.Requires(dst != null); if (MathUtil.IsOutOfBounds(dstIndex, length, dst.Length)) { ThrowHelper.ThrowIndexOutOfRangeException_DstIndex(dstIndex); } if (length != 0) { PlatformDependent.CopyMemory(addr, dst, dstIndex, length); } }
// Fast-Path implementation internal static int WriteAscii(AbstractByteBuffer buffer, int writerIndex, ICharSequence seq) { if (seq is IHasAsciiSpan hasAscii) { _ = buffer.SetBytes(writerIndex, hasAscii.AsciiSpan); return(seq.Count); } if (seq is IHasUtf16Span hasUtf16) { return(WriteAscii0(buffer, writerIndex, hasUtf16.Utf16Span)); } return(WriteAscii0(buffer, writerIndex, seq)); }
internal static void GetBytes(AbstractByteBuffer buf, byte *addr, int index, Stream output, int length) { if (length != 0) { IByteBuffer tmpBuf = buf.Allocator.HeapBuffer(length); try { byte[] tmp = tmpBuf.Array; int offset = tmpBuf.ArrayOffset; PlatformDependent.CopyMemory(addr, tmp, offset, length); output.Write(tmp, offset, length); } finally { tmpBuf.Release(); } } }
internal UnpooledDuplicatedByteBuffer(AbstractByteBuffer buffer, int readerIndex, int writerIndex) : base(buffer.MaxCapacity) { if (buffer is UnpooledDuplicatedByteBuffer duplicated) { this.buffer = duplicated.buffer; } else if (buffer is AbstractPooledDerivedByteBuffer) { this.buffer = (AbstractByteBuffer)buffer.Unwrap(); } else { this.buffer = buffer; } this.SetIndex0(readerIndex, writerIndex); this.MarkIndex(); // Mark read and writer index }
internal static void SetBytes(AbstractByteBuffer buf, byte *addr, int index, IByteBuffer src, int srcIndex, int length) { Contract.Requires(src != null); if (MathUtil.IsOutOfBounds(srcIndex, length, src.Capacity)) { ThrowHelper.ThrowIndexOutOfRangeException_SrcIndex(srcIndex); } if (length != 0) { if (src.HasMemoryAddress) { IntPtr ptr = src.AddressOfPinnedMemory(); if (ptr != IntPtr.Zero) { PlatformDependent.CopyMemory((byte *)(ptr + srcIndex), addr, length); } else { fixed(byte *source = &src.GetPinnableMemoryAddress()) { PlatformDependent.CopyMemory(source + srcIndex, addr, length); } } } else if (src.HasArray) { PlatformDependent.CopyMemory(src.Array, src.ArrayOffset + srcIndex, addr, length); } else { src.GetBytes(srcIndex, buf, index, length); } } }
public UnpooledDuplicatedByteBuffer(AbstractByteBuffer buffer) : this(buffer, buffer.ReaderIndex, buffer.WriterIndex) { }
internal static void ThrowIndexOutOfRangeException_ReaderIndex(int minimumReadableBytes, int readerIndex, int writerIndex, AbstractByteBuffer buf) { throw GetIndexOutOfRangeException(); IndexOutOfRangeException GetIndexOutOfRangeException() { return(new IndexOutOfRangeException(string.Format("readerIndex({0}) + length({1}) exceeds writerIndex({2}): {3}", readerIndex, minimumReadableBytes, writerIndex, buf))); } }
public PooledNonRetainedSlicedByteBuffer(IReferenceCounted referenceCountDelegate, AbstractByteBuffer buffer, int index, int length) : base(buffer, index, length) { this.referenceCountDelegate = referenceCountDelegate; }
internal PooledNonRetainedDuplicateByteBuffer(IReferenceCounted referenceCountDelegate, AbstractByteBuffer buffer) : base(buffer) { this.referenceCountDelegate = referenceCountDelegate; }
private static void CheckMinWritableBounds(int minWritableBytes, int writerIndex, int maxCapacity, AbstractByteBuffer buf) { if (minWritableBytes > maxCapacity - writerIndex) { ThrowHelper.ThrowIndexOutOfRangeException_WriterIndex(minWritableBytes, writerIndex, maxCapacity, buf); } }
private static void CheckMinReadableBounds(int minimumReadableBytes, int readerIndex, int writerIndex, AbstractByteBuffer buf) { if (CheckBounds && (readerIndex > writerIndex - minimumReadableBytes)) { ThrowHelper.ThrowIndexOutOfRangeException_ReaderIndex(minimumReadableBytes, readerIndex, writerIndex, buf); } }
internal static PooledSlicedByteBuffer NewInstance(AbstractByteBuffer unwrapped, IByteBuffer wrapped, int index, int length) { CheckSliceOutOfBounds(index, length, unwrapped); return(NewInstance0(unwrapped, wrapped, index, length)); }
// No need to check length zero, the calling method already done it internal static void SetBytes(AbstractByteBuffer buf, byte *addr, int index, byte[] src, int srcIndex, int length) => PlatformDependent.CopyMemory(src, srcIndex, addr, length);
private static int WriteAscii0(AbstractByteBuffer buffer, int writerIndex, in ReadOnlySpan <char> utf16Source)
internal static int WriteAscii(AbstractByteBuffer buffer, int writerIndex, string value) { return(WriteAscii0(buffer, writerIndex, value.AsSpan())); }