/** * Returns the direct or heap buffer which is capable of the specified * size. This method tries to allocate direct buffer first, and then * tries heap buffer if direct buffer memory is exhausted. Please use * {@link #allocate(int, boolean)} to allocate buffers of specific type. * * @param capacity the capacity of the buffer */ public static ByteBuffer allocate(int capacity) { if (useDirectBuffers) { try { // first try to allocate direct buffer return allocate(capacity, true); } catch (OutOfMemoryError e) { // fall through to heap buffer } } return allocate(capacity, false); }
private Context() { decoder = charset.newDecoder(); buf = ByteBuffer.allocate(80).setAutoExpand(true); }
private Context() { decoder = charset.newDecoder(); buf = ByteBuffer.allocate(80).setAutoExpand(true); }
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 IoSessionInputStream() { buf = ByteBuffer.allocate(16); buf.setAutoExpand(true); buf.limit(0); }
public IoSessionInputStream(final IoSession ioSession, final int readTimeout) { m_ioSession = ioSession; m_readTimeout = readTimeout; m_buf = ByteBuffer.allocate(16); m_buf.setAutoExpand(true); m_buf.limit(0); }
@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 Object readObject() throws ClassNotFoundException, IOException { int objectSize = in.readInt(); if (objectSize <= 0) { throw new StreamCorruptedException("Invalid objectSize: " + objectSize); } if (objectSize > maxObjectSize) { throw new StreamCorruptedException("ObjectSize too big: " + objectSize + " (expected: <= " + maxObjectSize + ')'); } ByteBuffer buf = ByteBuffer.allocate(objectSize + 4, false); buf.putInt(objectSize); in.readFully(buf.array(), 4, objectSize); buf.position(0); buf.limit(objectSize + 4); Object answer = buf.getObject(classLoader); buf.release(); return answer; }
private static ByteBuffer createBuffer(final ByteBuffer buffer) { final ByteBuffer data = ByteBuffer.allocate( buffer.limit() - buffer.position()); LOG.trace("Created buffer with capacity: "+data.capacity()); data.put(buffer); data.rewind(); return data; }
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 writeObject(Object obj) throws IOException { ByteBuffer buf = ByteBuffer.allocate(64, false); buf.setAutoExpand(true); buf.putObject(obj); int objectSize = buf.position() - 4; if (objectSize > maxObjectSize) { buf.release(); throw new IllegalArgumentException( "The encoded object is too big: " + objectSize + " (> " + maxObjectSize + ')'); } out.write(buf.array(), 0, buf.position()); buf.release(); }
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; }
private void storeRemainingInSession(ByteBuffer buf, IoSession session) { ByteBuffer remainingBuf = ByteBuffer.allocate(buf.capacity()); remainingBuf.setAutoExpand(true); remainingBuf.order(buf.order()); remainingBuf.put(buf); session.setAttribute(BUFFER, remainingBuf); } }
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 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)); }