Example usage for java.nio ByteBuffer position

List of usage examples for java.nio ByteBuffer position

Introduction

In this page you can find the example usage for java.nio ByteBuffer position.

Prototype

public final int position() 

Source Link

Document

Returns the position of this buffer.

Usage

From source file:net.jenet.Command.java

public static Command readCommand(ByteBuffer buffer) {
    int position = buffer.position();
    Command command = null;/*from w w w  .ja va2  s .c om*/

    CommandHeader header = new CommandHeader();
    header.fromBuffer(buffer);
    buffer.position(position);

    switch (header.getCommand()) {
    case 0:
        command = new None();
        break;
    case 1:
        command = new Acknowledge();
        break;
    case 2:
        command = new Connect();
        break;
    case 3:
        command = new VerifyConnect();
        break;
    case 4:
        command = new Disconnect();
        break;
    case 5:
        command = new Ping();
        break;
    case 6:
        command = new SendReliable();
        break;
    case 7:
        command = new SendUnreliable();
        break;
    case 8:
        command = new SendFragment();
        break;
    case 9:
        command = new BandwidthLimit();
        break;
    case 10:
        command = new ThrottleConfigure();
        break;
    case 11:
        command = new SendUnsequenced();
        break;

    default:
        command = null;
        return command;
    }

    command.fromBuffer(buffer);

    return command;
}

From source file:Main.java

public static ByteBuffer copyBinary(final ByteBuffer orig) {
    if (orig == null) {
        return null;
    }//w  ww. ja  va 2  s  .  c  o m
    ByteBuffer copy = ByteBuffer.wrap(new byte[orig.remaining()]);
    if (orig.hasArray()) {
        System.arraycopy(orig.array(), orig.arrayOffset() + orig.position(), copy.array(), 0, orig.remaining());
    } else {
        orig.slice().get(copy.array());
    }

    return copy;
}

From source file:com.glaf.core.util.BinaryUtils.java

/**
 * Returns a copy of the bytes from the given <code>ByteBuffer</code>,
 * ranging from the the buffer's current position to the buffer's limit; or
 * null if the input is null.//from   ww w  . ja va  2s .  c  o  m
 * <p>
 * The internal states of the given byte buffer will be restored when this
 * method completes execution.
 * <p>
 * When handling <code>ByteBuffer</code> from user's input, it's typical to
 * call the {@link #copyBytesFrom(ByteBuffer)} instead of
 * {@link #copyAllBytesFrom(ByteBuffer)} so as to account for the position
 * of the input <code>ByteBuffer</code>. The opposite is typically true,
 * however, when handling <code>ByteBuffer</code> from withint the
 * unmarshallers of the low-level clients.
 */
public static byte[] copyBytesFrom(ByteBuffer bb) {
    if (bb == null)
        return null;
    if (bb.hasArray())
        return Arrays.copyOfRange(bb.array(), bb.position(), bb.limit());
    bb.mark();
    try {
        byte[] dst = new byte[bb.remaining()];
        bb.get(dst);
        return dst;
    } finally {
        bb.reset();
    }
}

From source file:com.oneguy.recognize.Util.java

public static ByteBuffer putData(ByteBuffer buffer, int data) {
    if (buffer == null) {
        return buffer;
    }/*  w  w w  .  ja v  a2  s  .c  o m*/
    // sizeof(int) == 4
    while (buffer.capacity() < buffer.position() + 4 - 1) {
        buffer = doubleSize(buffer);
    }
    buffer.putInt(data);
    return buffer;
}

From source file:com.oneguy.recognize.Util.java

public static ByteBuffer putData(ByteBuffer buffer, short data) {
    if (buffer == null) {
        return buffer;
    }//from  ww  w  .  ja  v  a  2s  .co m
    // sizeof(int) == 2
    while (buffer.capacity() < buffer.position() + 2 - 1) {
        buffer = doubleSize(buffer);
    }
    buffer.putShort(data);
    return buffer;
}

From source file:Main.java

/**
 * Relative <em>get</em> method for reading {@code size} number of bytes from the current
 * position of this buffer./* ww w. j ava 2s . c o  m*/
 * <p>
 * <p>This method reads the next {@code size} bytes at this buffer's current position,
 * returning them as a {@code ByteBuffer} with start set to 0, limit and capacity set to
 * {@code size}, byte order set to this buffer's byte order; and then increments the position by
 * {@code size}.
 */
private static ByteBuffer getByteBuffer(final ByteBuffer source, final int size)
        throws BufferUnderflowException {
    if (size < 0) {
        throw new IllegalArgumentException("size: " + size);
    }
    final int originalLimit = source.limit();
    final int position = source.position();
    final int limit = position + size;
    if ((limit < position) || (limit > originalLimit)) {
        throw new BufferUnderflowException();
    }
    source.limit(limit);
    try {
        final ByteBuffer result = source.slice();
        result.order(source.order());
        source.position(limit);
        return result;
    } finally {
        source.limit(originalLimit);
    }
}

From source file:com.oneguy.recognize.Util.java

public static ByteBuffer putData(ByteBuffer buffer, byte[] data) {
    if (buffer == null || data == null || data.length == 0) {
        return buffer;
    }/* w  w w  .  j  av  a  2  s .  c om*/
    while (buffer.capacity() < buffer.position() + data.length - 1) {
        buffer = doubleSize(buffer);
    }
    buffer.put(data);
    return buffer;
}

From source file:com.l2jfree.util.HexUtil.java

public static String printData(ByteBuffer buf, int offset, int len) {
    byte[] tmp = new byte[len];
    int pos = buf.position();
    buf.position(offset);/*from w  ww .  java  2 s. c  om*/
    buf.get(tmp);
    buf.position(pos);
    return printData(tmp, len);
}

From source file:com.liveramp.commons.util.BytesUtils.java

public static byte[] deepCopyByteBufferToByteArray(ByteBuffer byteBuffer) {
    byte[] result = new byte[byteBuffer.remaining()];
    System.arraycopy(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), result, 0,
            byteBuffer.remaining());//from   ww  w .j a v a  2 s  . co  m
    return result;
}

From source file:com.healthmarketscience.jackcess.impl.CustomToStringStyle.java

private static void appendDetail(StringBuffer buffer, ByteBuffer bb) {
    int len = bb.remaining();
    buffer.append("(").append(len).append(") ");
    buffer.append(ByteUtil.toHexString(bb, bb.position(), Math.min(len, MAX_BYTE_DETAIL_LEN)));
    if (len > MAX_BYTE_DETAIL_LEN) {
        buffer.append(" ...");
    }// w w  w.  j  av  a 2  s  .  c  o  m
}