public ByteBuffer flip() { buf.flip(); return this; }
private ByteBuffer createDelimiterBuf(final String delimiter) { ByteBuffer tmp = ByteBuffer.allocate(delimiter.length()); try { tmp.putString(delimiter, charset.newEncoder()); tmp.flip(); return tmp; } catch (CharacterCodingException e) { LOG.error("Bad charset?", e); return null; } }
public void write(final ByteBuffer src) { m_log.debug("Writing data to input stream..."); m_rawBytesReceived += src.remaining(); m_log.debug("Received raw bytes: {}", m_rawBytesReceived); synchronized (m_mutex) { if (m_closed) { m_log.debug("InputStream closed..."); return; } if (m_buf.hasRemaining()) { m_log.debug("Copying buffer data..."); this.m_buf.compact(); this.m_buf.put(src); this.m_buf.flip(); m_mutex.notifyAll(); } else { m_log.debug("Nothing remaining in buffer..."); this.m_buf.clear(); this.m_buf.put(src); this.m_buf.flip(); m_mutex.notifyAll(); } } }
@Override public void write(int b) throws IOException { ByteBuffer buf = ByteBuffer.allocate(1); buf.put((byte) b); buf.flip(); write(buf); }
@Override public void write(final int b) throws IOException { final ByteBuffer buf = ByteBuffer.allocate(1); buf.put((byte) b); buf.flip(); write(buf); } }
public void mergeAll() { int sum = 0; final int size = bufferQueue.size(); if (size < 2) { // no need to merge! return; } // Get the size of merged BB for (Object o : bufferQueue) { sum += ((ByteBuffer) o).remaining(); } // Allocate a new BB that will contain all fragments ByteBuffer newBuf = ByteBuffer.allocate(sum); // and merge all. for (;;) { ByteBuffer buf = bufferQueue.poll(); if (buf == null) { break; } newBuf.put(buf); buf.release(); } // Push the new buffer finally. newBuf.flip(); bufferQueue.offer(newBuf); }
public ByteBuffer encode(final SipMessage message) { final ByteBuffer buffer = ByteBuffer.allocate(300); buffer.setAutoExpand(true); final SipMessageVisitor visitor = new EncoderVisitor(buffer); message.accept(visitor); buffer.flip(); return buffer; }
/** * Combines the remaining data from the given <code>Collection</code> of * <code>ByteBuffer</code>s into a single consolidated * <code>ByteBuffer</code>. * * @param buffers The <code>Collection</code> of <code>ByteBuffer</code>s * to make into a single buffer. * @return A new <code>ByteBuffer</code> combining the remaining data of * the <code>Collection</code> of <code>ByteBuffer</code>s. */ public static ByteBuffer combine(final Collection<ByteBuffer> buffers) { final ByteBuffer buf = ByteBuffer.allocate(remaining(buffers)); for (final ByteBuffer curBuf : buffers) { buf.put(curBuf); } buf.flip(); return buf; }
private static ByteBuffer createBuffer(final ByteBuffer buffer) { // We calculate this here because the final split buffer will not // necessarily have a size equal to the chunk size -- it will // usually be smaller. final ByteBuffer data = ByteBuffer.allocate( buffer.limit() - buffer.position()); data.put(buffer); data.flip(); return data; }
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { if (!(message instanceof Serializable)) { throw new NotSerializableException(); } ByteBuffer buf = ByteBuffer.allocate(64); buf.setAutoExpand(true); buf.putObject(message); int objectSize = buf.position() - 4; if (objectSize > maxObjectSize) { buf.release(); throw new IllegalArgumentException( "The encoded object is too big: " + objectSize + " (> " + maxObjectSize + ')'); } buf.flip(); out.write(buf); } }
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER); if (encoder == null) { encoder = charset.newEncoder(); session.setAttribute(ENCODER, encoder); } String value = message.toString(); ByteBuffer buf = ByteBuffer.allocate(value.length()) .setAutoExpand(true); buf.putString(value, encoder); if (buf.position() > maxLineLength) { throw new IllegalArgumentException("Line length: " + buf.position()); } buf.putString(delimiter.getValue(), encoder); buf.flip(); out.write(buf); }
/** * Encodes the TCP frame into a {@link ByteBuffer}. * * @param frame The frame to encode. * @return The encoded frame in a {@link ByteBuffer}. */ public ByteBuffer encode(final TcpFrame frame) { final int length = frame.getLength(); final ByteBuffer buf = ByteBuffer.allocate(2 + length); MinaUtils.putUnsignedShort(buf, length); buf.put(frame.getData()); buf.flip(); //m_log.debug("Encoded TCP Frame as buffer: {}", buf); return buf; }
buf.flip(); m_log.debug("Encoded STUN message as buf: {}", buf); return buf;
private void readSession(DatagramSessionImpl session) { ByteBuffer readBuf = ByteBuffer.allocate(session.getReadBufferSize()); try { int readBytes = session.getChannel().read(readBuf.buf()); if (readBytes > 0) { readBuf.flip(); ByteBuffer newBuf = ByteBuffer.allocate(readBuf.limit()); newBuf.put(readBuf); newBuf.flip(); session.increaseReadBytes(readBytes); session.getFilterChain().fireMessageReceived(session, newBuf); } } catch (IOException e) { session.getFilterChain().fireExceptionCaught(session, e); } finally { readBuf.release(); } }
private void readSession(DatagramChannel channel, RegistrationRequest req) throws Exception { ByteBuffer readBuf = ByteBuffer .allocate(((DatagramSessionConfig) req.config .getSessionConfig()).getReceiveBufferSize()); try { SocketAddress remoteAddress = channel.receive(readBuf.buf()); if (remoteAddress != null) { DatagramSessionImpl session = (DatagramSessionImpl) newSession( remoteAddress, req.address); readBuf.flip(); ByteBuffer newBuf = ByteBuffer.allocate(readBuf.limit()); newBuf.put(readBuf); newBuf.flip(); session.increaseReadBytes(newBuf.remaining()); session.getFilterChain().fireMessageReceived(session, newBuf); } } finally { readBuf.release(); } }
private static UUID createTransactionId() { final UUID id = UUID.randomUUID(); final byte[] idBytes = id.getRawBytes(); final ByteBuffer idBuf = ByteBuffer.wrap(idBytes); // Lower the limit to make room for the magic cookie. idBuf.limit(idBytes.length - 4); final ByteBuffer newIdBuf = ByteBuffer.allocate(16); MinaUtils.putUnsignedInt(newIdBuf, MAGIC_COOKIE); newIdBuf.put(idBuf); newIdBuf.flip(); return new UUID(MinaUtils.toByteArray(newIdBuf)); }
ByteBuffer wb = ByteBuffer.allocate(rb.remaining()); wb.put(rb); wb.flip(); rb.reset(); messageCopy = wb;
final ByteBuffer product = this.m_buffer; this.m_buffer = null; return finishDecode(product.flip(), out);