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 Bitmap byteArrayToBitmap(int width, int height, byte[] byteArray) {
    // use Bitmap.Config.ARGB_8888 instead of type is OK
    Bitmap stitchBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    stitchBmp.copyPixelsFromBuffer(ByteBuffer.wrap(byteArray));
    return stitchBmp;
}

From source file:Main.java

public static String bytesToUuid(byte[] bytes) {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(ByteOrder.LITTLE_ENDIAN);

    long lsb = bb.getLong();
    long msb = bb.getLong();
    UUID uuid = new UUID(msb, lsb);
    return uuidToString(uuid);
}

From source file:Main.java

public static Short byteArrayToShort(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }/*from  w w  w  .ja va 2 s  .c  o m*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.BIG_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static Short byteArrayToShortLE(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }/*www  . j a  v a2 s  .  com*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

/**
 * Generates a Base64 version of UUID/*from ww w. j  a  va2s  . com*/
 * @return a string in Base64 URL-safe format without padding at the end and no wrapping
 */
public static String randomUuidBase64() {
    UUID uuid = UUID.randomUUID();
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return encodeUrlSafe64(bb.array());
}

From source file:Main.java

public static long ReadlittleEndianLong(InputStream dis) throws IOException {
    byte[] bytes = new byte[8];
    int re = dis.read(bytes);
    if (re == -1) {
        return -1;
    }/* ww  w.  jav a 2s.co m*/
    ByteBuffer bytebuffer = ByteBuffer.wrap(bytes);
    bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
    long result = bytebuffer.getLong();
    return result;
}

From source file:Main.java

public static int byteArrayToInt(byte[] bytes) throws IllegalArgumentException {
    if (bytes.length != (Integer.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to an int");
    }// w w w  .j a v  a2s .  c o  m
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.BIG_ENDIAN);
    return buffer.getInt();
}

From source file:Main.java

public static int byteArrayToIntLE(byte[] bytes) throws IllegalArgumentException {
    if (bytes.length != (Integer.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to an int");
    }/*from ww  w .  j av  a 2 s. c o  m*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getInt();
}

From source file:Main.java

public static boolean isPureAscii(String v) {
    byte bytearray[] = v.getBytes();
    CharsetDecoder d = Charset.forName("US-ASCII").newDecoder();
    try {//from   w  w  w  .j a  v  a2s. c om
        CharBuffer r = d.decode(ByteBuffer.wrap(bytearray));
        r.toString();
    } catch (CharacterCodingException e) {
        return false;
    }
    return true;
}

From source file:Main.java

private static void writeFileFromBytes(final File file, final byte[] bytes) {
    FileChannel fc = null;/*ww  w  . j  a va 2  s.  co  m*/
    try {
        fc = new FileOutputStream(file, false).getChannel();
        fc.write(ByteBuffer.wrap(bytes));
        fc.force(true);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fc != null) {
                fc.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}