public ByteBuffer limit(int newLimit) { buf.limit(newLimit); return this; }
public int limit() { return buf.limit(); }
/** * @see java.nio.Buffer#remaining() */ public int remaining() { return limit() - position(); }
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 String toString() { StringBuffer buf = new StringBuffer(); if (isDirect()) { buf.append("DirectBuffer"); } else { buf.append("HeapBuffer"); } buf.append("[pos="); buf.append(position()); buf.append(" lim="); buf.append(limit()); buf.append(" cap="); buf.append(capacity()); buf.append(": "); buf.append(getHexDump()); buf.append(']'); return buf.toString(); }
public IoSessionInputStream() { buf = ByteBuffer.allocate(16); buf.setAutoExpand(true); buf.limit(0); }
@Override public int hashCode() { int h = 1; int p = position(); for (int i = limit() - 1; i >= p; i--) { h = 31 * h + get(i); } return h; }
/** * Useful for debugging. Turns the given buffer into an ASCII string. * This does not affect the position or the limit of the buffer (it resets * them to their original values when done). * * @param buf The buffer to convert to a string. * @return The string. */ public static String toAsciiString(final ByteBuffer buf) { DECODER.reset(); final int position = buf.position(); final int limit = buf.limit(); try { return buf.getString(DECODER); } catch (final CharacterCodingException e) { LOG.error("Could not decode: "+buf, e); return StringUtils.EMPTY; } finally { buf.position(position); buf.limit(limit); } }
/** * Returns the buffer as a string while preserving the buffer position * and limit. * * @param buffer The buffer to create a string from. * @return The buffer string. */ public static String toString(final ByteBuffer buffer) { final int position = buffer.position(); final int limit = buffer.limit(); final byte[] data = new byte[buffer.remaining()]; buffer.get(data); final String dataString = new String(data); buffer.position(position); buffer.limit(limit); return dataString; } }
private void discard(ByteBuffer in) { if (Integer.MAX_VALUE - in.remaining() < overflowPosition) { overflowPosition = Integer.MAX_VALUE; } else { overflowPosition += in.remaining(); } in.position(in.limit()); } }
@Override public boolean equals(Object o) { if (!(o instanceof ByteBuffer)) { return false; } ByteBuffer that = (ByteBuffer) o; if (this.remaining() != that.remaining()) { return false; } int p = this.position(); for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) { byte v1 = this.get(i); byte v2 = that.get(j); if (v1 != v2) { return false; } } return true; }
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; }
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 DecodingState decode(ByteBuffer in, ProtocolDecoderOutput out) throws Exception { final int beginPos = in.position(); final int limit = in.limit(); for (int i = beginPos; i < limit; i++) { final byte b = in.get(i); if (b != m_byteToSkip) { in.position(i); return finishDecode(); } else { } } in.position(limit); return this; }
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)); }
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(); } }