I am writing a custom archive format in JAVA (J2ME to be precise). The archiver works OK, however I have little problems with de-archiving.
How could I know how many bytes have been read when reading a UTF8 string thorough the readUTF
method? I know that the first two bytes of a string saved using writeUTF are a short value of the string's length, but I'd like a nicer solution.
Or is there any way to know how many bytes are left till the end of the DataInputStream (the available
method doesn't seem to return meaningful values). The stream was opened by a FileConnection, i.e. FileConnection.openDataInputStream
.
Thanks guys!
This may be a bit late, but I've recently been working on something where I needed to know the amount of bytes that have been read, as opposed to how many are left to be read, as this question asks. But you can derive that number from the solution that I've got. I wanted to know the amount of bytes read so I could monitor the download's progress (in case it took a while).
I was initially just using DataInputStream#readFully() to download the files.
But sometimes I was left waiting for anywhere from 50 - 60 seconds for the download to complete, depending on the size. I wanted to get real-time updates on how things were going, so I changed to use just the regular DataInputStream#read(byte[] b, int off, int len). So there's a System.out there at the end telling me whenever I've jumped a percentage point:
To know how many bytes remain to be read, some minor modifications can be made to the last example. Having a tracker variable in place of my percentage, for example:
I can't see a way of achieving this using the CLDC API directly. A simple solution however, would be to roll your own "CountingInputStream" that extends InputStream and counts the number of bytes read. You would basically just need to override the three read-methods.
The file size is available through the FileConnection.
The available() method returns what it is documented to return, which isn't what you are trying to use it for. See the Javadoc.
You could write your own byte-counting FilterInputStream and put it between the data input stream and the underlying stream. But you'll have to get the total byte count of the source from the source, that information isn't available to any input stream.
This is a fairly old question, but I'll post my solution as it might help someone:
There is actually a convenient CountingInputStream wrapper in Apache Commons IO http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/CountingInputStream.html
It's derived from java.io.FilterInputStream so you can basically wrap it around any standard input stream.
In your case, assuming dataInputStream variable denotes the stream you work with: