sentence
stringlengths 1
1.38k
| label
stringclasses 3
values |
---|---|
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
|
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.