Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

jdavidb
  (email not shown publicly)
http://voiceofjohn.blogspot.com/

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Friday February 08, 2008
11:48 AM

Java gem^W workaround of the day: Stream to String

[ #35615 ]

Yesterday I lamented that far too many Java APIs put useful data into a stream but fail to offer me an accessor method to obtain the data as a String. Today I know how to get that data as a String.

The secret is the ByteArrayOutputStream class. You "print" to this stream, then call toString() on it to get the String. Or you can call toByteArray() if you need that for some reason.

This looks like a painful extra step to my normal way of thinking, but I can see that a case might somehow be made that it is more flexible design, since the mechanism exists to take any method that outputs to streams and obtain the data in a String. I guess having methods that are inappropriately coupled to streams seems convenient when it's so inconvenient to access streams.

All I wanted to do yesterday was send the little chunk of XML I'd manipulated to log4j so I could see it in a test run and make sure it was right without having to write it to a file and go open the file to look at it. I think logging is a perfect example of why there ought to be accessors like this for every piece of data, rather than methods that force you to output.

Minimize side effects!

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • The problem with doing things like this is that you are completely ignoring the encoding. Encoding is one of the reason why there are parallel IO hierarchies in Java: InputStream [sun.com]/OutputStream [sun.com] vs Reader [sun.com]/Writer [sun.com]. The latter understand characters, the former understand bytes. Unfortunately, every time you go near a Reader/Writer, you need to specify a character encoding. As a rule, I try to prefer Reader/Writer where I can.

    Classes to look at:

    • The commons io [apache.org] library has implementations for useful stuff like this -- see IOUtils#toString() for a stream (with default encoding or with a specified one).
      • Thanks for the pointer!

        --
        J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
    • Thank you for the useful information!

      For the record, ByteArrayOutputStream.toString() does offer a version which specifies the character encoding as a parameter. But that's definitely something I wasn't thinking about (especially since I just wanted it for logging). My personal wiring is probably far too close to Paul Graham's recent unforgivable sin in making Arc ASCII-only for the taste of people around here, or anyone steeped in Java. It's something I'd prefer to leave as Somebody Else's Problem as

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
      • As to thinking about encodings, I'm completely in agreement with you — everything should use UTF-8!
  • …Scheme:

    (let ((str (with-output-to-string
                 (write exprs))))
      (do-something-to str))
    --
    Ordinary morality is for ordinary people. -- Aleister Crowley
    • Or IO::Scalar in Perl. :)

      --
      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
      • You no longer need IO::Scalar since perl 5.8. Plain old open will now accept a scalar ref:

          my $str
          open my $fh, '>', \$str;
        • Cool! Thank you!

          --
          J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers