commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chia-Hung Lin <>
Subject A concept question about streaming api
Date Tue, 27 Apr 2010 17:02:31 GMT
I have an application which uses servletupload streaming api to receive multipart data from

There is no problem in normal scenario. For instance, an user hits a page in the browser and
choose a file; then click submit button uploading the file to the server side. I also have
an unit test program, in which I mock up an HttpServletRequest (derived from testProgressListener()
in ), which is an inputstream containing whole multipart content.
The content may looks as A. 

In the sever side, there is a program whose responsibility is to, 1st, parse the request (as
described in and collect each item (FileItemStream)
for later processing; 2nd, in the stage of processing, a while loop (as B) read the actual
file uploaded and write to an output stream. 

While doing unit test, the error occurs in the 2nd stage where InputStream tries to read data
into the byte array buffer (byte[] buf). The underlying MultipartStream throws MalformedStreamException
with message 'Stream ended unexpectedly;' and the rest of the operation will be omitted. Then
jump to the final block and close the stream. I notice that if commenting out the close statement
(i.e. if(in!=null) in.close();), everything works fine (the rest of operation will be executed).
I check the source code (line 983) and notice it is because in the function
makeAvailable(), if the input stream (actual file uploaded) read to the end and return -1,
then it throws MalformedStreamException("Stream ended unexpectedly"). 

Does that mean I should not close the stream because it is a stream read from socket, which
is opened outside the scope of my program? Or what might be the root cause?

I appreciate any suggestion.
Thank you very much.

Content-Disposition: form-data; name="a"^M
Content-Disposition: form-data; name="b"^M
Content-Disposition: form-data; name="file"; filename="file.txt"^M
Content-Type: text/plain^M
This is a book that is used for unit test!

    InputStream in = ... // the real file (is not form field) uploaded
    ByteArrayOutputStream out = // an outputstrea that will write to storage.
      int read = 0;
      byte[] buf = new byte[1024];
      while(-1!=(, 0, 1024))){
        out.write(buf, 0, read);
      // the rest of operations 
    }catch(IOException ioe){
        if(in!=null) in.close();
        if(out!=null) out.close();
      }catch(Exception e){ e.printStackTrace(); }


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message