Example usage for java.nio ByteBuffer arrayOffset

List of usage examples for java.nio ByteBuffer arrayOffset

Introduction

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

Prototype

public final int arrayOffset() 

Source Link

Document

Returns the offset of the byte array which this buffer is based on, if there is one.

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

    System.out.println(bbuf.arrayOffset());
}

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

    System.out.println(bbuf.arrayOffset());
}

From source file:Main.java

public static boolean wrapsFullArray(ByteBuffer byteBuffer) {
    return byteBuffer.hasArray() && byteBuffer.position() == 0 && byteBuffer.arrayOffset() == 0
            && byteBuffer.remaining() == byteBuffer.capacity();
}

From source file:Main.java

public static void toString(ByteBuffer bb, StringBuilder sb) {
    byte[] buf = bb.array();

    int arrayOffset = bb.arrayOffset();
    int offset = arrayOffset + bb.position();
    int origLimit = arrayOffset + bb.limit();
    int limit = (origLimit - offset > 128) ? offset + 128 : origLimit;

    for (int i = offset; i < limit; i++) {
        if (i > offset) {
            sb.append(" ");
        }/*from ww w.  ja v  a2 s .c o m*/
        sb.append(paddedByteString(buf[i]));
    }
    if (origLimit != limit) {
        sb.append("...");
    }
}

From source file:net.sf.jml.util.DigestUtils.java

private static void update(MessageDigest digest, ByteBuffer buffer) {
    if (buffer.hasArray()) {
        digest.update(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
    } else {/*w w w.  j  a va 2  s .co m*/
        byte[] b = new byte[buffer.remaining()];
        buffer.get(b);
        digest.update(b);
    }
}

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static String string(ByteBuffer b) {
    return new String(b.array(), b.arrayOffset() + b.position(), b.remaining());
}

From source file:Main.java

public static int byteBufferToByteArray(ByteBuffer byteBuffer, byte[] target, int offset) {
    int remaining = byteBuffer.remaining();
    System.arraycopy(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), target, offset,
            remaining);// w ww .  jav  a 2s  .c  o  m
    return remaining;
}

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static int compare(byte[] o1, ByteBuffer o2) {
    return compareUnsigned(o1, o2.array(), 0, o2.arrayOffset() + o2.position(), o1.length,
            o2.limit() + o2.arrayOffset());
}

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static String string(ByteBuffer b, Charset charset) {
    return new String(b.array(), b.arrayOffset() + b.position(), b.remaining(), charset);
}

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static int compare(ByteBuffer o1, byte[] o2) {
    return compareUnsigned(o1.array(), o2, o1.arrayOffset() + o1.position(), 0, o1.limit() + o1.arrayOffset(),
            o2.length);// ww  w.  j  a  va2s . com
}