I clear some fragments of our code base at work, and one of the high classes is used to read and write data. This data is a mixture of US-ASCII encoded strings and binary encoded primitives.
The current implementation uses a DataInputStream , but, as you can see in the documentation, the readLine()
method is deprecated due to a problem with converting bytes to characters. Although this encoding problem did not appear for us, the obsolescence problem is a problem because it no longer works with any version of OpenJDK 7, and obsolescence means that it can be completely removed in the future. The βofficialβ alternative is to use readLine
from BufferedReader , but we cannot do a full swap with DataInputStream, because BufferedReader cannot handle binary encoded primitives.
The problem with mixing these two classes is that when the BufferedReader buffers the stream, it advances the stream marker. This means that subsequent calls to methods like readDouble()
from the DataInputStream will not be made using IOExceptions or EOFExceptions, since the actual location of the stream marker is not where it should be in the context of the application logic.
I looked at some hacking strategy mark()
/ reset()
, but sometimes the stream is supported by FileInputStream, which does not support mark()
/ reset()
.
With the exception of changing our data protocol to write primitives as characters or write my own implementation of readLine()
(which is surprisingly non-trivial), is there any way to achieve this? I would even want to consider an external library at this point.
source share