Example usage for java.nio ByteBuffer slice

List of usage examples for java.nio ByteBuffer slice

Introduction

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

Prototype

public abstract ByteBuffer slice();

Source Link

Document

Returns a sliced buffer that shares its content with this buffer.

Usage

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);

    ByteBuffer bb = bbuf.slice();

    System.out.println(Arrays.toString(bb.array()));
}

From source file:org.commoncrawl.service.queryserver.master.S3Helper.java

public static void main(String[] args) {
    File arcFile = new File(args[0]);
    long offset = Long.parseLong(args[1]);
    long contentSize = Long.parseLong(args[2]);

    try {/*  w w w.  j  a  v a 2 s  .com*/
        RandomAccessFile fileHandle = new RandomAccessFile(arcFile, "r");

        fileHandle.seek(Math.max(offset - 10, 0));

        byte data[] = new byte[(int) contentSize + 10];
        fileHandle.readFully(data);

        ByteBuffer buffer = ByteBuffer.wrap(data);
        buffer.position(0);

        int position = scanForGZIPHeader(buffer.slice());

        buffer.position(position);

        StreamingArcFileReader reader = new StreamingArcFileReader(false);
        reader.available(buffer);
        ArcFileItem nextItem = reader.getNextItem();
        System.out.println(nextItem.getUri());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * <p>/*from   www  .  jav  a 2  s.  c o  m*/
 * Creates a new byte buffer whose content is a shared subsequence of this
 * buffer's content.
 * </p>
 * <p>
 * The content of the new buffer will start at this buffer's current position.
 * Changes to this buffer's content will be visible in the new buffer, and
 * vice versa; the two buffers' position, limit, and mark values will be
 * independent. The utility method also preserves the byte order of the source
 * buffer into the new buffer.
 * </p>
 * <p>
 * The new buffer's position will be zero, its capacity and its limit will be
 * the number of bytes remaining in this buffer, and its mark will be
 * undefined. The new buffer will be direct if, and only if, this buffer is
 * direct, and it will be read-only if, and only if, this buffer is read-only.
 * </p>
 * 
 * @param buffer
 *          source buffer
 * @return new buffer
 */
public static ByteBuffer slice(ByteBuffer buffer) {

    final ByteOrder o = buffer.order();
    final ByteBuffer r = buffer.slice();
    r.order(o);

    return r;
}

From source file:Main.java

/**
 * Finds next Nth MPEG bitstream marker 0x000001xx and returns the data that
 * preceeds it as a ByteBuffer slice//from w  w  w  . j  av  a 2 s.  c  o m
 * 
 * Segment byte order is always little endian
 * 
 * @param buf
 * @return
 */
public static final ByteBuffer gotoMarker(ByteBuffer buf, int n, int mmin, int mmax) {
    if (!buf.hasRemaining())
        return null;

    int from = buf.position();
    ByteBuffer result = buf.slice();
    result.order(ByteOrder.BIG_ENDIAN);

    int val = 0xffffffff;
    while (buf.hasRemaining()) {
        val = (val << 8) | (buf.get() & 0xff);
        if (val >= mmin && val <= mmax) {
            if (n == 0) {
                buf.position(buf.position() - 4);
                result.limit(buf.position() - from);
                break;
            }
            --n;
        }
    }
    return result;
}

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

public static ByteBuffer byteBufferDeepCopy(ByteBuffer src) {
    ByteBuffer copy = ByteBuffer.allocate(src.remaining()).put(src.slice());
    copy.flip();/*w w  w .  j  av a  2 s  .c o  m*/
    return copy;
}

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java

static Pair<byte[], Long> decompose(ByteBuffer composite) {
    composite = composite.slice().order(ByteOrder.BIG_ENDIAN);

    short len = composite.getShort();
    byte[] colName = new byte[len];
    composite.get(colName);//w  w  w.  jav  a  2  s  . c  o  m

    short shouldBeZero = composite.getShort();
    Validate.isTrue(shouldBeZero == 0);

    byte shouldBe8 = composite.get();
    Validate.isTrue(shouldBe8 == 8);
    long ts = composite.getLong();

    return Pair.create(colName, (~ts));
}

From source file:Main.java

public static ByteBuffer copyBinary(final ByteBuffer orig) {
    if (orig == null) {
        return null;
    }//w  ww  .j  a  v a  2 s .  com
    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:de.javagl.jgltf.model.io.GltfUtils.java

/**
 * Tries to find an <code>ImageReader</code> that is capable of reading
 * the given image data. The returned image reader will be initialized
 * by passing an ImageInputStream that is created from the given data
 * to its <code>setInput</code> method. The caller is responsible for 
 * disposing the returned image reader.//  w w  w . j a  v  a2s.c o  m
 *  
 * @param imageData The image data
 * @return The image reader
 * @throws IOException If no matching image reader can be found
 */
@SuppressWarnings("resource")
private static ImageReader findImageReader(ByteBuffer imageData) throws IOException {
    InputStream inputStream = Buffers.createByteBufferInputStream(imageData.slice());
    ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
    Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
    if (imageReaders.hasNext()) {
        ImageReader imageReader = imageReaders.next();
        imageReader.setInput(imageInputStream);
        return imageReader;
    }
    throw new IOException("Could not find ImageReader for image data");
}

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

public static ByteBuffer byteBufferDeepCopy(ByteBuffer src, ByteBuffer dst) {
    if (dst == null || dst.capacity() < src.remaining()) {
        dst = byteBufferDeepCopy(src);// w ww. j a v a 2s.  co  m
    } else {
        dst.rewind();
        dst.limit(src.remaining());
        dst.put(src.slice());
        dst.flip();
    }
    return dst;
}

From source file:org.wso2.andes.amqp.AMQPUtils.java

/**
 * convert Andes metadata to StorableMessageMetaData
 *
 * @param andesMessageMetadata andes metadata
 * @return StorableMessageMetaData//from ww w. j  ava2s  .  c o  m
 */
public static StorableMessageMetaData convertAndesMetadataToAMQMetadata(
        AndesMessageMetadata andesMessageMetadata) {
    byte[] dataAsBytes = andesMessageMetadata.getMetadata();
    ByteBuffer buf = ByteBuffer.wrap(dataAsBytes);
    buf.position(1);
    buf = buf.slice();
    MessageMetaDataType type = MessageMetaDataType.values()[dataAsBytes[0]];
    return type.getFactory().createMetaData(buf);
}