/** * {@inheritDoc} */ @Override public int indexOf(byte b) { if (hasArray()) { int arrayOffset = arrayOffset(); int beginPos = arrayOffset + position(); int limit = arrayOffset + limit(); byte[] array = array(); for (int i = beginPos; i < limit; i++) { if (array[i] == b) { return i - arrayOffset; } } } else { int beginPos = position(); int limit = limit(); for (int i = beginPos; i < limit; i++) { if (get(i) == b) { return i; } } } return -1; }
/** * {@inheritDoc} */ @Override public final IoBuffer asReadOnlyBuffer() { recapacityAllowed = false; return asReadOnlyBuffer0(); }
/** * {@inheritDoc} */ @Override public final IoBuffer limit(int newLimit) { autoExpand(newLimit, 0); buf().limit(newLimit); if (mark > newLimit) { mark = -1; } return this; }
/** * {@inheritDoc} */ @Override public final IoBuffer sweep() { clear(); return fillAndReset(remaining()); }
/** * {@inheritDoc} */ @Override public IoBuffer skip(int size) { autoExpand(size); return position(position() + size); }
if (newCapacity > capacity()) { int pos = position(); int limit = limit(); ByteOrder bo = order(); ByteBuffer oldBuf = buf(); ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); oldBuf.clear(); newBuf.put(oldBuf); buf(newBuf); buf().limit(limit); if (mark >= 0) { buf().position(mark); buf().mark(); buf().position(pos); buf().order(bo);
int remaining = remaining(); int capacity = capacity(); if (isAutoShrink() && remaining <= capacity >>> 2 && capacity > minimumCapacity) { int newCapacity = capacity; int minCapacity = Math.max(minimumCapacity, remaining << 1); ByteOrder bo = order(); ByteBuffer oldBuf = buf(); ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); newBuf.put(oldBuf); buf(newBuf); buf().order(bo); } else { buf().compact();
public String getString(int fieldSize, CharsetDecoder decoder) throws CharacterCodingException { checkFieldSize(fieldSize); if (!hasRemaining()) { return ""; int oldPos = position(); int oldLimit = limit(); int end = oldPos + fieldSize; if (get(i) == 0) { break; limit(end); } else { limit(i); if (get(i) == 0 && get(i + 1) == 0) { break; limit(end); } else { limit(i); if (!hasRemaining()) { limit(oldLimit); position(end); return "";
public IoBuffer putString(CharSequence val, int fieldSize, CharsetEncoder encoder) throws CharacterCodingException { checkFieldSize(fieldSize); autoExpand(fieldSize); int oldLimit = limit(); int end = position() + fieldSize; put((byte) 0x00); } else { put((byte) 0x00); put((byte) 0x00); position(end); return this; limit(end); encoder.reset(); CoderResult cr; if (in.hasRemaining()) { cr = encoder.encode(in, buf(), true); } else { cr = encoder.flush(buf()); limit(oldLimit); if (position() < end) {
/** * {@inheritDoc} */ @Override public String toString() { StringBuilder buf = new StringBuilder(); if (isDirect()) { buf.append("DirectBuffer"); } else { buf.append("HeapBuffer"); } buf.append("[pos="); buf.append(position()); buf.append(" lim="); buf.append(limit()); buf.append(" cap="); buf.append(capacity()); buf.append(": "); buf.append(getHexDump(16)); buf.append(']'); return buf.toString(); }
/** * {@inheritDoc} */ @Override public final IoBuffer getSlice(int index, int length) { if (length < 0) { throw new IllegalArgumentException("length: " + length); } int pos = position(); int limit = limit(); if (index > limit) { throw new IllegalArgumentException("index: " + index); } int endIndex = index + length; if (endIndex > limit) { throw new IndexOutOfBoundsException("index + length (" + endIndex + ") is greater " + "than limit (" + limit + ")."); } clear(); limit(endIndex); position(index); IoBuffer slice = slice(); limit(limit); position(pos); return slice; }
if (!prefixedDataAvailable(4)) { throw new BufferUnderflowException(); int length = getInt(); if (length <= 4) { throw new BufferDataException("Object length should be greater than 4: " + length); int oldLimit = limit(); limit(position() + length); try (ObjectInputStream in = new ObjectInputStream(asInputStream()) { @Override protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { throw new BufferDataException(e); } finally { limit(oldLimit);
int oldPos = position(); skip(4); // Make a room for the length field. try (ObjectOutputStream out = new ObjectOutputStream(asOutputStream()) { @Override protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException { int newPos = position(); position(oldPos); putInt(newPos - oldPos - 4); position(newPos); return this;
private IoBuffer expand(int pos, int expectedRemaining, boolean autoExpand) { if (!recapacityAllowed) { throw new IllegalStateException("Derived buffers and their parent can't be expanded."); } int end = pos + expectedRemaining; int newCapacity; if (autoExpand) { newCapacity = IoBuffer.normalizeCapacity(end); } else { newCapacity = end; } if (newCapacity > capacity()) { // The buffer needs expansion. capacity(newCapacity); } if (end > limit()) { // We call limit() directly to prevent StackOverflowError buf().limit(end); } return this; }
/** * {@inheritDoc} */ @Override public final boolean isDirect() { return buf().isDirect(); }
if (newCapacity > capacity()) { int pos = position(); int limit = limit(); ByteOrder bo = order(); ByteBuffer oldBuf = buf(); ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); oldBuf.clear(); newBuf.put(oldBuf); buf(newBuf); buf().limit(limit); if (mark >= 0) { buf().position(mark); buf().mark(); buf().position(pos); buf().order(bo);
int remaining = remaining(); int capacity = capacity(); if (isAutoShrink() && remaining <= capacity >>> 2 && capacity > minimumCapacity) { int newCapacity = capacity; int minCapacity = Math.max(minimumCapacity, remaining << 1); ByteOrder bo = order(); ByteBuffer oldBuf = buf(); ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity, isDirect()); newBuf.put(oldBuf); buf(newBuf); buf().order(bo); } else { buf().compact();
checkFieldSize(fieldSize); if (!hasRemaining()) { return ""; int oldPos = position(); int oldLimit = limit(); int end = oldPos + fieldSize; if (get(i) == 0) { break; limit(end); } else { limit(i); if (get(i) == 0 && get(i + 1) == 0) { break; limit(end); } else { limit(i); if (!hasRemaining()) { limit(oldLimit); position(end); return ""; int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1;
checkFieldSize(fieldSize); autoExpand(fieldSize); int oldLimit = limit(); int end = position() + fieldSize; put((byte) 0x00); } else { put((byte) 0x00); put((byte) 0x00); position(end); return this; limit(end); encoder.reset(); CoderResult cr; if (in.hasRemaining()) { cr = encoder.encode(in, buf(), true); } else { cr = encoder.flush(buf()); limit(oldLimit); if (position() < end) { if (!utf16) { put((byte) 0x00);
/** * {@inheritDoc} */ @Override public String toString() { StringBuilder buf = new StringBuilder(); if (isDirect()) { buf.append("DirectBuffer"); } else { buf.append("HeapBuffer"); } buf.append("[pos="); buf.append(position()); buf.append(" lim="); buf.append(limit()); buf.append(" cap="); buf.append(capacity()); buf.append(": "); buf.append(getHexDump(16)); buf.append(']'); return buf.toString(); }