/** Appends a byte to a buffer * @param to Buffer is flush mode * @param b byte to append */ public static void append(ByteBuffer to, byte b) { int pos = flipToFill(to); try { to.put(b); } finally { flipToFlush(to, pos); } }
/** Append bytes to a buffer. * @param to Buffer is flush mode * @param b bytes to append * @param off offset into byte * @param len length to append * @throws BufferOverflowException if unable to append buffer due to space limits */ public static void append(ByteBuffer to, byte[] b, int off, int len) throws BufferOverflowException { int pos = flipToFill(to); try { to.put(b, off, len); } finally { flipToFlush(to, pos); } }
/** * Like append, but does not throw {@link BufferOverflowException} * @param to Buffer The buffer to fill to. The buffer will be flipped to fill mode and then flipped back to flush mode. * @param b bytes The bytes to fill * @param off offset into bytes * @param len length to fill * @return the number of bytes taken from the buffer. */ public static int fill(ByteBuffer to, byte[] b, int off, int len) { int pos = flipToFill(to); try { int remaining = to.remaining(); int take = remaining < len ? remaining : len; to.put(b, off, take); return take; } finally { flipToFlush(to, pos); } }
/** Appends a buffer to a buffer * @param to Buffer is flush mode * @param b buffer to append * @return The position of the valid data before the flipped position. */ public static int append(ByteBuffer to, ByteBuffer b) { int pos = flipToFill(to); try { return put(b, to); } finally { flipToFlush(to, pos); } }
public static ByteBuffer toBuffer(Resource resource,boolean direct) throws IOException { int len=(int)resource.length(); if (len<0) throw new IllegalArgumentException("invalid resource: "+String.valueOf(resource)+" len="+len); ByteBuffer buffer = direct?BufferUtil.allocateDirect(len):BufferUtil.allocate(len); int pos=BufferUtil.flipToFill(buffer); if (resource.getFile()!=null) BufferUtil.readFrom(resource.getFile(),buffer); else { try (InputStream is = resource.getInputStream();) { BufferUtil.readFrom(is,len,buffer); } } BufferUtil.flipToFlush(buffer,pos); return buffer; }
public void transferTo(ByteBuffer buffer) { if (buffer.remaining() < length) { throw new IllegalArgumentException(String.format("Not enough space in ByteBuffer remaining [%d] for accumulated buffers length [%d]", buffer.remaining(),length)); } int position = buffer.position(); for (byte[] chunk : chunks) { buffer.put(chunk,0,chunk.length); } BufferUtil.flipToFlush(buffer,position); } }
/** */ public static void append(ByteBuffer to, byte[] b,int off,int len) { int pos= flipToFill(to); try { to.put(b,off,len); } finally { flipToFlush(to,pos); } }
/** */ public static void append(ByteBuffer to, byte[] b,int off,int len) { int pos= flipToFill(to); try { to.put(b,off,len); } finally { flipToFlush(to,pos); } }
/** Appends a byte to a buffer * @param to Buffer is flush mode * @param b byte to append */ public static void append(ByteBuffer to, byte b) { int pos = flipToFill(to); try { to.put(b); } finally { flipToFlush(to, pos); } }
public int generatePing(ByteBufferPool.Lease lease, byte[] payload, boolean reply) { if (payload.length != PingFrame.PING_LENGTH) throw new IllegalArgumentException("Invalid payload length: " + payload.length); ByteBuffer header = generateHeader(lease, FrameType.PING, PingFrame.PING_LENGTH, reply ? Flags.ACK : Flags.NONE, 0); header.put(payload); BufferUtil.flipToFlush(header, 0); lease.append(header, true); return Frame.HEADER_LENGTH + PingFrame.PING_LENGTH; } }
public int generateWindowUpdate(ByteBufferPool.Lease lease, int streamId, int windowUpdate) { if (windowUpdate < 0) throw new IllegalArgumentException("Invalid window update: " + windowUpdate); ByteBuffer header = generateHeader(lease, FrameType.WINDOW_UPDATE, WindowUpdateFrame.WINDOW_UPDATE_LENGTH, Flags.NONE, streamId); header.putInt(windowUpdate); BufferUtil.flipToFlush(header, 0); lease.append(header, true); return Frame.HEADER_LENGTH + WindowUpdateFrame.WINDOW_UPDATE_LENGTH; } }
public int generateReset(ByteBufferPool.Lease lease, int streamId, int error) { if (streamId < 0) throw new IllegalArgumentException("Invalid stream id: " + streamId); ByteBuffer header = generateHeader(lease, FrameType.RST_STREAM, ResetFrame.RESET_LENGTH, Flags.NONE, streamId); header.putInt(error); BufferUtil.flipToFlush(header, 0); lease.append(header, true); return Frame.HEADER_LENGTH + ResetFrame.RESET_LENGTH; } }
public int generateReset(ByteBufferPool.Lease lease, int streamId, int error) { if (streamId < 0) throw new IllegalArgumentException("Invalid stream id: " + streamId); ByteBuffer header = generateHeader(lease, FrameType.RST_STREAM, ResetFrame.RESET_LENGTH, Flags.NONE, streamId); header.putInt(error); BufferUtil.flipToFlush(header, 0); lease.append(header, true); return Frame.HEADER_LENGTH + ResetFrame.RESET_LENGTH; } }
public int generateWindowUpdate(ByteBufferPool.Lease lease, int streamId, int windowUpdate) { if (windowUpdate < 0) throw new IllegalArgumentException("Invalid window update: " + windowUpdate); ByteBuffer header = generateHeader(lease, FrameType.WINDOW_UPDATE, WindowUpdateFrame.WINDOW_UPDATE_LENGTH, Flags.NONE, streamId); header.putInt(windowUpdate); BufferUtil.flipToFlush(header, 0); lease.append(header, true); return Frame.HEADER_LENGTH + WindowUpdateFrame.WINDOW_UPDATE_LENGTH; } }
public int generatePing(ByteBufferPool.Lease lease, byte[] payload, boolean reply) { if (payload.length != PingFrame.PING_LENGTH) throw new IllegalArgumentException("Invalid payload length: " + payload.length); ByteBuffer header = generateHeader(lease, FrameType.PING, PingFrame.PING_LENGTH, reply ? Flags.ACK : Flags.NONE, 0); header.put(payload); BufferUtil.flipToFlush(header, 0); lease.append(header, true); return Frame.HEADER_LENGTH + PingFrame.PING_LENGTH; } }
private void generateFrame(ByteBufferPool.Lease lease, int streamId, ByteBuffer data, boolean last) { int length = data.remaining(); int flags = Flags.NONE; if (last) flags |= Flags.END_STREAM; ByteBuffer header = headerGenerator.generate(lease, FrameType.DATA, Frame.HEADER_LENGTH + length, length, flags, streamId); BufferUtil.flipToFlush(header, 0); lease.append(header, true); // Skip empty data buffers. if (data.remaining() > 0) lease.append(data, false); } }
private void generateFrame(ByteBufferPool.Lease lease, int streamId, ByteBuffer data, boolean last) { int length = data.remaining(); int flags = Flags.NONE; if (last) flags |= Flags.END_STREAM; ByteBuffer header = headerGenerator.generate(lease, FrameType.DATA, Frame.HEADER_LENGTH + length, length, flags, streamId); BufferUtil.flipToFlush(header, 0); lease.append(header, true); // Skip empty data buffers. if (data.remaining() > 0) lease.append(data, false); } }
/** Appends a buffer to a buffer * @param to Buffer is flush mode * @param b buffer to append */ public static int append(ByteBuffer to, ByteBuffer b) { int pos = flipToFill(to); try { return put(b, to); } finally { flipToFlush(to, pos); } }
public int generatePriority(ByteBufferPool.Lease lease, int streamId, int parentStreamId, int weight, boolean exclusive) { ByteBuffer header = generateHeader(lease, FrameType.PRIORITY, PriorityFrame.PRIORITY_LENGTH, Flags.NONE, streamId); generatePriorityBody(header, streamId, parentStreamId, weight, exclusive); BufferUtil.flipToFlush(header, 0); lease.append(header, true); return Frame.HEADER_LENGTH + PriorityFrame.PRIORITY_LENGTH; }
public int generatePriority(ByteBufferPool.Lease lease, int streamId, int parentStreamId, int weight, boolean exclusive) { ByteBuffer header = generateHeader(lease, FrameType.PRIORITY, PriorityFrame.PRIORITY_LENGTH, Flags.NONE, streamId); generatePriorityBody(header, streamId, parentStreamId, weight, exclusive); BufferUtil.flipToFlush(header, 0); lease.append(header, true); return Frame.HEADER_LENGTH + PriorityFrame.PRIORITY_LENGTH; }