• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to convert a Bytearray input stream to String

 
venkata satya prasad Kolla
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have to convert a ByteArryinputString to a string and then decode the string to ByteArrayInputStream.

here is the code i am using to convert the Byte array input stream to String.

generatedInputStream is of type ByteArrayInputStream:

if(generatedInputStream != null){
byte[] buffer = new byte[generatedInputStream.available()];
int bytesRead;
try {
bytesRead = generatedInputStream.read(buffer);
while (bytesRead >= 0)
{
generatedOutputStream.write(buffer, 0, bytesRead);
bytesRead = generatedInputStream.read(buffer);
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new SSPApplicationException();
}
}
if(generatedOutputStream.size() > 0){
generatedDoc = new String(generatedOutputStream.toByteArray());
}


String to ByteArrayInputStream:

byte generatedDocBytes[] = generatedDoc.getBytes();
ByteArrayInputStream generatedDocBAIS = new ByteArrayInputStream(generatedDocBytes);

The issue is the two attributes generatedInputStream and generatedDocBAIS are not similar. Their size is not the same. In the transformation i lost some bytes. Can some one suggest me how to convert a ByteArrayInputStream type to String and then convert the String to a ByteArrayInputStream
 
James Jiang
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,venkata satya:
you can do it like this:
BufferedReader br=new BufferedReader(new InputStreamReader(generatedInputStream));
String s=br.readLine();
i wish it helps you.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also create a ByteArrayOutputStream from the ByteArrayInputStream, and then use the toString() method to create the String equivalent.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Venkata: you might want to read Available Doesn't Do What You Think It Does. Roger's suggestion is a good way to go about this.

[James Jiang]: Hey,venkata satya:
you can do it like this:
BufferedReader br=new BufferedReader(new InputStreamReader(generatedInputStream));
String s=br.readLine();
i wish it helps you.


The problem with this is that it reads only up to and including the first line separator. It assumes that the string you're looking for does not contain any line separators. Often that's true, but if not, this won't really work.

A similar but better method is:
 
Vilmantas Baranauskas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not forget about encodings. Bytes are just bytes, they are not characters. You need an encoding to convert them to characters.

E.g. use "new String(bytes, encoding)" instead of "new String(bytes)".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!