Java Utililty Methods Convert via ByteBuffer

List of utility methods to do Convert via ByteBuffer

Description

The list of methods to do Convert via ByteBuffer are organized into topic(s).

Method

byte[]toByteArray(int[] intArray)
Returns a new byte array using data from the given integer array.
ByteBuffer byteBuffer = ByteBuffer.allocate(intArray.length * 4);
byteBuffer.asIntBuffer().put(intArray);
return byteBuffer.array();
byte[]toByteArray(long[] data)
to Byte Array
ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 8);
LongBuffer longBuffer = byteBuffer.asLongBuffer();
longBuffer.put(data);
return byteBuffer.array();
byte[]ToByteArray(long[] data)
To Byte Array
List<Byte> result = new ArrayList<Byte>();
for (int i = 0; i < data.length; i++) {
    byte[] bs = long2bytes(data[i]);
    for (int j = 0; j < 8; j++) {
        result.add(bs[j]);
while (result.get(result.size() - 1) == SPECIAL_CHAR) {
...
byte[]toByteArray(ReadableByteChannel channel)
to Byte Array
final ByteArrayOutputStream out = new ByteArrayOutputStream();
copy(channel, Channels.newChannel(out));
return out.toByteArray();
byte[]toByteArray(String bits)
to Byte Array
int byteLength = bits.length() / 8;
if (bits.length() % 8 != 0) {
    byteLength += 1;
byte[] bytes = new byte[byteLength];
for (int i = 0; i < bits.length(); i++) {
    setBit(bytes, i, (byte) (bits.charAt(i) == '1' ? 1 : 0));
return bytes;
byte[]ToByteArray(String hexString)
To Byte Array
final String[] hexChunks = hexString.replace(" ", "").split("(?<=\\G..)");
byte[] result = new byte[hexChunks.length];
for (int i = 0; i < hexChunks.length; i++) {
    result[i] = (byte) Integer.parseInt(hexChunks[i], 16);
return result;
byte[]toByteArray(String value)
Encodes the given string in UTF-8.
if (value == null) {
    return null;
try {
    CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder()
            .onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
    ByteBuffer buf = encoder.encode(CharBuffer.wrap(value));
    byte[] res = new byte[buf.remaining()];
...
byte[]toByteArray(UUID uniqueId)
to Byte Array
byte[] bytes = new byte[(Long.SIZE / Byte.SIZE) * 2];
LongBuffer longBuffer = ByteBuffer.wrap(bytes).asLongBuffer();
longBuffer.put(new long[] { uniqueId.getMostSignificantBits(), uniqueId.getLeastSignificantBits() });
return bytes;
byte[]toByteArray2(String filename)
NIO way
File f = new File(filename);
if (!f.exists()) {
    throw new FileNotFoundException(filename);
FileChannel channel = null;
FileInputStream fs = null;
try {
    fs = new FileInputStream(f);
...
byte[]toByteArray3(String filename)
to Byte Array
FileChannel fc = null;
try {
    fc = new RandomAccessFile(filename, "r").getChannel();
    MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).load();
    System.out.println(byteBuffer.isLoaded());
    byte[] result = new byte[(int) fc.size()];
    if (byteBuffer.remaining() > 0) {
        byteBuffer.get(result, 0, byteBuffer.remaining());
...