Example usage for java.nio ByteBuffer rewind

List of usage examples for java.nio ByteBuffer rewind

Introduction

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

Prototype

public final Buffer rewind() 

Source Link

Document

Rewinds this buffer.

Usage

From source file:Main.java

public static ByteBuffer cloneOnHeap(final ByteBuffer buf) {
    if (buf == null) {
        return null;
    }/* ww w .j av a 2s  .co m*/
    buf.rewind();

    final ByteBuffer copy = createByteBufferOnHeap(buf.limit());
    copy.put(buf);

    return copy;
}

From source file:Main.java

public static ByteBuffer copyByteBuffer(ByteBuffer buf) {
    ByteBuffer dest = newByteBuffer(buf.remaining());
    buf.mark();/* w w  w . j a v  a 2s. co  m*/
    dest.put(buf);
    buf.reset();
    dest.rewind();
    return dest;
}

From source file:Main.java

public static ByteBuffer copyShortBufferAsByteBuffer(ShortBuffer buf) {
    ByteBuffer dest = newByteBuffer(buf.remaining() * SIZEOF_SHORT);
    buf.mark();//from   w  ww  . j a v  a2  s. c o  m
    dest.asShortBuffer().put(buf);
    buf.reset();
    dest.rewind();
    return dest;
}

From source file:Main.java

public static ByteBuffer copyFloatBufferAsByteBuffer(FloatBuffer buf) {
    ByteBuffer dest = newByteBuffer(buf.remaining() * SIZEOF_FLOAT);
    buf.mark();/*from  w ww  . j a v  a 2 s.  c o  m*/
    dest.asFloatBuffer().put(buf);
    buf.reset();
    dest.rewind();
    return dest;
}

From source file:Main.java

public static void readData(SeekableByteChannel seekableChannel, Charset cs) throws IOException {
    ByteBuffer byteBuffer = ByteBuffer.allocate(128);
    String encoding = System.getProperty("file.encoding");
    while (seekableChannel.read(byteBuffer) > 0) {
        byteBuffer.rewind();
        CharBuffer charBuffer = cs.decode(byteBuffer);
        System.out.print(charBuffer);
        byteBuffer.flip();/* w ww.j a  va2 s.  com*/
    }
}

From source file:Main.java

public static ByteBuffer copyDoubleBufferAsByteBuffer(DoubleBuffer buf) {
    ByteBuffer dest = newByteBuffer(buf.remaining() * SIZEOF_DOUBLE);
    buf.mark();//from   ww w.  j av a 2 s.c o m
    dest.asDoubleBuffer().put(buf);
    buf.reset();
    dest.rewind();
    return dest;
}

From source file:com.knewton.mapreduce.util.RandomStudentEventGenerator.java

/**
 * Helper method for wrapping an id in a <code>ByteBuffer</code>.
 *
 * @return A byte buffer with a random ID
 *///  w w  w. j a  v a  2 s .  c  o  m
public static ByteBuffer getRandomIdBuffer() {
    long id = getRandomId();
    ByteBuffer bb = ByteBuffer.wrap(new byte[8]);
    bb.putLong(id);
    bb.rewind();
    return bb;
}

From source file:com.knewton.mapreduce.util.RandomStudentEventGenerator.java

/**
 * Helper method for generating decorated increasing student ids. Used by
 * {@link WriteSampleSSTable} for writing a sorted SSTable.
 *
 * @return A list of byte buffers with student IDs
 *///from   w  w  w  .jav a  2s  . c om
public static List<ByteBuffer> getStudentIds(int numberOfStudents) {
    long studentId = 1000000L;
    List<ByteBuffer> studentIds = Lists.newArrayListWithCapacity(numberOfStudents);

    for (int i = 0; i < numberOfStudents; i++) {
        ByteBuffer bb = ByteBuffer.wrap(new byte[8]);
        bb.putLong(studentId++);
        bb.rewind();
        studentIds.add(bb);
    }

    Collections.sort(studentIds, new Comparator<ByteBuffer>() {
        @Override
        public int compare(ByteBuffer o1, ByteBuffer o2) {
            DecoratedKey dk1 = StorageService.getPartitioner().decorateKey(o1);
            DecoratedKey dk2 = StorageService.getPartitioner().decorateKey(o2);
            return dk1.compareTo(dk2);
        }
    });

    return studentIds;
}

From source file:Main.java

private static byte[] byteBufferToArray(ByteBuffer bb) {
    byte[] bytes = new byte[bb.position()];
    bb.rewind();
    bb.get(bytes, 0, bytes.length);//from   w w  w .  j  a  v a 2s  .c om
    return bytes;
}

From source file:Main.java

public static byte[] toByteArrayNew(Bitmap source) {
    //int size = source.getRowBytes() * source.getHeight();
    int size = source.getByteCount();
    ByteBuffer byteBuffer = ByteBuffer.allocate(size);
    source.copyPixelsToBuffer(byteBuffer);
    byteBuffer.rewind();
    byte[] b = byteBuffer.array();
    return b;/*from www.  jav  a2 s. co  m*/
}