/** 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 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); } }
/** 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); } }
/** 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 */ 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); } }
/** 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); } }
/** * Put data from one buffer into another, avoiding over/under flows * @param from Buffer to take bytes from in flush mode * @param to Buffer to put bytes to in flush mode. The buffer is flipToFill before the put and flipToFlush after. * @return number of bytes moved */ public static int flipPutFlip(ByteBuffer from, ByteBuffer to) { int pos= flipToFill(to); try { return put(from,to); } finally { flipToFlush(to,pos); } }
/** * Put data from one buffer into another, avoiding over/under flows * @param from Buffer to take bytes from in flush mode * @param to Buffer to put bytes to in flush mode. The buffer is flipToFill before the put and flipToFlush after. * @return number of bytes moved */ public static int flipPutFlip(ByteBuffer from, ByteBuffer to) { int pos= flipToFill(to); try { return put(from,to); } 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 MessageOutputStream(OutgoingFrames outgoing, int bufferSize, ByteBufferPool bufferPool) { this.outgoing = outgoing; this.bufferPool = bufferPool; this.blocker = new BlockingWriteCallback(); this.buffer = bufferPool.acquire(bufferSize, true); BufferUtil.flipToFill(buffer); this.frame = new BinaryFrame(); }
public MessageWriter(OutgoingFrames outgoing, int bufferSize, ByteBufferPool bufferPool) { this.outgoing = outgoing; this.bufferPool = bufferPool; this.blocker = new BlockingWriteCallback(); this.buffer = bufferPool.acquire(bufferSize, true); BufferUtil.flipToFill(buffer); this.frame = new TextFrame(); this.utf = Utf8CharBuffer.wrap(buffer); }
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; }
protected void forwardIncoming(Frame frame, ByteAccumulator accumulator) { DataFrame newFrame = new DataFrame(frame); // Unset RSV1 since it's not compressed anymore. newFrame.setRsv1(false); ByteBuffer buffer = getBufferPool().acquire(accumulator.getLength(),false); try { BufferUtil.flipToFill(buffer); accumulator.transferTo(buffer); newFrame.setPayload(buffer); nextIncomingFrame(newFrame); } finally { getBufferPool().release(buffer); } }
public <C> void output(C context, Callback<C> callback, long channelId, WebSocketFrame frame) throws IOException { ByteBuffer muxPayload = bufferPool.acquire(frame.getPayloadLength() + DATA_FRAME_OVERHEAD,false); BufferUtil.flipToFill(muxPayload); // start building mux payload writeChannelId(muxPayload,channelId); byte b = (byte)(frame.isFin()?0x80:0x00); // fin b |= (byte)(frame.isRsv1()?0x40:0x00); // rsv1 b |= (byte)(frame.isRsv2()?0x20:0x00); // rsv2 b |= (byte)(frame.isRsv3()?0x10:0x00); // rsv3 b |= (byte)(frame.getOpCode() & 0x0F); // opcode muxPayload.put(b); BufferUtil.put(frame.getPayload(),muxPayload); // build muxed frame WebSocketFrame muxFrame = WebSocketFrame.binary(); BufferUtil.flipToFlush(muxPayload,0); muxFrame.setPayload(muxPayload); // NOTE: the physical connection will handle masking rules for this frame. // release original buffer (no longer needed) bufferPool.release(frame.getPayload()); // send muxed frame down to the physical connection. outgoing.output(context,callback,muxFrame); }