Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File length check - Help  RSS feed

 
Mathur Neni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to do check the file length from the passed InputStream. And after that, it has to be passed to another class. But after checking length of inputstream, getting file length as 0.

anything wrong in this method to check the file length:
checkFileLength(InputStream inpStr){
BufferedInputStream bufInput = new BufferedInputStream(inpStr);
int data, len = 0;
try {
do {
data = bufInput.read();
len += data;
} while (data > -1);
bufInput .close();
} catch (Exception e) {
}
}
if (len ........etc.,

}

I think "inpStr" is passed by reference here. Is there any way to copy a inputstream and pass the copy to this checklength method or retain the file content after checking the lenght of the file.

Is there any other ways?

Thanks in advance.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a couple of things wrong here. First, streams are usually one-way conduits. You can't re-read it once you've traversed the data. You can try mark() to set a bookmark and reset() to go back but those methods are optional and are likely not to be implemented. You can also try PushbackInputStream, but that's probably going to be a performance problem if input data are large.
Second, in your code here:

You read a byte and add that value of that byte to a variable "len". Is len supposed to be the size of the input stream or the accumulation of all the bytes in the file?
Last, you close your BufferedInputStream, which will close your InputStream so even if you were able to mark() and reset(), the InputStream would be unusable from that point on.
Why do you need the length of the file stream in the first place?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the java.io.File class has a length() method; can you change your application to call this method at a point where you know the path to the file?
 
Mathur Neni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joe and Ernest for the reply.

Not able to do File.length() - file is dynamically built & InputStream is given as input.

The data is of 3MB max. Can it be considered as a big size to use PushBackInputStream. Yes, it looks like as I am traversing the data and trying to use the same inputstream again, thats why it is showing file length as 0.

This is for checking file size validation and pass on the stream to other classes for further processing.

Any solutions to retaining the inputstream after reading it or to check file length by retaining the file content.

Thanks.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the max is 3mb, just read the stream into memory (provided, of course, you aren't doing too many at once). Have a look at ByteArrayOutputStream. It may be of some use.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when length > 3m? Can you just process it without checking ahead of time and throw an exception or take some action when it hits 3m? How about a FilterInputStream that throws an IO exception on byte 3,000,001?
 
Mathur Neni
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joe, got it solved by using ByteArrayOutputStream. But without making a copy of another inputstream couldn't do it.

But as file size is not bigger (because some env constraints), it is good for this scenario.

Thanks all, sincerely appreciate your inputs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!