sentence
stringlengths 1
1.38k
| label
stringclasses 3
values |
---|---|
Sorry, after re-reading my comment, it comes off a little arrogant.
|
n
|
I just think it's important to have a good reason to avoid libraries and that the reason is a valid one, which there very well could be :).
|
p
|
isn't it better to close stream/streamreader ?
|
o
|
Yes, I guess the reader should be closed at the end.
|
o
|
@jmort253 Many libraries are large, slow but powerful.
|
n
|
However there are many chances that we need a small, fast one.
|
o
|
In our product, I even replaced many JDK classes with our own implement.
|
o
|
@coolcfan - I do agree with you on that point.
|
o
|
Sometimes speed is of the utmost importance and is a hard requirement.
|
n
|
Many times, good enough is good enough.
|
p
|
I question how often we put effort into optimizing something that no one notices.
|
o
|
The answer of whether the reward is greater than the cost is a decision not to be taken lightly.
|
p
|
Thank you for indicating that there are times when this is necessary.
|
p
|
@jmort253 We noticed performance regression after updating some library in our product for several times.
|
o
|
Luckily we are building and selling our own product so we don't really have the so called deadlines.
|
p
|
Unfortunately we are building a product that is available on many JVMs, databases and app servers on many operation systems so we have to think for the users using poor machines... And a string operation optimizing can improve the perf by 30~40%.
|
n
|
And a fix: In our product, I even replaced should be 'we even replaced'.
|
o
|
@jmort253 If you would already use apache commons I would say, go for it.
|
o
|
At the same time, there is a real cost to using libraries (as the dependency proliferation in many apache java libraries shows).
|
o
|
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.