Example usage for java.nio ByteBuffer allocate

List of usage examples for java.nio ByteBuffer allocate

Introduction

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

Prototype

public static ByteBuffer allocate(int capacity) 

Source Link

Document

Creates a byte buffer based on a newly allocated byte array.

Usage

From source file:Main.java

public static ByteBuffer createByteBufferOnHeap(final int size) {
    final ByteBuffer buf = ByteBuffer.allocate(size).order(ByteOrder.nativeOrder());
    buf.clear();//from   w ww  . j ava 2  s.c om
    return buf;
}

From source file:Main.java

public static String smallFileSha1(File file) {
    InputStream inputStream = null;
    try {/*  w w w  .  j  a va  2s  .  co  m*/
        MessageDigest md = MessageDigest.getInstance("SHA1");
        inputStream = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[1024];
        int nRead = 0;
        while ((nRead = inputStream.read(buffer)) != -1) {
            md.update(buffer, 0, nRead);
        }
        byte[] digest = md.digest();
        byte[] blockBytes = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(1).array();
        byte[] result = ByteBuffer.allocate(4 + digest.length).put(blockBytes, 0, 4)
                .put(digest, 0, digest.length).array();
        return Base64.encodeToString(result, Base64.URL_SAFE | Base64.NO_WRAP);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static IntBuffer createIntBufferOnHeap(final int size) {
    final IntBuffer buf = ByteBuffer.allocate(4 * size).order(ByteOrder.nativeOrder()).asIntBuffer();
    buf.clear();/*from   w w  w  . j  a v a2 s  .  c  o m*/
    return buf;
}

From source file:Main.java

public static FloatBuffer createFloatBufferOnHeap(final int size) {
    final FloatBuffer buf = ByteBuffer.allocate(4 * size).order(ByteOrder.nativeOrder()).asFloatBuffer();
    buf.clear();//  w w w.j a v  a 2s . c  o  m
    return buf;
}

From source file:Main.java

public static ShortBuffer createShortBufferOnHeap(final int size) {
    final ShortBuffer buf = ByteBuffer.allocate(2 * size).order(ByteOrder.nativeOrder()).asShortBuffer();
    buf.clear();//from  w w w  .  ja  v a2  s.  c  o  m
    return buf;
}

From source file:Main.java

public static DoubleBuffer createDoubleBufferOnHeap(final int size) {
    final DoubleBuffer buf = ByteBuffer.allocate(8 * size).order(ByteOrder.nativeOrder()).asDoubleBuffer();
    buf.clear();/*from   ww w .j  ava  2 s  .  c o m*/
    return buf;
}

From source file:Main.java

public static byte[] leIntToByteArray(int i) {
    final ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putInt(i);//from   w  w  w  . jav a  2s.co m
    return bb.array();
}

From source file:br.com.semanticwot.cd.discovery.infra.Hash.java

public static String hash(Object entity) {
    int hashCode = HashCodeBuilder.reflectionHashCode(entity);

    byte[] array = ByteBuffer.allocate(4).putInt(hashCode).array();

    String hash = Hex.encodeHexString(array);
    return hash;//from   w ww . j av  a2 s  .c om

}

From source file:Main.java

public static byte[] getAESkey(byte[] authKey, byte[] msgKey, boolean isOut) {
    int x = 0;/*from  w  ww .ja va 2  s  .  c  o m*/
    if (!isOut)
        x = 8;

    ByteBuffer tempA = ByteBuffer.allocate(msgKey.length + 32);
    tempA.put(msgKey);
    tempA.put(authKey, x, 32);
    byte[] a = getSHA1hash(tempA.array());

    ByteBuffer tempB = ByteBuffer.allocate(msgKey.length + 32);
    tempB.put(authKey, 32 + x, 16);
    tempB.put(msgKey);
    tempB.put(authKey, 48 + x, 16);
    byte[] b = getSHA1hash(tempB.array());

    ByteBuffer tempC = ByteBuffer.allocate(msgKey.length + 32);
    tempC.put(authKey, 64 + x, 32);
    tempC.put(msgKey);
    byte[] c = getSHA1hash(tempC.array());

    ByteBuffer key = ByteBuffer.allocate(8 + 12 + 12);
    key.put(a, 0, 8);
    key.put(b, 8, 12);
    key.put(c, 4, 12);

    return key.array();
}

From source file:Main.java

/**
 * Convert the given int to a 4-byte array
 * /*from   w  w  w  . j a  v a2 s .co  m*/
 * @param i
 *            An 'int'
 * 
 * @return A 4-byte array in big endian (MSB) ordering
 */
public static byte[] convertIntToByteArrayBigEndian(int i) {
    return ByteBuffer.allocate(Integer.SIZE / EIGHT).putInt(i).array();
}