sentence
stringlengths 1
1.38k
| label
stringclasses 3
values |
---|---|
If this would be the only use of the library, it would be overkill to use the library.
|
o
|
On the other hand, determining your own buffer size(s) you can tune your memory/processor usage balance.
|
n
|
How about this?
|
o
|
InputStream in
_/* your InputStream
is
new InputStreamReader(in);StringBuilder sb=new StringBuilder();BufferedReader br
new BufferedReader(is);String read
br.readLine();while(read != null) { //System.out.println(read); sb.append(read); read
sb.toString(); .
|
o
|
Doesn't answer the question.
|
o
|
This shows how to iterate over the lines in a stream, not how to read the entire stream into a string.
|
o
|
you can concat all string in to one String.that is simple.....
|
p
|
The thing is, you're first splitting into lines, and then undoing that.
|
o
|
It's easier and faster to just read arbitrary buffers.
|
p
|
Also, readLine does not distinguish between \n and \r, so you cannot reproduce the exact stream again.
|
o
|
@PauldeVrieze how many lines, and how quickly do you need to process them!?
|
o
|
I would hazard a guess that any performance loss would be small, or could be handled by every once in a while logging them to a file and destroying the old String obj's.
|
o
|
If you are using Google-Collections/Guava you could do the following: CODESNIPPET_JAVA1 .
|
o
|
Note that the second parameter (i.e.
|
o
|
Charsets.UTF_8) for the CODETERM1 isn't necessary, but it is generally a good idea to specify the encoding if you know it (which you should!
|
p
|
) .
|
o
|
@harschware: Given the question was: "If you have java.io.InputStream object how should you process that object and produce a String?
|
o
|
" I assumed that a stream is already present in the situation.
|
o
|
You didn't explain your answer very well, and had extraneous variables; user359996 said the same thing as you, but much more clearly.
|
p
|
it returns to me boxes instead of actual text characters.
|
o
|
plz advise.
|
o
|
+1 for guava, -1 for not specifying the encoding of the input stream.
|
o
|
eg.
|
o
|
new InputStreamReader(stream, "UTF-8").
|
o
|
Downvoting as doesn't close inputstream.
|
n
|
@plasma147 Instead of downvoting, consider editing the example (I just submitted that, adding Closeables.closeQuietly(stream);).
|
n
|
@Chris Noldus On the other hand, some people already have guava in their project, like me, and think this solution is more elegant than the sdk-only version.
|
p
|
CODESNIPPET_JAVA1 .
|
o
|
Works well on Android in comparison with other answers which work only in enterprise java.
|
p
|
That's where it came from... My Android tools library :).
|
o
|
Crashed in Android with OutOfMemory error on the ".write" line, every time, for short strings.
|
n
|
What on earth do you copy...???
|
o
|
I copy 5 - 10 mb files easy...
|
p
|
Please specify the encoding - it's a very common bug.
|
n
|
I've added the encoding.
|
o
|
just as a side note, the original readFully method I have in my code does not return String, it returns byte[] for a more general purpose functionality.
|
o
|
Implementing the new String(...) with encoding is the responsibility of the on that uses the API!.
|
o
|
Here's the most elegant, pure-Java (no library) solution I came up with after some experimentation: CODESNIPPET_JAVA1 .
|
p
|
Isn't there a reader.close() missing?
|
o
|
Ideally with try/finally...
|
o
|
@TorbenKohlmeier, readers and buffers don't need to be closed.
|
o
|
The provided InputStream should be closed by the caller.
|
o
|
Don't forget to mention that there's a more preferable constructor in InputStreamReader that takes a CharSet.
|
p
|
newlines are gone after this.
|
o
|
How about: CODESNIPPET_JAVA1 .
|
o
|
As an alternative to the Commons libraries, Google's excellent URL_http://code.google.com/p/guava-libraries/ [guava-libraries] let you do this fairly concisely; given an _InputStream_ named _inputStream_: CODESNIPPET_JAVA1 .
|
p
|
Note this method is particularly convenient if, instead of an
you have a
|
o
|
For example, if you're getting the body of an HTTP request via
it's just
request.getReader() )**.
|
o
|
Here's more-or-less sampath's answer, cleaned up a bit and represented as a function: CODESNIPPET_JAVA1 .
|
p
|
use a StringBuilder.
|
o
|
Changed to use StringBuilder.
|
o
|
Thanks @trnl.
|
o
|
again no encoding.
|
o
|
I ran some timing tests because time matters, always.
|
o
|
I attempted to get the response into a String 3 different ways.
|
o
|
(shown below) I left out try/catch blocks for the sake readability.
|
o
|
To give context, this is the preceding code for all 3 approaches: CODETERM1 CODETERM2 CODETERM3 CODETERM4 1) CODETERM5 2) CODETERM6 CODETERM7 CODETERM8 CODETERM9 CODETERM10 CODETERM11 CODETERM12 CODETERM13 CODETERM14 3) CODETERM15 CODETERM16 CODETERM17 CODETERM18 So, after running 500 tests on each approach with the same request/response data, here are the numbers.
|
o
|
Once again, these are my findings and your findings may not be exactly the same, but I wrote this to give some indication to others of the efficiency differences of these approaches.
|
o
|
Ranks: Approach
Approach
- 2.6% slower than
Approach
- 4.3% slower than
Any of these approaches is an appropriate solution for grabbing a response and creating a String from it.
|
n
|
2) contains an error, it adds always "null" at the end of the string as you are always makeing one more step then necessary.
|
o
|
Performance will be the same anyway I think.
|
o
|
This should work:String read
null; StringBuffer sb
new StringBuffer();while((read
br.readLine()) != null) { sb.append(read);}.
|
o
|
If you can't use Commons IO (FileUtils/IOUtils/CopyUtils) here's an example using a BufferedReader to read the file line by line: CODESNIPPET_JAVA1 .
|
o
|
or if you want raw speed I'd propose a variation on what Paul de Vrieze suggested (which avoids using a StringWriter (which uses a StringBuffer internally) : CODESNIPPET_JAVA2 .
|
o
|
In order to make your code work, I had to use this.getClass().getClassLoader().getResourceAsStream() (using Eclipse with a maven project).
|
o
|
If you were feeling adventurous, you could mix Scala and Java and end up with this: CODESNIPPET_JAVA1 .
|
o
|
Mixing Java and Scala code and libraries has it's benefits.
|
o
|
See full description here: URL_http://stackoverflow.com/q/5221524/828757 [Idiomatic-way-to-convert-an-InputStream-to-a-String-in-Scala] .
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
Please give a description on what you are trying to accomplish.
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
here s is your InputStream object which will get convert into String :) .
|
o
|
make sure to close the streams at end if you use Stream Readers CODESNIPPET_JAVA1 .
|
o
|
Well you can program it for yourself.. it's not complicated.. CODESNIPPET_JAVA1 .
|
o
|
Since you're using buffer variable locally with no chance of being shared across multiple threads you should consider changing its type to StringBuilder, to avoid the overhead of (useless) synchronization.
|
o
|
That's a good point alex!.
|
p
|
I thing that we both agree that this method isn't thread-safe in many ways.
|
n
|
Even the input stream operations aren't thread-safe.
|
n
|
The below code worked for me.
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
Please note, according to Java docs, the available() method might not work with InputStream but always works with BufferedInputStream.In case you don't want to use available() method we can always use the below code CODESNIPPET_JAVA2 .
|
p
|
I am not sure if there will be any encoding issues.
|
o
|
Please comment, if there will be any issues with the code .
|
o
|
The whole point of using InputStream is, that a) you don't know the length of the _complete_ stream (which bails out anything depending on available) and b) the stream can be anything - a file, a socket, something internal (which bails out anything based on File.size()).
|
o
|
Regarding available: This will cut off data if the stream is longer than the buffer size.
|
o
|
You can use apache commons.In the IOUtils you can find the toString metod with 3 helpfull implementations.
|
p
|
CODESNIPPET_JAVA1 .
|
o
|
JDK 7/8 answer that closes the stream and still throws an IOException: CODESNIPPET_JAVA1 .
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
See the commet by ChristofferHammarstrm in the answer by HarryLime.
|
o
|
First ,you have to know the encoding of string that you want to convert.Because the java.io.InputStream operates an underlying array of bytes,however,a string is composed by a array of character that needs an encoding, e,g.
|
o
|
UTF-8,the JDK will take the default encoding that is taken from System.getProperty("file.encoding","UTF-8"); CODESNIPPET_JAVA1 .
|
o
|
If inputStream's byte array is very big, you could do it in loop.
|
o
|
:EOF .
|
o
|
From the javadocs: Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not.
|
o
|
It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
|
n
|
This is a bad idea!
|
n
|
Don't be burnt by misunderstanding what available() gives you.
|
n
|
As mentioned above, available() is not the way to go.
|
o
|
It's highly recommended to ignore this method so much so that you could treat this to be a restricted API.
|
p
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.