Get an OutputStream into a String?

I would use a ByteArrayOutputStream. And on finish you can call.

I would use a ByteArrayOutputStream. And on finish you can call: new String( baos.toByteArray(), codepage ); or better baos. ToString( codepage ).

2 ByteArrayOutputStream has no toArray() method; it does have toByteArray() though. Can you fix the answer? Also, why not use baos.

ToString(String charsetName) which would be slightly simpler. – Jonik Jun 7 '09 at 15:29 Thanks for the tip with the toString(charset). I have never look on the toString because I have expect a ByteArrayOutputStream@123456.

I have also correct the mistake with toByteArray(). – Horcrux7 Jun 7 '09 at 18:41 Also note that some codepages are not installed unless you explicitly ask for them with a custom install. – Thorbjørn Ravn Andersen Jun 7 '09 at 21:14 can return simply baos.toString(), what is the coepage thing for?

– Tom Brito May 26 '10 at 13:42 1 A bytearray is just binary data. As (unicode) text can be encoded binary in many different ways, the ByteArrayOutputStream needs to know what encoding was used to encode the bytes, so it can use the same encoding to decode the bytes to a string again. Simply using toString without an argument is not wise as you just ignore the problem instead of tackling it; Java will use the platform encoding which could be correct...or not.It's random basically.

You need to find out what encoding was used to write the text to bytes and pass that encoding to toString. – Stijn de Witt Feb 10 '11 at 13:46.

I like the Apache Commons IO library. Take a look at its version of ByteArrayOutputStream, which has a toString(String enc) method as well as toByteArray() . Using existing and trusted components like the Commons project lets your code be smaller and easier to extend and repurpose.

Good luck.

4 Save yourself a year of your life and read through all the common's APIs so when you encounter a problem, you can unleash a fully tested and community owned solution. – Bob Herrmann Oct 20 '08 at 0:53 2 Hmm, I'm an avid Apache Commons user, but in this case I fail to see why you should use Commons IO's ByteArrayOutputStream instead of JDK's own java.io. ByteArrayOutputStream.

The latter also provides toString(String charsetName) and toByteArray() methods. Care to elaborate? – Jonik Jun 7 '09 at 15:33 Yeah, since the original context was a better way to stream and extract content, I included the Commons IO example since it included a 'write(InputStream)' method for a then-undefined/questionable mechanism for populating the OutputStream.

I'd go with the JDK, too. – Joe Liversedge Jun 8 '09 at 18:21.

This worked nicely OutputStream output = new OutputStream() { private StringBuilder string = new StringBuilder(); @Override public void write(int b) throws IOException { this.string. Append((char) be ); } //Netbeans IDE automatically overrides this toString() public String toString(){ return this.string.toString(); } }; method call =>> marshaller. Marshal( (Object) toWrite , (OutputStream) output); then to print the string or get it just reference the "output" stream itself As an example, to print the string out to console =>> System.out.

Println(output); FYI: my method call marshaller. Marshal(Object,Outputstream) is for working with xml. It is irrelevant to this topic.

This is higly wasteful for productional use, there are way to many conversion and it is a bit loose. This was just coded to prove to you that it is totally possible to create a custom OuputStream and output a string. But just go Horcrux7 way and all is good with merely two method calls.

And the world lives on another day....

1 Just casting a byte to char will only work on ascii. Use ByteArrayOutputStream like Horcrux7 – Dave Ray Oct 11 '09 at 22:45.

Adrian : More exactly 'os' is an ByteArrayOutputStream as OutputStream doesn't have a method toByteArray(); I don't know how to reply just under your response to Synox, so I just added an new answer.

Thanks, I've clarified the answer. You probably couldn't add a comment due to low rep. – Adrian Mouat Sep 22 '10 at 16:30.

Here's what I ended up doing: Obj. WriteToStream(toWrite, os); try { String out = new String(os.toByteArray(), "UTF-8"); assertTrue(out. Contains("testString")); } catch (UnsupportedEncondingException e) { fail("Caught exception: " + e.getMessage()); } Where os is a ByteArrayOutputStream.

From what you describe I would suggest having a look at java.io.StringWriter. It collects in a StringBuffer which you can then convert to a String when you need it. java.sun.com/j2se/1.5.0/docs/api/java/io....

2 Writers require char data, not bytes as the question has, which would require some intermediate decoding. – McDowell Jun 7 '09 at 19:40 1 You are right - somehow I mixed Writers/Streams up while checking. For OutputStreams the usual trick is to use a ByteArrayOutputStream.

– Thorbjørn Ravn Andersen Jun 7 '09 at 21:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions