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:com.icloud.framework.core.nio.ByteBufferUtil.java

public static int compareUnsigned(ByteBuffer o1, ByteBuffer o2) {
    return compareUnsigned(o1.array(), o2.array(), o1.arrayOffset() + o1.position(),
            o2.arrayOffset() + o2.position(), o1.limit() + o1.arrayOffset(), o2.limit() + o2.arrayOffset());
}

From source file:de.zalando.spring.cloud.config.aws.kms.KmsTextEncryptor.java

private static String extractString(final ByteBuffer bb) {
    if (bb.hasRemaining()) {
        final byte[] bytes = new byte[bb.remaining()];
        bb.get(bytes, bb.arrayOffset(), bb.remaining());
        return new String(bytes);
    } else {/*from w  w  w.  j  av  a  2  s  . c  om*/
        return EMPTY_STRING;
    }
}

From source file:com.cloudera.branchreduce.impl.thrift.Writables.java

public static <T extends Writable> T fromByteBuffer(ByteBuffer bb, Class<T> clazz) {
    T instance = ReflectionUtils.newInstance(clazz, DUMMY);
    try {/*w w  w . jav a  2 s. c  o m*/
        instance.readFields(
                new DataInputStream(new ByteArrayInputStream(bb.array(), bb.arrayOffset(), bb.limit())));
    } catch (IOException e) {
        LOG.error("Deserialization error for class: " + clazz, e);
    }
    return instance;
}

From source file:Main.java

public static ByteBuffer copyBinary(final ByteBuffer orig) {
    if (orig == null) {
        return null;
    }/* w  w  w  .  j av  a  2s .  c  o  m*/
    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:org.apache.nutch.util.StringUtil.java

/**
 * Get a text representation of a ByteBuffer as hexadecimal String, where each
 * pair of hexadecimal digits corresponds to consecutive bytes in the array.
 * /*from www.  j  a va2 s.  co  m*/
 * @param buf
 *          input data
 * @param sep
 *          separate every pair of hexadecimal digits with this separator, or
 *          null if no separation is needed.
 * @param lineLen
 *          break the output String into lines containing output for lineLen
 *          bytes.
 */
public static String toHexString(ByteBuffer buf, String sep, int lineLen) {
    return toHexString(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining(), sep, lineLen);
}

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

public static byte[] deepCopyByteBufferToByteArray(ByteBuffer byteBuffer) {
    byte[] result = new byte[byteBuffer.remaining()];
    System.arraycopy(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), result, 0,
            byteBuffer.remaining());/*from  www.j  a  v a 2s .  c o  m*/
    return result;
}

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

public static String bytesToHexString(ByteBuffer b) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < b.remaining(); ++i) {
        final int v = b.array()[b.arrayOffset() + b.position() + i] & 0xff;
        if (i > 0) {
            result.append(" ");
        }/*from ww  w.  j  a v  a 2s.c o  m*/
        if (v < 16) {
            result.append("0");
        }
        result.append(Integer.toString(v, 16));
    }
    return result.toString();
}

From source file:org.apache.spark.network.util.JavaUtils.java

/**
 * Returns a byte array with the buffer's contents, trying to avoid copying the data if
 * possible.//from   w  w w .jav  a2  s .  co m
 */
public static byte[] bufferToArray(ByteBuffer buffer) {
    if (buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.array().length == buffer.remaining()) {
        return buffer.array();
    } else {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        return bytes;
    }
}

From source file:gobblin.util.io.StreamUtils.java

/**
 * Reads the full contents of a ByteBuffer and writes them to an OutputStream. The ByteBuffer is
 * consumed by this operation; eg in.remaining() will be 0 after it completes successfully.
 * @param in  ByteBuffer to write into the OutputStream
 * @param out Destination stream/*w  w  w.j ava  2s  . c  o m*/
 * @throws IOException If there is an error writing into the OutputStream
 */
public static void byteBufferToOutputStream(ByteBuffer in, OutputStream out) throws IOException {
    final int BUF_SIZE = 8192;

    if (in.hasArray()) {
        out.write(in.array(), in.arrayOffset() + in.position(), in.remaining());
    } else {
        final byte[] b = new byte[Math.min(in.remaining(), BUF_SIZE)];
        while (in.remaining() > 0) {
            int bytesToRead = Math.min(in.remaining(), BUF_SIZE);
            in.get(b, 0, bytesToRead);

            out.write(b, 0, bytesToRead);
        }
    }
}

From source file:Main.java

public static List<ByteBuffer> mergeAdjacentBuffers(List<ByteBuffer> samples) {
    ArrayList<ByteBuffer> nuSamples = new ArrayList<ByteBuffer>(samples.size());
    for (ByteBuffer buffer : samples) {
        int lastIndex = nuSamples.size() - 1;
        if (lastIndex >= 0 && buffer.hasArray() && nuSamples.get(lastIndex).hasArray()
                && buffer.array() == nuSamples.get(lastIndex).array() && nuSamples.get(lastIndex).arrayOffset()
                        + nuSamples.get(lastIndex).limit() == buffer.arrayOffset()) {
            ByteBuffer oldBuffer = nuSamples.remove(lastIndex);
            ByteBuffer nu = ByteBuffer
                    .wrap(buffer.array(), oldBuffer.arrayOffset(), oldBuffer.limit() + buffer.limit()).slice();
            // We need to slice here since wrap([], offset, length) just sets position and not the arrayOffset.
            nuSamples.add(nu);/*w  ww  . j a v a 2 s  . com*/
        } else if (lastIndex >= 0 && buffer instanceof MappedByteBuffer
                && nuSamples.get(lastIndex) instanceof MappedByteBuffer && nuSamples.get(lastIndex)
                        .limit() == nuSamples.get(lastIndex).capacity() - buffer.capacity()) {
            // This can go wrong - but will it?
            ByteBuffer oldBuffer = nuSamples.get(lastIndex);
            oldBuffer.limit(buffer.limit() + oldBuffer.limit());
        } else {
            buffer.reset();
            nuSamples.add(buffer);
        }
    }
    return nuSamples;
}