public ByteBuffer position(int newPosition) { buf.position(newPosition); return this; }
public int position() { return buf.position(); }
/** * Changes the capacity and limit of this buffer so this buffer get * the specified <tt>expectedRemaining</tt> room from the current position. * This method works even if you didn't set <tt>autoExpand</tt> to * <tt>true</tt>. */ public ByteBuffer expand(int expectedRemaining) { return expand(position(), expectedRemaining); }
/** * @see java.nio.Buffer#remaining() */ public int remaining() { return limit() - position(); }
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; }
@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; }
@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 int compareTo(ByteBuffer that) { int n = this.position() + Math.min(this.remaining(), that.remaining()); for (int i = this.position(), j = that.position(); i < n; i++, j++) { byte v1 = this.get(i); byte v2 = that.get(j); if (v1 == v2) { continue; } if (v1 < v2) { return -1; } return +1; } return this.remaining() - that.remaining(); }
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(); }
private void discard(ByteBuffer in) { if (Integer.MAX_VALUE - in.remaining() < overflowPosition) { overflowPosition = Integer.MAX_VALUE; } else { overflowPosition += in.remaining(); } in.position(in.limit()); } }
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); }
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 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); } }
@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; }