Java DataInputStream.readUTF and the number of bytes read

I am writing a custom archive format in JAVA (more precisely, J2ME). The archiver works fine, but I have few problems with archiving.

How could I find out how many bytes were read while reading a UTF8 string, a thorough readUTF method? I know that the first two bytes of a string saved with writeUTF are a short string length value, but I would like to get a nicer solution.

Or is there a way to find out how many bytes are left until the end of a DataInputStream (the available method does not seem to return significant values). The stream was opened using FileConnection, i.e. FileConnection.openDataInputStream .

Thanks guys!

+4
source share
4 answers

I see no way to achieve this using the CLDC API. However, a simple solution would be to run your own "CountingInputStream", which extends the InputStream and counts the number of bytes read. You just need to override the three reading methods.

File size is available through FileConnection.

+4
source

It may be a little late, but I recently worked on something where I needed to know the number of bytes read, as opposed to how much was left to read, as asked by this question. But you can get this number from the solution that I have. I wanted to know the number of bytes read so that I could track the progress of the download (in case it took some time).

At first, I just used DataInputStream # readFully () to load files.

 buffer = new byte[length]; in.readFully(buffer); 

But sometimes I was left to wait from 50 to 60 seconds to complete the download, depending on the size. I wanted to get real-time updates on how everything happens, so I changed to using just the usual DataInputStream # read (byte [] b, int off, int len). So, there System.out is there at the end telling me whenever I jumped a percentage point:

 buffer = new byte[length]; int totalRead = 0; int percentage = 0; while (totalRead < length) { int bytesRead = in.read(buffer, totalRead, (length - totalRead)); if (bytesRead < 0) throw new IOException("Data stream ended prematurely"); totalRead += bytesRead; double progress = ((totalRead*1.0) / length) * 100; if ((int)progress > percentage) { percentage = (int)progress; System.out.println("Downloading: " + percentage + "%"); } } 

To find out how many bytes are left to read, some minor changes can be made to the last example. Having a tracker variable instead of my percentage, for example:

 buffer = new byte[length]; int totalRead = 0; int bytesLeft= 0; while (totalRead < length) { bytesLeft = length - totalRead; int bytesRead = in.read(buffer, totalRead, bytesLeft); if (bytesRead < 0) throw new IOException("Data stream ended prematurely"); totalRead += bytesRead; System.out.println("Bytes left to read: " + bytesLeft); } 
+2
source

The available () method returns what it documents to return, which is not what you are trying to use it for. See Javadoc.

You can write your own FilterInputStream byte counter and place it between the data input stream and the base stream. But you will need to get the total number of source bytes from the source, this information is not available for any input stream.

+1
source

This is a pretty old question, but I will post my solution as it may help someone:

Actually there is a convenient wrapping of CountingInputStream in Apache Commons IO http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/CountingInputStream.html

It is derived from java.io.FilterInputStream, so you can basically wrap it around any standard input stream.

In your case, if the variable dataInputStream indicates the stream you are working with:

 CountingInputStream counter = new CountingInputStream( dataInputStream ); dataInputStream = new DataInputStream( counter ); ... counter.getByteCount() //this returns num of bytes read from dataInputStream 
0
source

All Articles