Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

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

Usage

From source file:Main.java

public static String readTxtFromFile(File file) throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
    FileInputStream in = new FileInputStream(file);
    in.getChannel().read(buffer);/*from w w  w. j a  v  a  2s  .  c  o  m*/
    in.close();
    return new String(buffer.array());
}

From source file:com.liveramp.cascading_ext.Bytes.java

/**
 * If the given ByteBuffer wraps completely its underlying byte array, return the underlying
 * byte array (no copy). Otherwise, return a deep copy of the range represented by the given
 * ByteBuffer.//ww  w  .  ja v  a  2 s  .  co m
 *
 * @param byteBuffer
 */
public static byte[] byteBufferToByteArray(ByteBuffer byteBuffer) {
    if (wrapsFullArray(byteBuffer)) {
        return byteBuffer.array();
    }
    byte[] target = new byte[byteBuffer.remaining()];
    byteBufferToByteArray(byteBuffer, target, 0);
    return target;
}

From source file:Main.java

public final static int count(final ByteBuffer bb1, final ByteBuffer bb2) {
    final int end1 = bb1.limit();
    final int end2 = bb2.limit();
    final byte[] array1 = bb1.array();
    final byte[] array2 = bb2.array();
    return count(array1, 0, end1, array2, 0, end2);
}

From source file:Main.java

/**
 * Convert from singed 16-bit PCM to 32-bit float PCM.
 * @param byteArray byte array/* w  w w .  jav a2s  .c o m*/
 * @return byte array
 */
public static byte[] convert16BitTo32Bit(final byte[] byteArray) {
    float[] audioDataF = shortToFloat(byteToShort(byteArray));
    for (int i = 0; i < audioDataF.length; i++) {
        audioDataF[i] /= 32768.0;
    }

    FloatBuffer fb = FloatBuffer.wrap(audioDataF);
    ByteBuffer byteBuffer = ByteBuffer.allocate(fb.capacity() * 4);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    byteBuffer.asFloatBuffer().put(fb);
    return byteBuffer.array();
}

From source file:com.bconomy.autobit.Encryption.java

private static byte[] charsToBytes(char[] chars) {
    CharBuffer charBuffer = CharBuffer.wrap(chars);
    ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
    byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
    Arrays.fill(charBuffer.array(), '\u0000'); // clear sensitive data
    Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data
    Arrays.fill(chars, '\u0000'); // clear sensitive data
    return bytes;
}

From source file:Main.java

public static byte[] uuidToBytes(String uuidStr) {
    UUID uuid = stringToUuid(uuidStr);

    ByteBuffer bb = ByteBuffer.allocate(16);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putLong(uuid.getLeastSignificantBits());
    bb.putLong(uuid.getMostSignificantBits());
    return bb.array();
}

From source file:com.hengyi.japp.tools.UuidUtils.java

protected static String base58Uuid(UUID uuid) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return Base58.encode(bb.array());
}

From source file:io.datenwelt.cargo.rest.utils.Strings.java

public static String token() {
    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES * 100);
    for (int i = 0; i < 100; i++) {
        buffer.putLong(random.nextLong());
    }/*from  ww  w  .  j av a 2 s.c  om*/
    return DigestUtils.md5Hex(buffer.array());
}

From source file:Main.java

public static final int indexOfP(final ByteBuffer lineBB, final byte[][] textsLower,
        final byte[][] textsUpper) {
    return indexOf(lineBB.array(), lineBB.position(), lineBB.limit(), textsLower, textsUpper);
}

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);//from  w w  w. ja v  a 2 s  .c  o m
        } 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;
}