GeeCON Prague 2014*
The moose likes I/O and Streams and the fly likes Reading file: ASCII or Binary? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading file: ASCII or Binary?" Watch "Reading file: ASCII or Binary?" New topic
Author

Reading file: ASCII or Binary?

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Hi guys,
When I read files using JSP from the server (images and texts). Is there a difference if read in ASCII or Binary mode?

thanks,
Alex


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
ASCII and binary will both be treated differently, yes, so you should use the one you mean. Binary streams for text is usually harmless, but text streams for binary just doesn't work.
Be aware that the code you posted isn't designed in particular to read in either ASCII or binary -- it will decode files according to your default character encoding, which is likely to be (but isn't necessarily) a superset of ASCII. To get a pure ASCII BufferedReader for a file, you'd write something like this:

If by ASCII you simply mean text then the default character encoding should suffice.
Trent DiBacco
Greenhorn

Joined: May 24, 2002
Posts: 18
Just as a performance note:
You create more object then necessary.

This is how you might correct it

Just my two cents worth.
[ January 11, 2004: Message edited by: Trent DiBacco ]
[ January 11, 2004: Message edited by: Trent DiBacco ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
To amplify David's answer to the original question - the code shown is definitely a Bad Idea™ for reading binary data such as an image file. First - why would you ever want to treat an image as a String? Second, even if you do getBytes() or something like that later on to convert the String back to something useful - you risk corrupting the data. Most of the time you may observe no problems, but every so often, things will be screwed up. In part this will depend on your platform's default encoding (unless you explicitly provide an encoding, which is almost always a good idea for text data that came from some other machine).
But there's also a problem with BufferedReader's readLine(), because it can split lines on several different types of line separators - "\r", "\n", or "\r\n". You don't know which kind was provided, and your code is replacing them all with "\n". That's fine for text data, because those different line separators were probably intended to mean the same thing anyway. But for binary data, a "\r\n" probably wasn't intended to be a line separator at all. It was two consecutive bytes 0x0D0A, which just happen to look like a line feed to a Reader. But when you replace "\r\n" (0x0D0A, two bytes) with "\n" (0x0A, one byte) you're subtly changing the data. Perhaps not so subtly, since the position of all subsequent bytes will shift by one, which could produce massive errors from some file formats.
So, as David said - don't treat binary data as if it were text; it isn't, and you can cause serious problems doing this. Even if it's not apparently a problem most of the time.
I agree with Trent too; I'm just less concerned with performance than I am about data corruption, in most cases.


"I'm not back." - Bill Harding, Twister
 
GeeCON Prague 2014
 
subject: Reading file: ASCII or Binary?