Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

how to convert a Bytearray input stream to String

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)".
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic