aemon
aemon

Reputation: 789

How to Convert InputStream.read(new byte[1024]); to String?

I would like to get data from InputStream() as a String eg. Hi, Start, Stop, etc.

My Code fragment is

byte[] buffer = new byte[1024];
inputStream.read(buffer);

My data is Command Sent from Bluetooth. The above code fragment only get (eg. 2 if I sent Hi, 5 if sent Start), I would like to get back Normal String as the same from the Sender Side.

I found only converting way from String to InputStream. any suggestion , I would like to appreciate!

Upvotes: 1

Views: 1394

Answers (2)

Bohemian
Bohemian

Reputation: 425278

The simplest way is via the Apache common-io library:

String input = IOUtils.toString(inputStream);

See the javadoc for this method for more.

Upvotes: 1

aemon
aemon

Reputation: 789

finally, i can solve this! inputStream.read(buffer); only return the int num of how many bytes is in the buffer and the data from the socket is stored in buffer. So, from the buffer you can make String eg. String result = new String (buffer);

Upvotes: 1

Related Questions