Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

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

Prototype

public abstract byte get(int index);

Source Link

Document

Returns the byte at the specified index and does not change the position.

Usage

From source file:Main.java

public static byte[] getBytes(Bitmap bitmap) {
    int byteCount = bitmap.getByteCount();
    ByteBuffer buffer = ByteBuffer.allocate(byteCount);
    bitmap.copyPixelsToBuffer(buffer);/*  w  w  w .j  a  va  2  s .c  om*/
    buffer.rewind();

    byte[] data = new byte[byteCount];
    buffer.get(data);
    return data;
}

From source file:Main.java

/**
 * Converts a char array into a byte array using the default character set.
 * /* ww  w.jav  a 2 s .co m*/
 * @param chars
 *            The source characters.
 * @param charsetName
 *            The character set to use.
 * @return The result bytes.
 */
public static byte[] toByteArray(char[] chars, String charsetName) {
    java.nio.CharBuffer cb = java.nio.CharBuffer.wrap(chars);
    java.nio.ByteBuffer bb = java.nio.charset.Charset.forName(charsetName).encode(cb);
    byte[] r = new byte[bb.remaining()];
    bb.get(r);
    return r;
}

From source file:Main.java

private static void receive(Map<Integer, byte[]> map, DatagramSocket socket, int[] length) {
    byte[] b = new byte[limit + 4];
    DatagramPacket p = new DatagramPacket(b, b.length);
    try {//from  www  .  j  a  v  a  2  s .  c om
        socket.receive(p);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    if (p.getLength() != 0) {
        ByteBuffer buf = ByteBuffer.wrap(b);
        int key = buf.getInt();
        byte[] bytes = new byte[p.getLength() - 4];
        length[0] += bytes.length;
        buf.get(bytes);
        map.put(key, bytes);
        receive(map, socket, length);
    }
}

From source file:com.inmobi.messaging.util.AuditUtil.java

private static boolean isValidMagicBytes(ByteBuffer buffer) {
    // compare all 3 magicBytes
    byte[] mBytesRead = new byte[3];
    buffer.get(mBytesRead);
    if (mBytesRead[0] != magicBytes[0] || mBytesRead[1] != magicBytes[1] || mBytesRead[2] != magicBytes[2]) {
        LOG.debug("Invalid magic bytes");
        return false;
    }/*  w ww .  j a  va 2 s .c o m*/
    return true;
}

From source file:cfa.vo.interop.EncodeDoubleArray.java

public static String encodeBase64(double[] data, boolean swapByteOrder) throws IOException {

    byte[] decodedData = doubleToByte(data);

    if (swapByteOrder) {
        ByteBuffer buf = ByteBuffer.wrap(decodedData);
        buf = buf.order(ByteOrder.LITTLE_ENDIAN);
        buf.get(decodedData);
    }/*w  w  w  .ja v a  2 s .  c o  m*/

    Base64 codec = new Base64();
    byte[] encodedData = codec.encode(decodedData);

    String result = new String(encodedData);

    return result;
}

From source file:cfa.vo.interop.EncodeDoubleArray.java

public static double[] decodeBase64(String dataString, boolean swapByteOrder) throws IOException {

    byte[] encodedData = dataString.getBytes();

    Base64 codec = new Base64();
    byte[] decodedData = codec.decode(encodedData);

    if (swapByteOrder) {
        ByteBuffer buf = ByteBuffer.wrap(decodedData);
        buf = buf.order(ByteOrder.LITTLE_ENDIAN);
        buf.get(decodedData);
    }/*ww  w.j  a  v  a 2s .co  m*/

    double[] result = byteToDouble(decodedData);

    return result;
}

From source file:Main.java

/**
 * Returns a new byte array containing the characters of the specified
 * string encoded using the given charset.
 * //from   w  w  w.  j  a v  a 2  s  .  com
 * It is equivalent to <code>input.getBytes(charset)</code> except it has
 * workaround for the bug ID 61917.
 * 
 * @see https://code.google.com/p/android/issues/detail?id=61917
 */
//@formatter:off
/*
 * The original code is available from
 *     https://android.googlesource.com/platform/libcore/+/android-4.4_r1.2/libdvm/src/main/java/java/lang/String.java
 */
//@formatter:on
public static byte[] getBytes(String input, Charset charset) {
    CharBuffer chars = CharBuffer.wrap(input.toCharArray());
    // @formatter:off
    CharsetEncoder encoder = charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
    // @formatter:on
    ByteBuffer buffer;
    buffer = encode(chars.asReadOnlyBuffer(), encoder);
    byte[] bytes = new byte[buffer.limit()];
    buffer.get(bytes);
    return bytes;

}

From source file:Main.java

/**
 * Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted
 * as the length of the buffer.//from   w w  w  .  j a v a 2s  .  c  o m
 * <p>
 * When the method returns, {@code data.position()} will contain the new length of the buffer. If
 * the buffer is not empty it is guaranteed to start with an SPS.
 *
 * @param data Buffer containing start code delimited NAL units.
 */
public static void discardToSps(ByteBuffer data) {
    int length = data.position();
    int consecutiveZeros = 0;
    int offset = 0;
    while (offset + 1 < length) {
        int value = data.get(offset) & 0xFF;
        if (consecutiveZeros == 3) {
            if (value == 1 && (data.get(offset + 1) & 0x1F) == H264_NAL_UNIT_TYPE_SPS) {
                // Copy from this NAL unit onwards to the start of the buffer.
                ByteBuffer offsetData = data.duplicate();
                offsetData.position(offset - 3);
                offsetData.limit(length);
                data.position(0);
                data.put(offsetData);
                return;
            }
        } else if (value == 0) {
            consecutiveZeros++;
        }
        if (value != 0) {
            consecutiveZeros = 0;
        }
        offset++;
    }
    // Empty the buffer if the SPS NAL unit was not found.
    data.clear();
}

From source file:Main.java

/**
 * Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted
 * as the length of the buffer.//ww  w.  j  av  a 2s  .  com
 * <p>
 * When the method returns, {@code data.position()} will contain the new length of the buffer. If
 * the buffer is not empty it is guaranteed to start with an SPS.
 *
 * @param data Buffer containing start code delimited NAL units.
 */
public static void discardToSps(ByteBuffer data) {
    int length = data.position();
    int consecutiveZeros = 0;
    int offset = 0;
    while (offset + 1 < length) {
        int value = data.get(offset) & 0xFF;
        if (consecutiveZeros == 3) {
            if (value == 1 && (data.get(offset + 1) & 0x1F) == NAL_UNIT_TYPE_SPS) {
                // Copy from this NAL unit onwards to the start of the buffer.
                ByteBuffer offsetData = data.duplicate();
                offsetData.position(offset - 3);
                offsetData.limit(length);
                data.position(0);
                data.put(offsetData);
                return;
            }
        } else if (value == 0) {
            consecutiveZeros++;
        }
        if (value != 0) {
            consecutiveZeros = 0;
        }
        offset++;
    }
    // Empty the buffer if the SPS NAL unit was not found.
    data.clear();
}

From source file:it.geosolutions.geostore.core.security.password.SecurityUtils.java

/**
 * Converts a char array to a byte array.
 *//*from   ww  w.ja va2s.c o m*/
public static byte[] toBytes(char[] ch, Charset charset) {
    ByteBuffer buff = charset.encode(CharBuffer.wrap(ch));
    byte[] tmp = new byte[buff.limit()];
    buff.get(tmp);
    return tmp;
}