sentence
stringlengths 1
1.38k
| label
stringclasses 3
values |
---|---|
Boy, I'm absolutely in love with Java, but this question comes up so often you'd think they'd just figure out that the chaining of streams is somewhat difficult and either make helpers to create various combinations or rethink the whole thing.
|
n
|
You are right.
|
o
|
I tend to use a set of helper classes that do it once for me, so I don't need keep referring to Google or even StackOverflow for the answer.
|
o
|
In this case, I was away from my utility code and couldn't remember exactly how to do it.
|
o
|
What better way to open my account on the site.
|
o
|
Yeah, it really shouldn't require so much boilerplate to do something as simple as read strings from a stream.
|
n
|
It's not that difficult, just annoying.
|
n
|
@Adam: It really depends on what kind of Stream you're working with.
|
p
|
For instance, System.console().readLine() (new in Java 6) is pretty easy.
|
p
|
Same with BufferedReader's readLine().
|
o
|
The only hard part is when you don't know how many characters you need to read.
|
n
|
The answers to this question only work if you want to read the stream's contents _fully_ (until it is closed).
|
o
|
Since that is not always intended (http requests with a keep-alive connection won't be closed), these method calls block (not giving you the contents).
|
o
|
You
to know and specify the character encoding for the stream, or you
have character encoding bugs, since you will be using a randomly chosen encoding depending on which machine/operating system/platform or version thereof your code is run on.
|
n
|
That is, do
use methods that depend on the platform default encoding.
|
o
|
I don't really know Java, but "InputStream
" seems wrong.
|
n
|
@Hello71: It is ok in Java, since the instanceof operator is spelled differently.
|
o
|
@Ronald: Erm... never mind that.
|
o
|
I must have been partially asleep while writing that.
|
o
|
Here's a way using only standard Java library (note that the stream is not closed, YMMV).
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
I learned this trick from URL_http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html ["Stupid-Scanner-tricks"] article.
|
o
|
The reason it works is because URL_http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html [Scanner] iterates over tokens in the stream, and in this case we separate tokens using "beginning of the input boundary" (\A) thus giving us only one token for the entire contents of the stream.
|
p
|
if you need to be specific about the input stream's encoding, you can provide the second argument to CODETERM1 constructor that indicates what charset to use (e.g.
|
o
|
"UTF-8").
|
o
|
Hat tip goes also to URL_http://stackoverflow.com/users/68127/jacob-gabrielson [Jacob,] who once pointed me to the said article.
|
o
|
Thanks to a suggestion from URL_http://stackoverflow.com/users/101272/patrick [Patrick] , made the function more robust when handling an empty input stream.
|
p
|
more edit:** nixed try/catch, Patrick's way is more laconic.
|
n
|
Thanks, for my version of this I added a finally block that closes the input stream, so the user doesn't have to since you've finished reading the input.
|
o
|
Simplifies the caller code considerably.
|
o
|
I think this should be public
String convertStreamToString .
|
o
|
@PavelRepin @Patrick in my case, an empty inputStream caused a NPE during Scanner construction.
|
o
|
I had to add if (is
null) return ""; right at the beginning of the method; I believe this answer needs to be updated to better handle null inputStreams.
|
o
|
The problem with this approach I find is it does not handle CR/LF translations too well.
|
p
|
So you have to make sure your line endings are consistent.
|
o
|
@ArchimedesTrajano does IOUtils.copy(inputStream, writer, encoding) deal with CR/LF translations better?
|
o
|
I think CR/LF consistency is entirely unrelated issue.
|
o
|
Not saying it isn't an issue.
|
o
|
For Java 7 you can close in a try-with: try(java.util.Scanner s
new java.util.Scanner(is)) { return s.useDelimiter("\\\A").hasNext() ?
|
o
|
s.next() : ""; }.
|
o
|
Unfortunately this solution seems to go and lose the exceptions thrown in my underlying stream implementation.
|
o
|
excellent trick!
|
p
|
any ideas about performance of Scanner vs reading the stream in a more verbose way?
|
o
|
@Igal I didn't measure it.
|
o
|
If you do, gist it and I'll append your results to the answer.
|
o
|
A nice way to do this is using URL_http://commons.apache.org/ [Apache-commons] URL_http://commons.apache.org/proper/commons- io/apidocs/org/apache/commons/io/IOUtils.html [IOUtils] to copy the CODETERM1 into a CODETERM2 ... something like CODESNIPPET_JAVA1 .
|
p
|
Alternatively, you could use CODETERM3 if you don't want to mix your Streams and Writers .
|
o
|
I hope IOUtils takes an optional Charset (or at least the name of the encodding to use).
|
o
|
Best not to leave this kind of thing to chance :).
|
p
|
Haha - of course it does!
|
o
|
URL_http://commons.apache.org/io/apidocs/org/apache/comm ons/io/IOUtils.html#copy(java.io.InputStream,%20java.io.Writer,%20java.lang.St ring).
|
o
|
Why copy the inputstream to a writer?
|
o
|
Why not just use a reader?
|
o
|
Or read all bytes and use new String(bytes, charset)?
|
o
|
You really,
should be using IOUtils.copy(inputStream, writer, encoding); unless you really,
know what you're doing, which people never do with character encoding related programming.
|
o
|
Or in this case, IOUtils.toString(inputStream, encoding).
|
o
|
Methods that use the platform default encoding are almost never correct to use, like every other method that gives different results depending on which machine/operating system/platform or version thereof it is run on.
|
n
|
I think the below answer (IOUtils.toString()) is simpler since there is no need for a StringWriter.
|
p
|
A quick but not clean solution.
|
p
|
I would rather prefer a solution without adding additional libraries as mentioned in the post below: URL_http://stackoverflow.com/a/6938341/411951 .
|
p
|
Only works for closes streams, I believe.
|
p
|
Apache Commons allows: CODESNIPPET_JAVA1 .
|
o
|
Of course, you could choose other character encodings besides UTF-8.
|
o
|
Also see: ( URL_http://commons.apache.org/proper/commons-io/javadocs/api-2.4/o rg/apache/commons/io/IOUtils.html#toString%28java.io.InputStream,%20java.lang.
|
o
|
String [Docs] ) .
|
o
|
IOUtils.toString is deprecated.
|
n
|
No it isn't.
|
o
|
Only the version that takes a byte[] parameter is.
|
o
|
Also, there is a method that only take a inputStream argument, if you are find with the default encoding.
|
o
|
@Guillaume Cot I guess the message here is that you never should be "fine with the default encoding", since you cannot be sure of what it is, depending on the platform the java code is run on.
|
o
|
@Per Wiklander I disagree with you.
|
o
|
Code that is going to work on a single could be quite sure that default encoding will be fine.
|
o
|
For code that only open local file, it is a reasonable option to ask them to be encoded in the platform default encoding.
|
o
|
This function is corrupting my input stream.
|
n
|
To save anyone the hassle of Googling - org.apache.commons commons-io 1.3.2 .
|
o
|
I don't know that I'd say the default encoding is NEVER fine.
|
o
|
On the other hand, I disagree with Guillaume also.
|
o
|
In fact, it's not a matter of fine or not; it's a matter of which is CORRECT.
|
p
|
In the situation where you want to take something that you know is encoded in the default encoding for the platform, then the default version is correct.
|
p
|
However, I believe this is a very rare use case.
|
o
|
The more normal case is that you need to specify what the encoding is, so specifying the encoding is correct.
|
p
|
There is no optional case: either one or (more likely) the other is correct.
|
p
|
Also little improvement would be to use apache io (or other) constant for character encoding instead of using plain string literal - eg: IOUtils.toString(myInputStream, Charsets.UTF_8);.
|
n
|
Taking into account file one should first get a CODETERM1 instance.
|
o
|
This can then be read and added to a CODETERM2 (we don't need CODETERM3 if we are not accessing it in multiple threads, and CODETERM4 is faster).
|
p
|
The trick here is that we work in blocks, and as such don't need other buffering streams.
|
o
|
The block size is parameterized for run-time performance optimization.
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
I like how this works without adding more libraries to your project.
|
p
|
is there a chance to broke multi-bytes character in this solution?
|
o
|
This solution uses multibyte characters.
|
o
|
The example uses the UTF-8 encoding that allows expression of the full unicode range (Including Chinese).
|
o
|
Replacing "UTF-8" with another encoding would allow that encoding to be used.
|
o
|
@User1 - I like using libraries in my code so I can get my job done faster.
|
p
|
It's awesome when your managers say "Wow James!
|
p
|
How did you get that done so fast?!".
|
o
|
But when we have to spend time reinventing the wheel just because we have misplaced ideas about including a common, reusable, tried and tested utility, we're giving up time we could be spending furthering our project's goals.
|
p
|
When we reinvent the wheel, we work twice as hard yet get to the finish line much later.
|
o
|
Once we're at the finish line, there is no one there to congratulate us.
|
p
|
When building a house, don't build the hammer too.
|
o
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.