I was implementing a web service to upload files (from 120 Kb to 1 Mb). Most of the time everything worked fine, but sometimes (randomly) the uploaded file appeared to be empty (size == 0) in the target platform.
I’ve tested CXF 2.5 and 2.6 – same behavior. I created the following Jira issue : https://issues.apache.org/jira/browse/CXF-4356 After some debugging I found that the problem came from the temp file created by the CachedOutputStream that was sometimes deleted during the process of getting the cached stream. By digging a bit more, I also found that the problem occurred when the created FileInputStream was collected by the GC before the last call to CachedOutputStream#getInputStream() for getting the stream. As the #close() method of the created FileInputStream was overridden to call CachedOutputStream#maybeDeleteTempFile() to delete the temp file and that the #close() method is called within the FileInputStream#finalize() method I understood why I got the empty stream. What I didn’t understand is why the created FileInputStream is sometimes (!) garbage collected prematurely? Has anyone else experienced the same problem? I found a workaround by keeping an instance of the created FileInputStream in CachedOutputStream#getInputStream() within the CachedOutputStream to prevent the GC to collect it too early and therefore to delete the need temp file. However the temp file is still deleted properly. Here is the code I changed in CachedOutputStream class : private FileInputStream fileInputStream; // <-- The instance we keep public InputStream getInputStream() throws IOException { flush(); if (inmem) { if (currentStream instanceof LoadingByteArrayOutputStream) { return ((LoadingByteArrayOutputStream) currentStream).createInputStream(); } else if (currentStream instanceof ByteArrayOutputStream) { return new ByteArrayInputStream(((ByteArrayOutputStream) currentStream).toByteArray()); } else if (currentStream instanceof PipedOutputStream) { return new PipedInputStream((PipedOutputStream) currentStream); } else { return null; } } else { try { fileInputStream = new FileInputStream(tempFile) { // <-- The instance we keep public void close() throws IOException { super.close(); maybeDeleteTempFile(this); } }; streamList.add(fileInputStream); return fileInputStream; } catch (FileNotFoundException e) { throw new IOException("Cached file was deleted, " + e.toString()); } } } -- View this message in context: http://cxf.547215.n5.nabble.com/MTOM-attachment-not-transferred-properly-empty-stream-tp5709170.html Sent from the cxf-user mailing list archive at Nabble.com.
