Is there a class that provides the unbuffered readLine method in Java?

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.

+6
source share
3 answers

I think you should create your own subclass of DataInputStream , which will add the readLine -like method, which behaves the way you need. (You can even override the existing readLine() method.)

Yes, an efficient implementation is nontrivial, but you could avoid a naive implementation if you stack your own class on top of a BufferedInputStream .

+1
source

If the current code base works well, and if your only problem is the deprecation tag, I personally would recommend copying the code from the readLine method of the DataInputStream class and transferring it to the helper / utility class. The readLine Method DataInputStream does not use many instance variables, so with a small amount work you should work well with him. A sample call would look like this: Utils.readLine(dataInStream) . This ensures that even if the method is deleted, your code base will not be affected.

Yes, it’s hacked and yes, it looks a little ugly, but it’s the fastest and probably the safest alternative (minimal changes to the rest of the code base).

+4
source

I had a similar problem and managed to solve it using a BufferedReader with a buffer size of 1.

As a result, the BufferedReader.readLine () method is not loaded.

  InputStreamReader inr=(new InputStreamReader( mInputStream(),"ASCII")); BufferedReader mReader=new BufferedReader(inr,1); 
0
source

Source: https://habr.com/ru/post/919688/


All Articles