BufferedInputStream To String Conversion? [duplicate]
Asked Answered
C

5

74

Possible Duplicate:
In Java how do a read/convert an InputStream in to a string?

Hi, I want to convert this BufferedInputStream into my string. How can I do this?

BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream() );
String a= in.read();
Conceptualism answered 19/4, 2011 at 8:52 Comment(0)
R
51
BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream());
byte[] contents = new byte[1024];

int bytesRead = 0;
String strFileContents; 
while((bytesRead = in.read(contents)) != -1) { 
    strFileContents += new String(contents, 0, bytesRead);              
}

System.out.print(strFileContents);
Rapeseed answered 19/4, 2011 at 9:0 Comment(3)
one small bug. in the while loop you should be appending with each iteration. it should be += instead of =. ie: strFileContents += new String(contents, 0, bytesRead);Hyperbole
@Hyperbole that isnt the only bug, this code relies on chance that the string encoding has a boundary at the bytesRead mark (which is an ok assumption ONLY for ASCII).Bonni
I had to put 'System.out.print(strFileContents);' inside loop else only last piece of my html response was shown. btw thanksStationmaster
G
37

With Guava:

new String(ByteStreams.toByteArray(inputStream),Charsets.UTF_8);

With Commons / IO:

IOUtils.toString(inputStream, "UTF-8")
Gewgaw answered 19/4, 2011 at 9:3 Comment(0)
C
19

I suggest you use apache commons IOUtils

String text = IOUtils.toString(sktClient.getInputStream());
Clingy answered 19/4, 2011 at 9:4 Comment(0)
R
11

Please following code

Let me know the results

public String convertStreamToString(InputStream is)
                throws IOException {
            /*
             * To convert the InputStream to String we use the
             * Reader.read(char[] buffer) method. We iterate until the
    35.         * Reader return -1 which means there's no more data to
    36.         * read. We use the StringWriter class to produce the string.
    37.         */
            if (is != null) {
                Writer writer = new StringWriter();

                char[] buffer = new char[1024];
                try
                {
                    Reader reader = new BufferedReader(
                            new InputStreamReader(is, "UTF-8"));
                    int n;
                    while ((n = reader.read(buffer)) != -1) 
                    {
                        writer.write(buffer, 0, n);
                    }
                }
                finally 
                {
                    is.close();
                }
                return writer.toString();
            } else {       
                return "";
            }
        }

Thanks, Kariyachan

Redintegration answered 19/4, 2011 at 8:59 Comment(2)
No casting needed. BufferedInputStream is an InputStreamGewgaw
"Thanks, Kariyachan" I remember that cat from "Man from U.N.C.L.E." - he's a programmer now?Most
W
6

If you don't want to write it all by yourself (and you shouldn't really) - use a library that does that for you.

Apache commons-io does just that.

Use IOUtils.toString(InputStream), or IOUtils.readLines(InputStream) if you want finer control.

Wan answered 19/4, 2011 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.