public boolean send(final ByteBuffer[] srcs, final int offs, final int len) throws IOException { if (len == 1) { return send(srcs[offs]); } else if (! Buffers.hasRemaining(srcs, offs, len)) { return false; } final ByteBuffer transmitBuffer = this.transmitBuffer.getResource(); final long remaining = Buffers.remaining(srcs, offs, len); final boolean longLengths = this.longLengths; final int lengthFieldSize = longLengths ? 4 : 2; if (remaining > transmitBuffer.capacity() - lengthFieldSize || ! longLengths && remaining > 65535) { throw msg.txMsgTooLarge(); } if (transmitBuffer.remaining() < lengthFieldSize + remaining && ! writeBuffer()) { return false; } if (longLengths) { transmitBuffer.putInt((int) remaining); } else { transmitBuffer.putShort((short) remaining); } Buffers.copy(transmitBuffer, srcs, offs, len); writeBuffer(); return true; }
/** * Take all of the remaining bytes from the buffers and return them in an array. * * @param buffers the buffer to read * @param offs the offset into the array * @param len the number of buffers * @return the bytes */ public static byte[] take(final ByteBuffer[] buffers, final int offs, final int len) { if (len == 1) return take(buffers[offs]); final long remaining = Buffers.remaining(buffers, offs, len); if (remaining == 0L) return NO_BYTES; if (remaining > Integer.MAX_VALUE) throw new OutOfMemoryError("Array too large"); final byte[] bytes = new byte[(int) remaining]; int o = 0; int rem; ByteBuffer buffer; for (int i = 0; i < len; i ++) { buffer = buffers[i + offs]; rem = buffer.remaining(); buffer.get(bytes, o, rem); o += rem; } return bytes; }
/** * Add a random amount of random data to the target buffer. * * @param target the target buffer * @param random the RNG */ public static void addRandom(ByteBuffer target, Random random) { if (target.remaining() == 0) { return; } addRandom(target, random, random.nextInt(target.remaining())); }
public boolean sendTo(final SocketAddress target, final ByteBuffer[] buffers, final int offset, final int length) throws IOException { if (length == 0) { return false; } if (length == 1) { return sendTo(target, buffers[offset]); } final long o = Buffers.remaining(buffers, offset, length); if (o > 65535L) { // there will never be enough room throw log.bufferTooLarge(); } final ByteBuffer buffer = ByteBuffer.allocate((int) o); Buffers.copy(buffer, buffers, offset, length); buffer.flip(); return datagramChannel.send(buffer, target) != 0; }
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; } } };
final int length = receiveBuffer.getInt(); if (length < 0 || length > receiveBuffer.capacity() - 4) { Buffers.unget(receiveBuffer, 4); throw msg.recvInvalidMsgLength(length); receiveBuffer.clear(); } else { Buffers.unget(receiveBuffer, 4); receiveBuffer.compact(); if (Buffers.hasRemaining(buffers)) { log.tracef("Copying message from %s into multiple buffers", receiveBuffer); Buffers.copy(buffers, offs, len, Buffers.slice(receiveBuffer, length)); } else { log.tracef("Not copying message from %s into multiple full buffers", receiveBuffer); Buffers.skip(receiveBuffer, length);
private Frame(FrameCallBack callback, ByteBuffer[] data, int offs, int len) { this.callback = callback; this.data = data; this.offs = offs; this.len = len; this.remaining = Buffers.remaining(data, offs, len); } }
/** * Copy unwrapped data from {@code unwrappedBuffer} into {@code dsts}. * * @param dsts destine of copy * @param offset offset * @param length length * @param unwrappedBuffer source from where byte will be copied * @return the amount of copied bytes */ private int copyUnwrappedData(final ByteBuffer[] dsts, final int offset, final int length, ByteBuffer unwrappedBuffer) { assert Thread.holdsLock(getUnwrapLock()); unwrappedBuffer.flip(); try { return Buffers.copy(dsts, offset, length, unwrappedBuffer); } finally { unwrappedBuffer.compact(); } }
final byte type = receiveBuffer.get(); final int len = receiveBuffer.get() & 0xff; final ByteBuffer data = Buffers.slice(receiveBuffer, len); switch (type) { case Protocol.CAP_VERSION: { remoteEndpointName = Buffers.getModifiedUtf8(data); server.tracef("Server received capability: remote endpoint name \"%s\"", remoteEndpointName); break; final String remoteVersionString = Buffers.getModifiedUtf8(data); server.tracef("Server received capability: remote version is \"%s\"", remoteVersionString); break;
static String readString(ByteBuffer buffer) { int length = buffer.get() & 0xff; return new String(take(buffer, length), StandardCharsets.UTF_8); }
/** * Determine whether any of the buffers has remaining data. * * @param buffers the buffers * @return {@code true} if any of the selected buffers has remaining data */ public static boolean hasRemaining(final Buffer[] buffers) { return hasRemaining(buffers, 0, buffers.length); }
public String toString() { StringBuilder b = new StringBuilder(); try { dump(buffer, b, indent, columns); } catch (IOException e) { // ignore, not possible! } return b.toString(); } };
static int readUnsignedShort(final ByteBuffer buffer) { int length = buffer.get() & 0xff; switch (length) { case 0: return 0; case 1: return buffer.get() & 0xff; case 2: return buffer.getShort() & 0xffff; default: Buffers.skip(buffer, length - 2); return buffer.getShort() & 0xffff; } }
/** * Construct a new instance. * * @param channel the channel to wrap * @param receiveBuffer the receive buffer (should be direct) * @param transmitBuffer the send buffer (should be direct) */ public FramedMessageChannel(final ConnectedStreamChannel channel, final ByteBuffer receiveBuffer, final ByteBuffer transmitBuffer) { super(channel); this.receiveBuffer = Buffers.pooledWrapper(receiveBuffer); this.transmitBuffer = Buffers.pooledWrapper(transmitBuffer); log.tracef("Created new framed message channel around %s, receive buffer %s, transmit buffer %s", channel, receiveBuffer, transmitBuffer); }
public ByteBuffer allocate(final int size) throws IllegalArgumentException { return Buffers.slice(buffer, size); } };
mechName = Buffers.getModifiedUtf8(receiveBuffer); } else { mechName = ProtocolUtils.readString(receiveBuffer);
final int length = receiveBuffer.getInt(); if (length < 0 || length > receiveBuffer.capacity() - 4) { Buffers.unget(receiveBuffer, 4); throw msg.recvInvalidMsgLength(length); receiveBuffer.clear(); } else { Buffers.unget(receiveBuffer, 4); receiveBuffer.compact(); Buffers.copy(buffer, Buffers.slice(receiveBuffer, length)); } else { log.tracef("Not copying message from %s into full buffer %s", receiveBuffer, buffer); Buffers.skip(receiveBuffer, length);
/** * Get the total remaining size of all the given buffers. * * @param buffers the buffers * @return the number of remaining elements */ public static long remaining(final Buffer[] buffers) { return remaining(buffers, 0, buffers.length); }
private void handleUpdate(final ByteBuffer origSrc) { LimitedBufferSlicePool.PooledByteBuffer[] pooled = cacheEntry.buffers(); ByteBuffer[] buffers = new ByteBuffer[pooled.length]; for (int i = 0; i < buffers.length; i++) { buffers[i] = pooled[i].getBuffer(); } written += Buffers.copy(buffers, 0, buffers.length, origSrc); if (written == length) { for (ByteBuffer buffer : buffers) { //prepare buffers for reading buffer.flip(); } cacheEntry.enable(); } }
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 String mechName = Buffers.getModifiedUtf8(data); client.tracef("Client received capability: SASL mechanism %s", mechName); offeredMechs.add(mechName); remoteEndpointName = Buffers.getModifiedUtf8(data); client.tracef("Client received capability: remote endpoint name \"%s\"", remoteEndpointName); break; final String remoteVersionString = Buffers.getModifiedUtf8(data); client.tracef("Client received capability: remote version is \"%s\"", remoteVersionString); break;