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:eu.pursuit.core.ScopeID.java

public byte[] toByteArray() {
    ByteBuffer buffer = ByteBuffer.allocate(getLength());
    fill(buffer);
    return buffer.array();
}

From source file:jext2.SymlinkInode.java

@NotThreadSafe(useLock = true)
private void writeSlowSymlink(String link, int size) throws JExt2Exception, NoSpaceLeftOnDevice, FileTooLarge {
    ByteBuffer buf = ByteBuffer.allocate(Ext2fsDataTypes.getStringByteLength(link));
    Ext2fsDataTypes.putString(buf, link, buf.capacity(), 0);
    buf.rewind();/*from   ww w.  ja va  2 s.  com*/
    writeData(buf, 0);
}

From source file:fr.ortolang.diffusion.security.authentication.TOTPHelper.java

private static int generateTOTP(String secret, long time) {
    Base32 codec = new Base32();
    byte[] decodedKey = codec.decode(secret);
    byte[] msg = ByteBuffer.allocate(8).putLong(time).array();
    byte[] hash = hmacSha(decodedKey, msg);
    int offset = hash[hash.length - 1] & 0xf;
    int binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16)
            | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff);
    return binary % 1000000;
}

From source file:com.byteatebit.nbecho.NbEchoService.java

public NbEchoService(INbContext nbContext, SocketChannel socket) {
    super(nbContext, socket);
    this.readDelimitedMessageTask = ReadDelimitedMessageTask.Builder.builder().withDelimiter(MESSAGE_DELIMITER)
            .withByteBuffer(ByteBuffer.allocate(2048)).build();
    this.writeMessageTask = WriteMessageTask.Builder.builder().withByteBuffer(ByteBuffer.allocate(2048))
            .build();//w w  w  . ja v a 2  s. c o m
}

From source file:Main.java

public static byte[] aesIGEencrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) {
    try {//  ww w.  j a  v  a 2  s  . co  m

        ByteBuffer out = ByteBuffer.allocate(data.length);

        byte[] ivp = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2);
        byte[] iv2p = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length);

        int len = data.length / AES_BLOCK_SIZE;

        byte[] xorInput = null;
        byte[] xorOutput = null;

        SecretKeySpec keySpec = null;
        keySpec = new SecretKeySpec(tmpAesKey, "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] input = null;
        byte[] output = null;

        for (int i = 0; i < len; i++) {

            input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE);
            xorInput = xor(input, ivp);
            output = cipher.doFinal(xorInput);
            xorOutput = xor(output, iv2p);
            out.put(xorOutput);

            ivp = xorOutput;
            iv2p = input;
        }
        return out.array();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public final static ByteBuffer borrowByteBufferVeryLarge() {
    if (byteBuffersPoolVeryLarge.isEmpty()) {
        return ByteBuffer.allocate(MAX_LINE_BYTES_VERY_LARGE);
    } else {//from w w  w.  j a va  2 s .  c om
        return byteBuffersPoolVeryLarge.remove(0);
    }
}

From source file:Main.java

public static String largeFileSha1(File file) {
    InputStream inputStream = null;
    try {//from  w  ww .j a  v  a 2 s .  c o  m
        MessageDigest gsha1 = MessageDigest.getInstance("SHA1");
        MessageDigest sha1 = MessageDigest.getInstance("SHA1");
        inputStream = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[1024];
        int nRead = 0;
        int block = 0;
        int count = 0;
        final int BLOCK_SIZE = 4 * 1024 * 1024;
        while ((nRead = inputStream.read(buffer)) != -1) {
            count += nRead;
            sha1.update(buffer, 0, nRead);
            if (BLOCK_SIZE == count) {
                gsha1.update(sha1.digest());
                sha1 = MessageDigest.getInstance("SHA1");
                block++;
                count = 0;
            }
        }
        if (count != 0) {
            gsha1.update(sha1.digest());
            block++;
        }
        byte[] digest = gsha1.digest();

        byte[] blockBytes = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(block).array();

        byte[] result = ByteBuffer.allocate(4 + digest.length).put(blockBytes, 0, blockBytes.length)
                .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:com.byteatebit.nbserver.simple.SimpleNbServerTestTcpService.java

public SimpleNbServerTestTcpService(INbContext nbContext, SocketChannel socket) {
    super(nbContext, socket);
    this.readDelimitedMessageTask = ReadDelimitedMessageTask.Builder.builder().withDelimiter(MESSAGE_DELIMITER)
            .withByteBuffer(ByteBuffer.allocate(2048)).build();
    this.writeMessageTask = WriteMessageTask.Builder.builder().withByteBuffer(ByteBuffer.allocate(2048))
            .build();/*  w  ww  . j a v  a  2 s. c o  m*/
}

From source file:FindInt.java

public int seek() throws IOException {
    int num = -1;

    try (SeekableByteChannel fc = Files.newByteChannel(file)) {

        ByteBuffer buf = ByteBuffer.allocate(8);

        //Get the offset into the file.
        fc.read(buf);/*  www  .  j ava2 s  .  c  o  m*/
        long offset = buf.getLong(0);

        //Seek to the offset location.
        fc.position(offset);
        buf.rewind();

        //Read the 'secret' value.
        fc.read(buf);
        num = buf.getInt(0);
    } catch (IOException x) {
        System.err.println(x);
    }
    return num;
}

From source file:com.spotify.heroic.metric.datastax.schema.legacy.MapSerializer.java

@Override
public ByteBuffer serialize(final Map<A, B> value) throws IOException {
    final List<Pair<ByteBuffer, ByteBuffer>> buffers = new ArrayList<>();

    short size = 0;

    for (final Map.Entry<A, B> e : value.entrySet()) {
        final ByteBuffer key = a.serialize(e.getKey());
        final ByteBuffer val = b.serialize(e.getValue());

        size += key.limit() + val.limit();

        buffers.add(Pair.of(key, val));
    }/*from  ww w. ja va2s  .com*/

    final ByteBuffer buffer = ByteBuffer.allocate(4 + 8 * value.size() + size);
    buffer.putShort((short) buffers.size());

    for (final Pair<ByteBuffer, ByteBuffer> p : buffers) {
        buffer.putShort((short) p.getLeft().remaining());
        buffer.put(p.getLeft());
        buffer.putShort((short) p.getRight().remaining());
        buffer.put(p.getRight());
    }

    buffer.flip();
    return buffer;
}