public ByteBuffer allocate(final int size) throws IllegalArgumentException { return Buffers.slice(buffer, size); } };
/** * Copy at most {@code count} bytes from {@code source} into {@code destination}. * * @param count the maximum number of bytes to copy * @param destination the destination buffer * @param source the source buffer * @return the number of bytes put into the destination buffer */ public static int copy(int count, final ByteBuffer destination, final ByteBuffer source) { int cnt = count >= 0? Math.min(Math.min(count, source.remaining()), destination.remaining()): Math.max(Math.max(count, - source.remaining()), - destination.remaining()); final ByteBuffer copy = slice(source, cnt); destination.put(copy); return copy.position(); // cnt could be negative, so it is safer to return copy.position() instead of cnt }
/** * Copy as many bytes as possible from {@code source} into {@code destination}. * * @param destination the destination buffer * @param source the source buffer * @return the number of bytes put into the destination buffer */ public static int copy(final ByteBuffer destination, final ByteBuffer source) { final int sr = source.remaining(); final int dr = destination.remaining(); if (dr >= sr) { destination.put(source); return sr; } else { destination.put(slice(source, dr)); return dr; } }
/** * Copy as many bytes as possible from {@code sources} into {@code destinations} in a "scatter" fashion. * * @param destinations the destination buffers * @param offset the offset into the destination buffers array * @param length the number of buffers to update * @param source the source buffer * @return the number of bytes put into the destination buffers */ public static int copy(final ByteBuffer[] destinations, final int offset, final int length, final ByteBuffer source) { int t = 0; for (int i = 0; i < length; i ++) { final ByteBuffer buffer = destinations[i + offset]; final int rem = buffer.remaining(); if (rem == 0) { continue; } else if (rem < source.remaining()) { buffer.put(slice(source, rem)); t += rem; } else { t += source.remaining(); buffer.put(source); return t; } } return t; }
/** * Copy as many bytes as possible from {@code sources} into {@code destination} in a "gather" fashion. * * @param destination the destination buffer * @param sources the source buffers * @param offset the offset into the source buffers array * @param length the number of buffers to read from * @return the number of bytes put into the destination buffers */ public static int copy(final ByteBuffer destination, final ByteBuffer[] sources, final int offset, final int length) { int t = 0; for (int i = 0; i < length; i ++) { final ByteBuffer buffer = sources[i + offset]; final int rem = buffer.remaining(); if (rem == 0) { continue; } else if (rem > destination.remaining()) { t += destination.remaining(); destination.put(slice(buffer, destination.remaining())); return t; } else { destination.put(buffer); t += rem; } } return t; }
t += sr; } else if (sr > dr) { dest.put(slice(source, dr)); d++; t += dr;
count -= (long)sr; } else if (sr > dr) { dest.put(slice(source, dr)); d++; t += dr;
ByteBuffer createBuffer() { synchronized (this) { // avoid a storm of mass-population by only allowing one thread to split a parent buffer at a time ByteBuffer appearing = getMasterQueue().poll(); if (appearing != null) { return appearing; } ByteBuffer parentBuffer = parent.allocate(); final int size = getSize(); ByteBuffer result = Buffers.slice(parentBuffer, size); while (parentBuffer.hasRemaining()) { // avoid false sharing between buffers if (size < CACHE_LINE_SIZE) { Buffers.skip(parentBuffer, CACHE_LINE_SIZE - size); } super.doFree(Buffers.slice(parentBuffer, size)); } return result; } } };
Buffers.copy(buffer, Buffers.slice(receiveBuffer, length)); } else { log.tracef("Not copying message from %s into full buffer %s", receiveBuffer, buffer);
Buffers.copy(buffers, offs, len, Buffers.slice(receiveBuffer, length)); } else { log.tracef("Not copying message from %s into multiple full buffers", receiveBuffer);
final byte type = receiveBuffer.get(); final int len = receiveBuffer.get() & 0xff; final ByteBuffer data = Buffers.slice(receiveBuffer, len); switch (type) { case Protocol.CAP_VERSION: {
final byte type = receiveBuffer.get(); final int len = receiveBuffer.get() & 0xff; final ByteBuffer data = Buffers.slice(receiveBuffer, len); switch (type) { case Protocol.CAP_VERSION: {
final byte type = receiveBuffer.get(); final int len = receiveBuffer.get() & 0xff; final ByteBuffer data = Buffers.slice(receiveBuffer, len); switch (type) { case Protocol.GRT_SERVER_NAME: {
public ByteBuffer allocate(final int size) throws IllegalArgumentException { return Buffers.slice(buffer, size); } };
/** * Copy as many bytes as possible from {@code source} into {@code destination}. * * @param destination the destination buffer * @param source the source buffer * @return the number of bytes put into the destination buffer */ public static int copy(final ByteBuffer destination, final ByteBuffer source) { final int sr = source.remaining(); final int dr = destination.remaining(); if (dr >= sr) { destination.put(source); return sr; } else { destination.put(slice(source, dr)); return dr; } }
/** * Copy at most {@code count} bytes from {@code source} into {@code destination}. * * @param count the maximum number of bytes to copy * @param destination the destination buffer * @param source the source buffer * @return the number of bytes put into the destination buffer */ public static int copy(int count, final ByteBuffer destination, final ByteBuffer source) { int cnt = count >= 0? Math.min(Math.min(count, source.remaining()), destination.remaining()): Math.max(Math.max(count, - source.remaining()), - destination.remaining()); final ByteBuffer copy = slice(source, cnt); destination.put(copy); return copy.position(); // cnt could be negative, so it is safer to return copy.position() instead of cnt }
/** * Copy as many bytes as possible from {@code sources} into {@code destinations} in a "scatter" fashion. * * @param destinations the destination buffers * @param offset the offset into the destination buffers array * @param length the number of buffers to update * @param source the source buffer * @return the number of bytes put into the destination buffers */ public static int copy(final ByteBuffer[] destinations, final int offset, final int length, final ByteBuffer source) { int t = 0; for (int i = 0; i < length; i ++) { final ByteBuffer buffer = destinations[i + offset]; final int rem = buffer.remaining(); if (rem == 0) { continue; } else if (rem < source.remaining()) { buffer.put(slice(source, rem)); t += rem; } else { t += source.remaining(); buffer.put(source); return t; } } return t; }
/** * Copy as many bytes as possible from {@code sources} into {@code destination} in a "gather" fashion. * * @param destination the destination buffer * @param sources the source buffers * @param offset the offset into the source buffers array * @param length the number of buffers to read from * @return the number of bytes put into the destination buffers */ public static int copy(final ByteBuffer destination, final ByteBuffer[] sources, final int offset, final int length) { int t = 0; for (int i = 0; i < length; i ++) { final ByteBuffer buffer = sources[i + offset]; final int rem = buffer.remaining(); if (rem == 0) { continue; } else if (rem > destination.remaining()) { t += destination.remaining(); destination.put(slice(buffer, destination.remaining())); return t; } else { destination.put(buffer); t += rem; } } return t; }
t += sr; } else if (sr > dr) { dest.put(slice(source, dr)); d++; t += dr;
ByteBuffer createBuffer() { synchronized (this) { // avoid a storm of mass-population by only allowing one thread to split a parent buffer at a time ByteBuffer appearing = getMasterQueue().poll(); if (appearing != null) { return appearing; } ByteBuffer parentBuffer = parent.allocate(); final int size = getSize(); ByteBuffer result = Buffers.slice(parentBuffer, size); while (parentBuffer.hasRemaining()) { // avoid false sharing between buffers if (size < CACHE_LINE_SIZE) { Buffers.skip(parentBuffer, CACHE_LINE_SIZE - size); } super.doFree(Buffers.slice(parentBuffer, size)); } return result; } } };