Search in sources :

Example 76 with ByteBuffer

use of java.nio.ByteBuffer in project Japid by branaway.

the class StringUtilsTest method testEncode.

@Test
public void testEncode() throws IOException {
    String src = "自由交往";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteBuffer bb = StringUtils.encodeUTF8(src);
    byte[] array = bb.array();
    int p = bb.position();
    assertTrue(p <= array.length);
    baos.write(array, 0, p);
    baos.flush();
    String out = new String(baos.toByteArray(), "UTF-8");
    assertEquals(src, out);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 77 with ByteBuffer

use of java.nio.ByteBuffer in project glide by bumptech.

the class ByteBufferUtil method fromStream.

public static ByteBuffer fromStream(InputStream stream) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(BUFFER_SIZE);
    byte[] buffer = BUFFER_REF.getAndSet(null);
    if (buffer == null) {
        buffer = new byte[BUFFER_SIZE];
    }
    int n = -1;
    while ((n = stream.read(buffer)) >= 0) {
        outStream.write(buffer, 0, n);
    }
    BUFFER_REF.set(buffer);
    byte[] bytes = outStream.toByteArray();
    // Some resource decoders require a direct byte buffer. Prefer allocateDirect() over wrap()
    return (ByteBuffer) ByteBuffer.allocateDirect(bytes.length).put(bytes).position(0);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

Example 78 with ByteBuffer

use of java.nio.ByteBuffer in project platform_frameworks_base by android.

the class AudioEffect method byteArrayToInt.

/**
     * @hide
     */
public static int byteArrayToInt(byte[] valueBuf, int offset) {
    ByteBuffer converter = ByteBuffer.wrap(valueBuf);
    converter.order(ByteOrder.nativeOrder());
    return converter.getInt(offset);
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 79 with ByteBuffer

use of java.nio.ByteBuffer in project platform_frameworks_base by android.

the class AudioEffect method shortToByteArray.

/**
     * @hide
     */
public static byte[] shortToByteArray(short value) {
    ByteBuffer converter = ByteBuffer.allocate(2);
    converter.order(ByteOrder.nativeOrder());
    short sValue = (short) value;
    converter.putShort(sValue);
    return converter.array();
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Example 80 with ByteBuffer

use of java.nio.ByteBuffer in project platform_frameworks_base by android.

the class ByteArrayHelpers method toByteArray.

/**
     * Convert a variadic list of {@code Number}s into a byte array using native endian order.
     *
     * <p>Each {@link Number} must be an instance of a primitive wrapper class
     * (e.g. {@link Integer} is OK, since it wraps {@code int}, but {@code BigInteger} is not.</p>
     *
     * @param numbers variadic list of numeric values
     * @return array converted into byte array using native endian order
     *
     * @throws IllegalArgumentException
     *          if {@code numbers} contained a class that wasn't a primitive wrapper
     */
public static byte[] toByteArray(Number... numbers) {
    if (numbers.length == 0) {
        throw new IllegalArgumentException("too few numbers");
    }
    if (VERBOSE)
        Log.v(TAG, "toByteArray - input: " + Arrays.toString(numbers));
    // Have a large enough capacity to fit in every number as a double
    ByteBuffer byteBuffer = ByteBuffer.allocate(numbers.length * (Double.SIZE / Byte.SIZE)).order(ByteOrder.nativeOrder());
    for (int i = 0; i < numbers.length; ++i) {
        Number value = numbers[i];
        Class<? extends Number> klass = value.getClass();
        if (VERBOSE)
            Log.v(TAG, "toByteArray - number " + i + ", class " + klass);
        if (klass == Integer.class) {
            byteBuffer.putInt((Integer) value);
        } else if (klass == Float.class) {
            byteBuffer.putFloat((Float) value);
        } else if (klass == Double.class) {
            byteBuffer.putDouble((Double) value);
        } else if (klass == Short.class) {
            byteBuffer.putShort((Short) value);
        } else if (klass == Long.class) {
            byteBuffer.putLong((Long) value);
        } else if (klass == Byte.class) {
            byteBuffer.put((Byte) value);
        } else {
            throw new IllegalArgumentException("number class invalid; must be wrapper around primitive class");
        }
    }
    if (VERBOSE)
        Log.v(TAG, "toByteArray - end of loop");
    // Each number written is at least 1 byte, so the position should be at least length
    if (numbers.length != 0 && byteBuffer.position() < numbers.length) {
        throw new AssertionError(String.format("Had %d numbers, but byte buffer position was only %d", numbers.length, byteBuffer.position()));
    }
    byteBuffer.flip();
    byte[] bytes = new byte[byteBuffer.limit()];
    byteBuffer.get(bytes);
    if (VERBOSE)
        Log.v(TAG, "toByteArray - output: " + Arrays.toString(bytes));
    return bytes;
}
Also used : ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)8919 Test (org.junit.Test)1965 IOException (java.io.IOException)1022 ArrayList (java.util.ArrayList)450 File (java.io.File)224 FileChannel (java.nio.channels.FileChannel)214 MappedByteBuffer (java.nio.MappedByteBuffer)196 HashMap (java.util.HashMap)177 CharBuffer (java.nio.CharBuffer)153 ByteArrayOutputStream (java.io.ByteArrayOutputStream)146 InetSocketAddress (java.net.InetSocketAddress)143 Random (java.util.Random)127 InputStream (java.io.InputStream)125 Map (java.util.Map)119 FileInputStream (java.io.FileInputStream)116 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)99 Test (org.testng.annotations.Test)98 IntBuffer (java.nio.IntBuffer)95 SocketChannel (java.nio.channels.SocketChannel)94 FileOutputStream (java.io.FileOutputStream)93