Example usage for java.nio ByteBuffer mark

List of usage examples for java.nio ByteBuffer mark

Introduction

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

Prototype

public final Buffer mark() 

Source Link

Document

Marks the current position, so that the position may return to this point later by calling reset().

Usage

From source file:MainClass.java

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

    bb.mark();
    bb.position(5);//  w  w  w . j  av  a2 s.  c  om
    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();
    bbuf.reset();//from   w w w .java2s. c  om
    System.out.println(Arrays.toString(bbuf.array()));
}

From source file:MainClass.java

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

    bb.put((byte) 0x07);
    bb.put((byte) 0x08);
    bb.put((byte) 0x09);
    bb.put((byte) 0x10);
    bb.put((byte) 0x11);
    bb.put((byte) 0x12);
    bb.put((byte) 0x13);
    bb.put((byte) 0x14);

    bb.position(1).limit(5);/*from   w ww. ja  v  a 2  s . c  om*/
    bb.mark();

    System.out.println("Expect an exception here");
    System.out.println("" + bb.order().toString() + ": " + bb.getLong());

}

From source file:Main.java

public static ByteBuffer copyByteBuffer(ByteBuffer buf) {
    ByteBuffer dest = newByteBuffer(buf.remaining());
    buf.mark();
    dest.put(buf);/*w  w w  .j  a  va2  s  .c  om*/
    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  ava  2s  .  c  om
    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 www.  ja va 2  s  .co  m
 * @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: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 .  co  m
 * @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: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);/*from   w w  w . j a v a2s  . 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.glaf.core.util.BinaryUtils.java

/**
 * Returns a copy of all the bytes from the given <code>ByteBuffer</code>,
 * from the beginning to the buffer's limit; or null if the input is null.
 * <p>//from   w  w w  .ja va  2s.  c om
 * 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[] copyAllBytesFrom(ByteBuffer bb) {
    if (bb == null)
        return null;
    if (bb.hasArray())
        return Arrays.copyOf(bb.array(), bb.limit());
    bb.mark();
    // the default ByteBuffer#mark() and reset() won't work, as the
    // rewind would discard the mark position
    final int marked = bb.position();
    try {
        byte[] dst = new byte[bb.rewind().remaining()];
        bb.get(dst);
        return dst;
    } finally {
        bb.position(marked);
    }
}

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./*www.j  av  a  2 s.co  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();
    }
}