Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert byte[] to Buffer type

I am working in Android. I need to convert byte[] to Buffer type. In Android, I have seen a type Buffered that I needed to use in particular functions. But, I my data source is type byte[].

like image 780
user1074474 Avatar asked Nov 30 '11 23:11

user1074474


People also ask

Is a buffer a byte array?

The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. It is an array of bytes, often referred to in other languages as a "byte array".

How do you convert a byte array into a string?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.

What is a byte buffer?

A ByteBuffer is a buffer which provides for transferring bytes from a source to a destination. In addition to storage like a buffer array, it also provides abstractions such as current position, limit, capacity, etc. A FileChannel is used for transferring data to and from a file to a ByteBuffer.

Can we convert byte array to file in Java?

Convert byte[] array to File using Java In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file.


1 Answers

Take a look at ByteBuffer.wrap:

byte[] bytes = ...;
Buffer buf = ByteBuffer.wrap(bytes);

There's also a ByteBuffer.wrap(byte[] array, int start, int byteCount) if you only want to wrap part of an array.

like image 197
Brendan Long Avatar answered Oct 19 '22 23:10

Brendan Long