Example usage for java.nio ByteBuffer reset

List of usage examples for java.nio ByteBuffer reset

Introduction

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

Prototype

public final Buffer reset() 

Source Link

Document

Resets the position of this buffer to the mark.

Usage

From source file:MainClass.java

public static void main(String argv[]) {
    ByteBuffer bb = ByteBuffer.allocate(100);

    bb.mark();//from www . j  a va2  s  . co  m
    bb.position(5);
    bb.reset();

    bb.mark().position(5).reset();

    char[] myBuffer = new char[100];

    CharBuffer cb = CharBuffer.wrap(myBuffer);
    cb.position(12).limit(21);

    CharBuffer sliced = cb.slice();

    System.out.println("Sliced: offset=" + sliced.arrayOffset() + ", capacity=" + sliced.capacity());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ByteBuffer bbuf = ByteBuffer.allocate(10);
    int capacity = bbuf.capacity(); // 10
    System.out.println(capacity);
    bbuf.putShort(2, (short) 123);
    System.out.println(Arrays.toString(bbuf.array()));
    bbuf.mark();//w w w.j a v  a2s .co m
    bbuf.reset();
    System.out.println(Arrays.toString(bbuf.array()));
}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(8);

    System.out.println("Capacity: " + bb.capacity());
    System.out.println("Limit: " + bb.limit());
    System.out.println("Position: " + bb.position());

    // The mark is not set for a new buffer. Calling the
    // reset() method throws a runtime exception if the mark is not set.
    try {//ww w . j  ava 2  s. com
        bb.reset();
        System.out.println("Mark: " + bb.position());
    } catch (InvalidMarkException e) {
        System.out.println("Mark is not  set");
    }
}

From source file:Main.java

public static ByteBuffer copyByteBuffer(ByteBuffer buf) {
    ByteBuffer dest = newByteBuffer(buf.remaining());
    buf.mark();//from w ww  .  j av a 2s  . c  om
    dest.put(buf);
    buf.reset();
    dest.rewind();
    return dest;
}

From source file:org.apache.hadoop.util.NativeJerasure.java

private static ByteBuffer directify(byte[] readBufs, int dataStart, int dataLen) {
    ByteBuffer newBuf = null;
    newBuf = ByteBuffer.allocateDirect(dataLen);
    newBuf.position(0);/*from  w  w w . j  a v a  2  s.c o  m*/
    newBuf.mark();
    newBuf.put(readBufs, dataStart, dataLen);
    newBuf.reset();
    newBuf.limit(dataLen);
    return newBuf;
}

From source file:com.offbynull.portmapper.common.ByteBufferUtils.java

/**
 * Copy the remaining content of a {@link ByteBuffer} in to a new array.
 * @param src buffer to copy/*from w ww  .ja v a2 s  .c  om*/
 * @param incrementSrc of {@code true} increments {@code src}'s position
 * @return new buffer with the remaining content in {@code src}
 * @throws NullPointerException if any arguments are {@code null}
 */
public static byte[] copyContentsToArray(ByteBuffer src, boolean incrementSrc) {
    Validate.notNull(src);
    if (!incrementSrc) {
        src.mark();
    }

    ByteBuffer dst = ByteBuffer.allocate(src.remaining());
    dst.put(src);

    if (!incrementSrc) {
        src.reset();
    }

    return dst.array();
}

From source file:org.apache.hadoop.util.NativeStair.java

private static ByteBuffer directify(byte[] readBufs, int dataStart, int dataLen) {
    //LOG.info("directify starts: " + System.nanoTime());
    ByteBuffer newBuf = null;
    newBuf = ByteBuffer.allocateDirect(dataLen);
    newBuf.position(0);//  www.j  av a2 s. c o  m
    newBuf.mark();
    newBuf.put(readBufs, dataStart, dataLen);
    newBuf.reset();
    newBuf.limit(dataLen);
    //LOG.info("directify ends: " + System.nanoTime());
    return newBuf;
}

From source file:com.offbynull.portmapper.common.ByteBufferUtils.java

/**
 * Copy the remaining content of a {@link ByteBuffer} in to a new non-direct {@link ByteBuffer}.
 * @param src buffer to copy//from w  w  w. j  a  v a  2  s. c  om
 * @param incrementSrc of {@code true} increments {@code src}'s position
 * @param incrementDst of {@code true} increments {@code dst}'s position
 * @return new buffer with the remaining content in {@code src}
 * @throws NullPointerException if any arguments are {@code null}
 */
public static ByteBuffer copyContents(ByteBuffer src, boolean incrementSrc, boolean incrementDst) {
    Validate.notNull(src);
    if (!incrementSrc) {
        src.mark();
    }

    ByteBuffer dst = ByteBuffer.allocate(src.remaining());
    dst.put(src);

    if (!incrementSrc) {
        src.reset();
    }

    if (!incrementDst) {
        dst.flip();
    }

    return dst;
}

From source file:com.nridge.core.base.std.BufUtl.java

/**
 * Retrieves the operation code stored within the header of the
 * <code>ByteBuffer</code> object.
 *
 * @param aBuffer Packet byte buffer object.
 * @return Application specific operation code value.
 *//*from  ww  w  . jav a  2s  .  c om*/
public static int getOpCode(ByteBuffer aBuffer) {
    int opCode;

    if (aBuffer == null)
        return -1;
    else {
        aBuffer.mark();
        aBuffer.position(0);
        opCode = aBuffer.getInt();
        aBuffer.reset();

        return opCode;
    }
}

From source file:com.nridge.core.base.std.BufUtl.java

/**
 * Resets the <code>ByteBuffer</code> position and adds the opcode and
 * version values to it./*from  w  w  w  .  ja  va 2  s  .  c  o m*/
 *
 * @param aBuffer Packet byte buffer object.
 * @param anOpCode Application specific operation code value.
 * @param aVersion Application specific operation version code value.
 */
public static void setHeader(ByteBuffer aBuffer, int anOpCode, int aVersion) {
    if (aBuffer != null) {
        aBuffer.mark();
        aBuffer.position(0);
        aBuffer.putInt(anOpCode);
        aBuffer.putInt(aVersion);
        aBuffer.reset();
    }
}