/** * 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 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); } }
public static ByteBuffer mergeBuffers(ByteBuffer... payload) { int size = (int) Buffers.remaining(payload); if (size == 0) { return Buffers.EMPTY_BYTE_BUFFER; } ByteBuffer buffer = ByteBuffer.allocate(size); for (ByteBuffer buf : payload) { buffer.put(buf); } buffer.flip(); return buffer; } }
/** * Queues a frame for sending. * * @param callback * @param data */ protected void queueFrame(FrameCallBack callback, ByteBuffer... data) { queuedData += Buffers.remaining(data); bufferCount += data.length; frameQueue.add(new Frame(callback, data, 0, data.length)); }
/** * Get a debug-friendly description of the buffer. * * @param buffers the buffers to describe * @param offs the offset into the array * @param len the number of buffers * @return the string */ public static String debugString(ByteBuffer[] buffers, int offs, int len) { StringBuilder b = new StringBuilder(); b.append(len).append(" buffer(s)"); if (len > 0) { b.append(" of ").append(Buffers.remaining(buffers, offs, len)).append(" bytes"); } return b.toString(); }
public UTF8Output(ByteBuffer... payload) { stringBuilder = new StringBuilder((int) Buffers.remaining(payload)); write(payload); }
public long write(final ByteBuffer[] srcs, final int offs, final int len) throws IOException { final long remaining = Buffers.remaining(srcs, offs, len); return next.send(srcs, offs, len) ? remaining : 0L; }
/** * 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; }
public long write(final ByteBuffer[] srcs, final int offs, final int len) throws IOException { if (allAreSet(state, WRITE_FLAG_SHUTDOWN)) { throw new ClosedChannelException(); } if (allAreClear(state, FLAG_TLS)) { return sinkConduit.write(srcs, offs, len); } else { final long r1 = Buffers.remaining(srcs, offs, len); performIO(IO_GOAL_WRITE, srcs, offs, len, NO_BUFFERS, 0, 0); return (r1 - Buffers.remaining(srcs, offs, len)); } }
public long writeFinal(final ByteBuffer[] srcs, final int offs, final int len) throws IOException { if (allAreSet(state, WRITE_FLAG_SHUTDOWN)) { throw new ClosedChannelException(); } if (allAreClear(state, FLAG_TLS)) { return sinkConduit.writeFinal(srcs, offs, len); } else { final long r1 = Buffers.remaining(srcs, offs, len); performIO(IO_GOAL_WRITE_FINAL, srcs, offs, len, NO_BUFFERS, 0, 0); return (r1 - Buffers.remaining(srcs, offs, len)); } }
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 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 void handleEvent(final StreamSinkChannel streamSinkChannel) { try { long toWrite = Buffers.remaining(buffer); long written = 0; while (written < toWrite) { long res = streamSinkChannel.write(buffer, 0, buffer.length); written += res; if (res == 0) { return; } } streamSinkChannel.suspendWrites(); invokeOnComplete(); } catch (IOException e) { streamSinkChannel.suspendWrites(); invokeOnException(callback, e); } } };
public long write(final ByteBuffer[] srcs, final int offset, final int length) throws IOException { if (length == 0) { return 0L; } int oldVal = state; int state = oldVal & MASK_STATE; try { if (state != 0) { long rem = Buffers.remaining(srcs, offset, length); state = processWrite(state, srcs, offset, length); long ret = rem - Buffers.remaining(srcs, offset, length); if (state != 0) { return ret; } if (allAreSet(oldVal, FLAG_SHUTDOWN)) { next.terminateWrites(); throw new ClosedChannelException(); } //we don't attempt to write again return ret; } return length == 1 ? next.write(srcs[offset]) : next.write(srcs, offset, length); } catch (IOException | RuntimeException | Error e) { IoUtils.safeClose(connection); throw e; } finally { this.state = oldVal & ~MASK_STATE | state; } }
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 void send(final ByteBuffer[] buffer, final IoCallback callback) { if (inCall) { queue(buffer, callback); return; } else { long responseContentLength = exchange.getResponseContentLength(); if(responseContentLength > 0 && Buffers.remaining(buffer) > responseContentLength) { callback.onException(exchange, this, UndertowLogger.ROOT_LOGGER.dataLargerThanContentLength(Buffers.remaining(buffer), responseContentLength)); return; } if (!exchange.isResponseStarted() && callback == IoCallback.END_EXCHANGE) { if (responseContentLength == -1 && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) { exchange.setResponseContentLength(Buffers.remaining(buffer)); } } } if (!writeBuffer(buffer, callback)) { return; } invokeOnComplete(callback); }
@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); } }
int old = data.getBuffer().limit(); try { long count = Buffers.remaining(dsts, offset, length); if (count < data.getBuffer().remaining()) { data.getBuffer().limit((int) (data.getBuffer().position() + count));
/** {@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; } }