/** * 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(); } }
/** * Copy at most {@code count} bytes from {@code sources} into {@code destinations} in a "scatter" fashion. * * @param count the maximum number of bytes to copy * @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(int count, final ByteBuffer[] destinations, final int offset, final int length, final ByteBuffer source) { if (source.remaining() > count) { final int oldLimit = source.limit(); if (count < 0) { // count from end (count is NEGATIVE) throw msg.copyNegative(); } else { try { source.limit(source.position() + count); return copy(destinations, offset, length, source); } finally { source.limit(oldLimit); } } } else { return copy(destinations, offset, length, source); } }
/** * Copy at most {@code count} bytes from {@code sources} into {@code destination} in a "gather" fashion. * * @param count the maximum number of bytes to copy * @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(int count, final ByteBuffer destination, final ByteBuffer[] sources, final int offset, final int length) { if (destination.remaining() > count) { if (count < 0) { // count from end (count is NEGATIVE) throw msg.copyNegative(); } else { final int oldLimit = destination.limit(); try { destination.limit(destination.position() + Math.min(count, destination.remaining())); return copy(destination, sources, offset, length); } finally { destination.limit(oldLimit); } } } else { return copy(destination, sources, offset, length); } }
public int read(final ByteBuffer dst) throws IOException { try { final ByteBuffer buffer = pooledBuffer.getResource(); final int lim = buffer.limit(); final int pos = buffer.position(); final int rem = lim - pos; if (rem > 0) { return Buffers.copy(dst, buffer); } else { final int dstRem = dst.remaining(); buffer.clear(); try { final long rres = next.read(new ByteBuffer[] { dst, buffer }, 0, 2); if (rres == -1L) { return -1; } } finally { buffer.flip(); } return dst.remaining() - dstRem; } } catch (IllegalStateException ignored) { return -1; } }
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(); } }
@Override public int write(final ByteBuffer src) throws IOException { ByteBuffer origSrc = src.duplicate(); int totalWritten = super.write(src); if(totalWritten > 0) { LimitedBufferSlicePool.PooledByteBuffer[] pooled = cacheEntry.buffers(); ByteBuffer[] buffers = new ByteBuffer[pooled.length]; for (int i = 0; i < buffers.length; i++) { buffers[i] = pooled[i].getBuffer(); } origSrc.limit(origSrc.position() + totalWritten); written += Buffers.copy(buffers, 0, buffers.length, origSrc); if (written == length) { for (ByteBuffer buffer : buffers) { //prepare buffers for reading buffer.flip(); } cacheEntry.enable(); } } return totalWritten; }
public int read(final ByteBuffer dst) throws IOException { int cnt; if (! dst.hasRemaining()) { return 0; } final StreamSourceConduit next = this.next; try { final ByteBuffer src = pooledBuffer.getResource(); cnt = Buffers.copy(dst, src); if (src.hasRemaining()) { return cnt; } current = next; pooledBuffer.free(); if (cnt > 0 && next == PushBackStreamSourceConduit.this.next) { // don't hit the main channel until the user wants to return cnt; } } catch (IllegalStateException ignored) { current = next; cnt = 0; } final int res = next.read(dst); return res > 0 ? res + cnt : cnt > 0 ? cnt : res; }
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; }
public int read(final ByteBuffer dst) throws IOException { int cnt; if (! dst.hasRemaining()) { return 0; } try { final ByteBuffer src = buffer.getResource(); cnt = Buffers.copy(dst, src); if (src.hasRemaining()) { return cnt; } final StreamSourceChannel next = channel = this.next; buffer.free(); if (cnt > 0 && next == firstChannel) { // don't hit the main channel until the user wants to return cnt; } } catch (IllegalStateException ignored) { channel = next; cnt = 0; } final int res = next.read(dst); return res > 0 ? res + cnt : cnt > 0 ? cnt : res; }
public long read(final ByteBuffer[] dsts, final int offset, final int length) throws IOException { long cnt; try { final ByteBuffer src = buffer.getResource(); cnt = Buffers.copy(dsts, offset, length, src); if (src.hasRemaining()) { return cnt; } final StreamSourceChannel next = channel = this.next; buffer.free(); if (cnt > 0L && next == firstChannel) { // don't hit the main channel until the user wants to return cnt; } } catch (IllegalStateException ignored) { channel = next; cnt = 0; } final long res = next.read(dsts, offset, length); return res > 0 ? res + cnt : cnt > 0 ? cnt : res; }
public long read(final ByteBuffer[] dsts, final int offs, final int len) throws IOException { long cnt; final StreamSourceConduit next = this.next; try { final ByteBuffer src = pooledBuffer.getResource(); cnt = Buffers.copy(dsts, offs, len, src); if (src.hasRemaining()) { return cnt; } current = next; pooledBuffer.free(); if (cnt > 0L && next == PushBackStreamSourceConduit.this.next) { // don't hit the main channel until the user wants to return cnt; } } catch (IllegalStateException ignored) { current = next; cnt = 0; } final long res = next.read(dsts, offs, len); return res > 0 ? res + cnt : cnt > 0 ? cnt : res; }
public long receiveFrom(final SocketAddressBuffer addressBuffer, final ByteBuffer[] buffers, final int offs, final int len) throws IOException { if (len == 0) { return 0L; } if (len == 1) { return receiveFrom(addressBuffer, buffers[offs]); } final int o = (int) Math.min(Buffers.remaining(buffers, offs, len), 65536L); final ByteBuffer buffer = ByteBuffer.allocate(o); final SocketAddress sourceAddress; try { sourceAddress = datagramChannel.receive(buffer); } catch (ClosedChannelException e) { return -1L; } if (sourceAddress == null) { return 0L; } else { final int t = o - buffer.remaining(); buffer.flip(); Buffers.copy(buffers, offs, len, buffer); if (addressBuffer != null) { addressBuffer.setSourceAddress(sourceAddress); addressBuffer.setDestinationAddress(null); } return t; } }
@Override public long read(final ByteBuffer[] dsts, final int offs, final int len) throws IOException { PooledByteBuffer eb = connection.getExtraBytes(); if (eb != null) { final ByteBuffer buffer = eb.getBuffer(); int result = Buffers.copy(dsts, offs, len, buffer); if (!buffer.hasRemaining()) { eb.close(); connection.setExtraBytes(null); } return result; } else { return super.read(dsts, offs, len); } }
@Override public int read(final ByteBuffer dst) throws IOException { PooledByteBuffer eb = connection.getExtraBytes(); if (eb != null) { final ByteBuffer buffer = eb.getBuffer(); int result = Buffers.copy(dst, buffer); if (!buffer.hasRemaining()) { eb.close(); connection.setExtraBytes(null); } return result; } else { return super.read(dst); } }
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; }
@Override public long write(ByteBuffer[] srcs, int offset, int length) throws IOException { if(!safeToSend()) { return 0; } if(writeBuffer == null) { writeBuffer = getChannel().getBufferPool().allocate(); } ByteBuffer buffer = writeBuffer.getBuffer(); int copied = Buffers.copy(buffer, srcs, offset, length); if(!buffer.hasRemaining()) { handleBufferFull(); } return copied; }
@Override public int write(ByteBuffer src) throws IOException { if(!safeToSend()) { return 0; } if(writeBuffer == null) { writeBuffer = getChannel().getBufferPool().allocate(); } ByteBuffer buffer = writeBuffer.getBuffer(); int copied = Buffers.copy(buffer, src); if(!buffer.hasRemaining()) { handleBufferFull(); } return copied; }
@Override public int read(final byte[] b, final int off, final int len) throws IOException { if(Thread.currentThread() == channel.getIoThread()) { throw UndertowMessages.MESSAGES.blockingIoFromIOThread(); } if (anyAreSet(state, FLAG_CLOSED)) { throw UndertowMessages.MESSAGES.streamIsClosed(); } readIntoBuffer(); if (anyAreSet(state, FLAG_FINISHED)) { return -1; } if (len == 0) { return 0; } ByteBuffer buffer = pooled.getBuffer(); int copied = Buffers.copy(ByteBuffer.wrap(b, off, len), buffer); if (!buffer.hasRemaining()) { pooled.close(); pooled = null; } return copied; }
@Override public long write(ByteBuffer[] srcs, int offset, int length) throws IOException { if (anyAreSet(state, SHUTDOWN)) { throw new ClosedChannelException(); } if (anyAreSet(state, FLUSHING)) { boolean res = flushBuffer(); if (!res) { return 0; } } PooledByteBuffer pooled = this.buffer; if (pooled == null) { this.buffer = pooled = pool.allocate(); } final ByteBuffer buffer = pooled.getBuffer(); long total = Buffers.remaining(srcs, offset, length); if (buffer.remaining() > total) { long put = total; Buffers.copy(buffer, srcs, offset, length); return put; } else { return flushBufferWithUserData(srcs, offset, length); } }
/** {@inheritDoc} */ public boolean send(final ByteBuffer[] buffers, final int offs, final int len) throws IOException { synchronized (writeLock) { if (isWriteShutDown()) { throw msg.writeShutDown(); } if (!Buffers.hasRemaining(buffers, offs, len)) { return true; } final ByteBuffer transmitBuffer = this.transmitBuffer.getResource(); final long remaining = Buffers.remaining(buffers, offs, len); if (remaining > transmitBuffer.capacity() - 4L) { throw msg.txMsgTooLarge(); } log.tracef("Accepting multiple buffers into %s", transmitBuffer); if (transmitBuffer.remaining() < 4 + remaining && ! doFlushBuffer()) { log.tracef("Insufficient room to accept multiple buffers into %s", transmitBuffer); return false; } transmitBuffer.putInt((int) remaining); Buffers.copy(transmitBuffer, buffers, offs, len); log.tracef("Accepted a message into %s", transmitBuffer); return true; } }