public override java.nio.LongBuffer compact() { if (byteBuffer.isReadOnly()) { throw new java.nio.ReadOnlyBufferException(); } byteBuffer.limit(_limit * libcore.io.SizeOf.LONG); byteBuffer.position(_position * libcore.io.SizeOf.LONG); byteBuffer.compact(); byteBuffer.clear(); _position = _limit - _position; _limit = _capacity; _mark = UNSET_MARK; return(this); }
/// <summary> /// Constructs a new InputStreamReader on the InputStream /// <code>in</code> /// and /// CharsetDecoder /// <code>dec</code> /// . /// </summary> /// <param name="in">the source InputStream from which to read characters.</param> /// <param name="dec">the CharsetDecoder used by the character conversion.</param> public InputStreamReader(java.io.InputStream @in, java.nio.charset.CharsetDecoder dec) : base(@in) { dec.averageCharsPerByte(); this.@in = @in; decoder = dec; bytes.limit(0); }
public java.nio.ByteBuffer AsByteBuffer() { java.nio.ByteBuffer dup = this.buffer.asReadOnlyBuffer(); dup.position(this.offset); java.nio.ByteBuffer result = dup.slice(); result.limit(this.size); return(result); }
/// <exception cref="System.IO.IOException"/> public int Read(java.nio.ByteBuffer dst) { int size = System.Math.Min(dst.remaining(), this.buf.remaining()); java.nio.ByteBuffer slice = this.buf.slice(); slice.limit(size); dst.buffer = slice.buffer; dst.offset = slice.offset; dst.limit(size); dst.position(size); this.buf.position(this.buf.position() + size); return(size); }
public java.nio.ByteBuffer[] getSegmentsForOutput() { java.nio.ByteBuffer[] result = new java.nio.ByteBuffer[this.segments.Count]; for (int ii = 0; ii < this.segments.Count; ++ii) { Capnproto.SegmentBuilder segment = segments[ii]; segment.buffer.rewind(); java.nio.ByteBuffer slice = segment.buffer.slice(); slice.limit(segment.currentSize() * Capnproto.Constants.BYTES_PER_WORD); slice.order(java.nio.ByteOrder.LITTLE_ENDIAN); result[ii] = slice; } return(result); }
/// <summary>Pretty-print byte buffer in hex</summary> /// <param name="buffer"/> /// <returns/> public static string print(java.nio.ByteBuffer buffer) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); int lim = buffer.limit(); for (int i = 0; i < lim; i++) { sb.Append(string.format("%x", buffer.get(i))); if (i % 8 == 7) { sb.Append(" "); } } return(sb.ToString().Trim()); }
/// <summary> /// Copys the elements of a char buffer to a byte buffer /// Concurency: access must be externally synchronized /// </summary> /// <param name="src"/> /// <param name="dest"/> /// <returns>dest for easy invocation chaining</returns> public static java.nio.ByteBuffer copy(java.nio.CharBuffer src, java.nio.ByteBuffer dest) { int length = src.Length; int i; for (i = 0; i < length; i++) { dest.put(i, unchecked ((byte)src.get(i))); } for (; i < dest.limit(); i++) { dest.put(i, unchecked ((byte)0)); } return(dest); }
/// <exception cref="System.IO.IOException"/> public int Read(java.nio.ByteBuffer dst) { int numBytes = dst.remaining(); if (numBytes < this.buf.remaining()) { //# Serve from the current buffer. java.nio.ByteBuffer slice = this.buf.slice(); slice.limit(numBytes); dst.put(slice); this.buf.position(this.buf.position() + numBytes); return(numBytes); } else { //# Copy current available into destination. int fromFirstBuffer = this.buf.remaining(); { java.nio.ByteBuffer slice = this.buf.slice(); slice.limit(fromFirstBuffer); dst.put(slice); } numBytes -= fromFirstBuffer; if (numBytes <= this.buf.capacity()) { //# Read the next buffer-full. this.buf.clear(); int n = readAtLeast(this.inner, this.buf, numBytes); this.buf.rewind(); java.nio.ByteBuffer slice = this.buf.slice(); slice.limit(numBytes); dst.put(slice); this.buf.limit(n); this.buf.position(numBytes); return(fromFirstBuffer + numBytes); } else { //# Forward large read to the underlying stream. this.buf.clear(); this.buf.limit(0); return(fromFirstBuffer + readAtLeast(this.inner, dst, numBytes)); } } }
/// <exception cref="System.IO.IOException"/> public int Write(java.nio.ByteBuffer src) { int available = this.buf.remaining(); int size = src.remaining(); if (size <= available) { this.buf.put(src); } else if (size <= this.buf.capacity()) { //# Too much for this buffer, but not a full buffer's worth, //# so we'll go ahead and copy. java.nio.ByteBuffer slice = src.slice(); slice.limit(available); this.buf.put(slice); this.buf.rewind(); while (this.buf.hasRemaining()) { this.inner.Write(this.buf); } this.buf.rewind(); src.position(src.position() + available); this.buf.put(src); } else { //# Writing so much data that we might as well write //# directly to avoid a copy. int pos = this.buf.position(); this.buf.rewind(); java.nio.ByteBuffer slice = this.buf.slice(); slice.limit(pos); while (slice.hasRemaining()) { this.inner.Write(slice); } while (src.hasRemaining()) { this.inner.Write(src); } } return(size); }
public virtual java.nio.ByteBuffer put(java.nio.ByteBuffer src) { if (src == this) { throw new System.ArgumentException("src == this"); } int srcByteCount = src.remaining(); if (srcByteCount > remaining()) { throw new java.nio.BufferOverflowException(); } if (src.isDirect()) { throw new System.InvalidOperationException(); } byte[] srcObject = java.nio.NioUtils.unsafeArray(src); int srcOffset = src.position(); if (!src.isDirect()) { srcOffset += java.nio.NioUtils.unsafeArrayOffset(src); } java.nio.ByteBuffer dst = this; if (dst.isDirect()) { throw new System.InvalidOperationException(); } byte[] dstObject = java.nio.NioUtils.unsafeArray(dst); int dstOffset = dst.position(); if (!dst.isDirect()) { dstOffset += java.nio.NioUtils.unsafeArrayOffset(dst); } System.Array.Copy(srcObject, srcOffset, dstObject, dstOffset, srcByteCount); src.position(src.limit()); dst.position(dst.position() + srcByteCount); return(this); }
public static long computeSerializedSizeInWords(Capnproto.MessageBuilder message) { java.nio.ByteBuffer[] segments = message.GetSegmentsForOutput(); //From the capnproto documentation: //"When transmitting over a stream, the following should be sent..." long bytes = 0; //"(4 bytes) The number of segments, minus one..." bytes += 4; //"(N * 4 bytes) The size of each segment, in words." bytes += segments.Length * 4; //"(0 or 4 bytes) Padding up to the next word boundary." if (bytes % 8 != 0) { bytes += 4; } //The content of each segment, in order. for (int i = 0; i < segments.Length; ++i) { java.nio.ByteBuffer s = segments[i]; bytes += s.limit(); } return(bytes / Capnproto.Constants.BYTES_PER_WORD); }
public static void BlockCopy(ByteBuffer src, int srcPos, ByteBuffer dst, int dstPos, int length) { if (src.hasArray() && dst.hasArray()) { java.lang.System.arraycopy(src.array(), src.arrayOffset() + srcPos, dst.array(), dst.arrayOffset() + dstPos, length); } else { if (src.limit() - srcPos < length || dst.limit() - dstPos < length) { throw new java.lang.IndexOutOfBoundsException(); } for (int i = 0; i < length; i++) { // TODO: ByteBuffer.put is polymorphic, and might be slow here dst.put(dstPos++, src.get(srcPos++)); } } }
/// <exception cref="System.IO.IOException"/> public int Read(java.nio.ByteBuffer outBuf) { if (outBuf.buffer == null) { outBuf.buffer = new byte[outBuf.remaining()]; } int len = outBuf.remaining(); if (len == 0) { return(0); } if (len % 8 != 0) { throw new System.Exception("PackedInputStream reads must be word-aligned"); } int outPtr = outBuf.position(); int outEnd = outPtr + len; java.nio.ByteBuffer inBuf = this.inner.GetReadBuffer(); while (true) { byte tag = 0; if (inBuf.remaining() < 10) { if (outBuf.remaining() == 0) { return(len); } if (inBuf.remaining() == 0) { inBuf = this.inner.GetReadBuffer(); continue; } //# We have at least 1, but not 10, bytes available. We need to read //# slowly, doing a bounds check on each byte. tag = inBuf.get(); for (int i = 0; i < 8; ++i) { if ((tag & (1 << i)) != 0) { if (inBuf.remaining() == 0) { inBuf = this.inner.GetReadBuffer(); } outBuf.put(inBuf.get()); } else { outBuf.put((byte)0); } } if (inBuf.remaining() == 0 && (tag == 0 || tag == 0xff)) { inBuf = this.inner.GetReadBuffer(); } } else { tag = inBuf.get(); for (int n = 0; n < 8; ++n) { bool isNonzero = (tag & (1 << n)) != 0; outBuf.put(unchecked ((byte)(inBuf.get() & (isNonzero? -1 : 0)))); inBuf.position(inBuf.position() + (isNonzero? 0 : -1)); } } if (tag == 0) { if (inBuf.remaining() == 0) { throw new System.Exception("Should always have non-empty buffer here."); } int runLength = (unchecked ((int)(0xff)) & (int)inBuf.get()) * 8; if (runLength > outEnd - outPtr) { throw new System.Exception("Packed input did not end cleanly on a segment boundary"); } for (int i = 0; i < runLength; ++i) { outBuf.put((byte)0); } } else if (tag == 0xff) { int runLength = (unchecked ((int)(0xff)) & (int)inBuf.get()) * 8; if (inBuf.remaining() >= runLength) { //# Fast path. java.nio.ByteBuffer slice = inBuf.slice(); slice.limit(runLength); outBuf.put(slice); inBuf.position(inBuf.position() + runLength); } else { //# Copy over the first buffer, then do one big read for the rest. runLength -= inBuf.remaining(); outBuf.put(inBuf); java.nio.ByteBuffer slice = outBuf.slice(); slice.limit(runLength); this.inner.Read(slice); outBuf.position(outBuf.position() + runLength); if (outBuf.remaining() == 0) { return(len); } inBuf = this.inner.GetReadBuffer(); continue; } } if (outBuf.remaining() == 0) { return(len); } } }
/// <exception cref="System.IO.IOException"/> public int Write(java.nio.ByteBuffer inBuf) { int length = inBuf.remaining(); java.nio.ByteBuffer @out = this.inner.GetWriteBuffer(); java.nio.ByteBuffer slowBuffer = java.nio.ByteBuffer.allocate(20); int inPtr = inBuf.position(); int inEnd = inPtr + length; while (inPtr < inEnd) { if (@out.remaining() < 10) { //# Oops, we're out of space. We need at least 10 //# bytes for the fast path, since we don't //# bounds-check on every byte. if (@out == slowBuffer) { int oldLimit = @out.limit(); @out.limit(@out.position()); @out.rewind(); this.inner.Write(@out); @out.limit(oldLimit); } @out = slowBuffer; @out.rewind(); } int tagPos = @out.position(); @out.position(tagPos + 1); byte curByte; curByte = inBuf.get(inPtr); byte bit0 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0); @out.put(curByte); @out.position(@out.position() + bit0 - 1); inPtr += 1; curByte = inBuf.get(inPtr); byte bit1 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0); @out.put(curByte); @out.position(@out.position() + bit1 - 1); inPtr += 1; curByte = inBuf.get(inPtr); byte bit2 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0); @out.put(curByte); @out.position(@out.position() + bit2 - 1); inPtr += 1; curByte = inBuf.get(inPtr); byte bit3 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0); @out.put(curByte); @out.position(@out.position() + bit3 - 1); inPtr += 1; curByte = inBuf.get(inPtr); byte bit4 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0); @out.put(curByte); @out.position(@out.position() + bit4 - 1); inPtr += 1; curByte = inBuf.get(inPtr); byte bit5 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0); @out.put(curByte); @out.position(@out.position() + bit5 - 1); inPtr += 1; curByte = inBuf.get(inPtr); byte bit6 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0); @out.put(curByte); @out.position(@out.position() + bit6 - 1); inPtr += 1; curByte = inBuf.get(inPtr); byte bit7 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0); @out.put(curByte); @out.position(@out.position() + bit7 - 1); inPtr += 1; byte tag = unchecked ((byte)((bit0 << 0) | (bit1 << 1) | (bit2 << 2) | (bit3 << 3) | (bit4 << 4) | (bit5 << 5) | (bit6 << 6) | (bit7 << 7))); @out.put(tagPos, tag); if (tag == 0) { //# An all-zero word is followed by a count of //# consecutive zero words (not including the first //# one). int runStart = inPtr; int limit = inEnd; if (limit - inPtr > 255 * 8) { limit = inPtr + 255 * 8; } while (inPtr < limit && inBuf.getLong(inPtr) == 0) { inPtr += 8; } @out.put(unchecked ((byte)((inPtr - runStart) / 8))); } else if (tag == unchecked ((byte)unchecked ((int)(0xff)))) { //# An all-nonzero word is followed by a count of //# consecutive uncompressed words, followed by the //# uncompressed words themselves. //# Count the number of consecutive words in the input //# which have no more than a single zero-byte. We look //# for at least two zeros because that's the point //# where our compression scheme becomes a net win. int runStart = inPtr; int limit = inEnd; if (limit - inPtr > 255 * 8) { limit = inPtr + 255 * 8; } while (inPtr < limit) { byte c = 0; for (int ii = 0; ii < 8; ++ii) { c += (inBuf.get(inPtr) == 0 ? (byte)1 : (byte)0); inPtr += 1; } if (c >= 2) { //# Un-read the word with multiple zeros, since //# we'll want to compress that one. inPtr -= 8; break; } } int count = inPtr - runStart; @out.put(unchecked ((byte)(count / 8))); if (count <= @out.remaining()) { //# There's enough space to memcpy. inBuf.position(runStart); java.nio.ByteBuffer slice = inBuf.slice(); slice.limit(count); @out.put(slice); } else { //# Input overruns the output buffer. We'll give it //# to the output stream in one chunk and let it //# decide what to do. if (@out == slowBuffer) { int oldLimit = @out.limit(); @out.limit(@out.position()); @out.rewind(); this.inner.Write(@out); @out.limit(oldLimit); } inBuf.position(runStart); java.nio.ByteBuffer slice = inBuf.slice(); slice.limit(count); while (slice.hasRemaining()) { this.inner.Write(slice); } @out = this.inner.GetWriteBuffer(); } } } if (@out == slowBuffer) { @out.limit(@out.position()); @out.rewind(); this.inner.Write(@out); } inBuf.position(inPtr); return(length); }