I need to use the DataInputStream, since I need the deprecated readLine() functionality and I don't know the exact file format of the input file (i.e. what line ending is used), but also need to read binary encoded primitives.
This is similar to this question:
Is there a class that exposes an unbuffered readLine method in Java?
My suggestion is to use something like this
public class SaveDataInputStream extends DataInputStream {
public SaveDataInputStream(InputStream in) {super(in);}
public String readLineSave() throws IOException {
// ???
}
}
and to use the readLine() method content that can be found in the DataInputStream class (this is similar to the accepted answer in the referred question). However I don't fully understand why the method was deprecated and would prefer to know if it is relevant for my code.
The javadoc says: This method does not properly convert bytes to characters.
But what does that mean? Should I worry about that and what could happen in the worst case? Is it possible to write my own method that fixes the issue (efficiency is not really a concern)?
Hint: new BufferedReader(new InputStreamReader(..)); is not the correct answer...
What they mean when they say readLine() was deprecated because it doesn't properly convert characters is that it does not allow you to specify the character encoding, for instance UTF-8 vs. CP1252. This means that data written using one character encoding would most likely fail if read one a system that defaulted to a different character encoding.
So, do you need to worry about it? Sure. Methods are deprecated to provide a warning to developers that it can possibly go away in the future. That said, according to the JavaDoc, readLine() was deprecated in JDK 1.1, which was a LOOONG time ago.
As to your point of not wanting a BufferedReader because of buffering, I'd say don't use it. Use one of the other classes that extend Reader or, if you want to be that extreme, roll your own. There is nothing stopping you from creating your own class called DataInputReader, tacking on methods to read your primitives, and providing a proper readLine() implementation to suit your needs.
However, if you are reading binary encoded data, I would recommend NOT using a Reader at all, and sticking with a InputStream so you can read raw bytes and handle the conversions yourself. Readers were designed with the handling of character encoding in mind, and as such have a tendency to modify what you are reading under the premise that it is trying to convert binary data to character strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With