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);
}
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);
}
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);
}
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();
}
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;
}
Aggregations