Example usage for java.nio ByteBuffer wrap

List of usage examples for java.nio ByteBuffer wrap

Introduction

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

Prototype

public static ByteBuffer wrap(byte[] array) 

Source Link

Document

Creates a new byte buffer by wrapping the given byte array.

Usage

From source file:Main.java

public static ByteBuffer readBytes(String file) throws IOException {
    ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
    FileChannel fChannel = new RandomAccessFile(file, "r").getChannel();
    fChannel.transferTo(0, fChannel.size(), Channels.newChannel(dataOut));
    fChannel.close();/* w  w  w.j a v  a  2s. c o  m*/
    return ByteBuffer.wrap(dataOut.toByteArray());
}

From source file:com.scf.utils.UUIDUtilies.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:adminpassword.Decryption.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText, String idKey) throws Exception {
    String password = idKey;/*from  w w  w. j av  a 2  s. c o m*/

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    //strip off the salt and iv
    ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText));
    byte[] saltBytes = new byte[20];
    buffer.get(saltBytes, 0, saltBytes.length);
    byte[] ivBytes1 = new byte[cipher.getBlockSize()];
    buffer.get(ivBytes1, 0, ivBytes1.length);
    byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length];
    buffer.get(encryptedTextBytes);

    // Deriving the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1));

    byte[] decryptedTextBytes = null;
    try {
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);

    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

From source file:eu.pursuit.core.ItemName.java

public static ItemName parseSerializedName(byte[] array, int segmentSize) {
    if (array.length % segmentSize != 0) {
        throw new IllegalArgumentException(
                "array length (" + array.length + ") not a multiple of segmentSize (" + segmentSize + ")");
    }/*from  w w  w .j a v  a 2  s  . co  m*/

    ByteBuffer bbuffer = ByteBuffer.wrap(array);
    List<ByteIdentifier> list = new ArrayList<ByteIdentifier>();
    int howmany = array.length / segmentSize;
    for (int i = 0; i < howmany - 1; i++) {
        byte[] arr = new byte[segmentSize];
        bbuffer.get(arr);
        list.add(new ByteIdentifier(arr));
    }
    ScopeID scope = new ScopeID(list);

    byte[] arr = new byte[segmentSize];
    bbuffer.get(arr);
    ByteIdentifier rid = new ByteIdentifier(arr);

    return new ItemName(scope, rid);
}

From source file:com.google.mr4c.util.ByteBufferInputStreamTest.java

@Test
public void testReadFully() throws Exception {
    byte[] data = new byte[] { -45, 76, 93, -112, 0 };
    ByteBuffer buf = ByteBuffer.wrap(data);
    ByteBufferInputStream stream = new ByteBufferInputStream(buf);
    byte[] result = IOUtils.toByteArray(stream);
    stream.close();/*  w  w w. j  av  a2 s  .c o m*/
    assertTrue(Arrays.equals(data, result));
}

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

public static String decodeBase58Uuid(String base58uuid) {
    byte[] byUuid = Base58.decode(base58uuid);
    ByteBuffer bb = ByteBuffer.wrap(byUuid);
    UUID uuid = new UUID(bb.getLong(), bb.getLong());
    return uuid.toString();
}

From source file:eu.databata.engine.util.PropagationUtils.java

/**
 * This method is suitable for reading files not larger than 2 GB.
 *///from  ww  w .j  av  a2  s.c o  m
private static String readFile(File file, String encoding) {
    String result = null;
    try {
        FileInputStream inputStream = new FileInputStream(file);
        try {
            byte[] bytes = new byte[(int) inputStream.getChannel().size()];
            inputStream.read(bytes);
            result = Charset.forName(encoding).decode(ByteBuffer.wrap(bytes)).toString();
        } finally {
            inputStream.close();
        }
    } catch (Exception e) {
        LOG.warn("Failed to read file: " + file.getName());
        throw new RuntimeException(e);
    }
    return dos2Unix(result);
}

From source file:com.nestedbird.util.UUIDConverter.java

/**
 * Turns a UUID in string format to a Base64 encoded version
 *
 * @param uuidString String representation of the uuid
 * @return base64 encoded version of the uuid
 * @throws IllegalArgumentException String must be a valid uuid
 * @throws NullPointerException     String cannot be null
 *///from   w  w  w . j  a v a 2 s .  c o  m
public static String toBase64(final String uuidString) {
    if (uuidString == null)
        throw new NullPointerException("String cannot be null");
    if (!isUUID(uuidString))
        throw new IllegalArgumentException("string must be a valid uuid");

    final UUID uuid = UUID.fromString(uuidString);
    final ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return Base64.encodeBase64URLSafeString(bb.array());
}

From source file:ninja.siden.internal.RendererSelectorTest.java

@Parameters(name = "{0}")
public static Iterable<Object[]> parameters() throws Exception {
    int port = 8000;
    return Arrays.asList(new Object[][] { { "String", "Hello", port++ }, { "File", tmp().toFile(), port++ },
            { "Path", tmp(), port++ }, { "FileChannel", FileChannel.open(tmp()), port++ },
            { "byteArray", "Hello".getBytes(), port++ },
            { "ByteBuffer", ByteBuffer.wrap("Hello".getBytes()), port++ }, { "URI", tmp().toUri(), port++ },
            { "URL", tmp().toUri().toURL(), port++ }, { "Reader", Files.newBufferedReader(tmp()), port++ },
            { "InputStream", new ByteArrayInputStream("Hello".getBytes()), port++ },
            { "CharSequence", new StringBuilder("Hello"), port++ },

    });/*from ww  w .ja  v  a 2  s . c  o  m*/
}

From source file:com.crushpaper.UuidlIdGenerator.java

@Override
public String getAnotherId() {
    final UUID uuid = UUID.randomUUID();
    final ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return Base64.encodeBase64URLSafeString(bb.array());
}