Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

can't use reset on input stream?  RSS feed

 
benjamin muktesh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get the input stream from a zip file uploaded through struts form file, like so:


I am then passing it to my delegate for some processing. The delegate method in turn passes the input stream to two methods like so:



Here is where i run into trouble, reset is not support , i have checked in.markSupported() and it returns false.

is there a way i can reuse the input stream?

I know this(resetting) works for text/csv files but not so(apparently) for zip files.

any pointers would be welcome. Also if anyone can tell me what is the different between inputstream coming from a text/csv and a zip file

Thanks
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benjamin: I know this(resetting) works for text/csv files but not so(apparently) for zip files.

Yeah, ZipInputStream(This is the stream for zip files) does not support mark and reset.

benjamin: Also if anyone can tell me what is the different between inputstream coming from a text/csv and a zip file

Reading/Writing a zip file is a whole lot different that writing a normal file. The reason being that there is a format that every zip files has to follow. ZipInputStream/ZipOutputStream abstract that format and gives the user a more usable API to read write entries.

benjamin: is there a way i can reuse the input stream?

It seems from your description that the readers of the input stream do not discriminate between a ZipInputStream and any other InputStream. The reader always read it byte by byte.
If this is the case then you can try wrapping the underlying stream with a PushbackInputStream.
You can "unread" the read bytes back into this stream.
However, you have to change the read() method implementation a little to make it aware of pushing the read chunk back into the stream.
 
benjamin muktesh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. ill try to work it out.

I found the reason why reset is not working in my case.

If we have a small file then FormFile object is read into a byte array hence we can reset it. But in case of a large file, FormFile object returns a File object which throws an IO exception if we try to reset it. Its pretty well documented in the API , I just overlooked it.

Guess i have to look at implementing it differently.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!