/** * Reads one unsigned byte as a short integer. */ public short getUnsigned() { return (short) (get() & 0xff); }
/** * Reads one byte as an unsigned short integer. */ public short getUnsigned(int index) { return (short) (get(index) & 0xff); }
/** * @see java.nio.ByteBuffer#get(byte[]) */ public ByteBuffer get(byte[] dst) { return get(dst, 0, dst.length); }
public ByteBuffer get(byte[] dst) { buf.get(dst); return this; }
public ByteBuffer get(byte[] dst, int offset, int length) { buf.get(dst, offset, length); return this; }
public byte get() { return buf.get(); }
public byte get(int index) { return buf.get(index); }
public int read() throws IOException { synchronized (m_mutex) { if (!waitForData()) { return -1; } return m_buf.get() & 0xff; } }
@Override public int read() { if (ByteBuffer.this.hasRemaining()) { return ByteBuffer.this.get() & 0xff; } else { return -1; } }
/** * Copies the specified buffer to a byte array. * * @param buf The buffer to copy. * @return The byte array. */ public static byte[] toByteArray(final ByteBuffer buf) { final byte[] bytes = new byte[buf.remaining()]; buf.get(bytes); return bytes; }
public int read() throws IOException { synchronized (mutex) { if (!waitForData()) { return -1; } return buf.get() & 0xff; } }
@Override public int read(byte[] b, int off, int len) { int remaining = ByteBuffer.this.remaining(); if (remaining > 0) { int readBytes = Math.min(remaining, len); ByteBuffer.this.get(b, off, readBytes); return readBytes; } else { return -1; } }
@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; }
public static String getHexdump(ByteBuffer in) { int size = in.remaining(); if (size == 0) { return "empty"; } StringBuffer out = new StringBuffer((in.remaining() * 3) - 1); int mark = in.position(); // fill the first int byteValue = in.get() & 0xFF; out.append((char) highDigits[byteValue]); out.append((char) lowDigits[byteValue]); size--; // and the others, too for (; size > 0; size--) { out.append(' '); byteValue = in.get() & 0xFF; out.append((char) highDigits[byteValue]); out.append((char) lowDigits[byteValue]); } in.position(mark); return out.toString(); } }
private void debugStateTransition2(final DecodingState returningState, final ByteBuffer in) { final String stateString; if (returningState != null) { stateString = ClassUtils.getShortClassName(returningState.getClass()); } else { stateString = null; } LOG.debug(ClassUtils.getShortClassName(getClass()) + " transitioning to state: {}", stateString); LOG.debug("Remaining bytes: "+in.remaining()); if (in.remaining() == 1) { LOG.debug("Remaining byte: '"+in.get()+"'"); } } }
@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 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 int read(byte[] b, int off, int len) throws IOException { synchronized (mutex) { if (!waitForData()) { return -1; } int readBytes; if (len > buf.remaining()) { readBytes = buf.remaining(); } else { readBytes = len; } buf.get(b, off, readBytes); return readBytes; } }
/** * 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; } }
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; }