You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2010/04/28 12:59:33 UTC

svn commit: r938877 - in /james/imap/trunk/store/src/main/java/org/apache/james/imap/store: AbstractRewindableInputStream.java InputStreamContent.java RewindableInputStream.java

Author: norman
Date: Wed Apr 28 10:59:33 2010
New Revision: 938877

URL: http://svn.apache.org/viewvc?rev=938877&view=rev
Log:
Make sure we dispose temporary files after finish processing (IMAP-135)

Modified:
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/AbstractRewindableInputStream.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/InputStreamContent.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/RewindableInputStream.java

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/AbstractRewindableInputStream.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/AbstractRewindableInputStream.java?rev=938877&r1=938876&r2=938877&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/AbstractRewindableInputStream.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/AbstractRewindableInputStream.java Wed Apr 28 10:59:33 2010
@@ -1,17 +1,37 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
 package org.apache.james.imap.store;
 
-import java.io.FilterInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
 /**
- * {@link FilterInputStream} which support the get rewinded. This is done by copy over every byte
+ * {@link RewindableInputStream} which support the get rewinded. This is done by copy over every byte
  * over to another {@link OutputStream}. What {@link OutputStream} to use is up to the implementations.
  * 
  * The rewinding will get delayed as long as possible. So if you call
  * rewind, it will only get performed when needed
  * 
+ * Be sure to call {@link #close()} once you done reading from the object. This will
+ * remove all temporary data
+ * 
  *
  */
 public abstract class AbstractRewindableInputStream extends RewindableInputStream{
@@ -22,15 +42,41 @@ public abstract class AbstractRewindable
         super(in);
     }
 
+    /**
+     * Return the OutputStream to which the data should get written when they are read the 
+     * first time
+     * 
+     * @return output
+     * @throws IOException
+     */
     protected abstract OutputStream getRewindOutputStream() throws IOException;
     
+    /**
+     * Return the InputStream which should get used after the stream was rewinded
+     * 
+     * @return rewindInput
+     * @throws IOException
+     */
     protected abstract InputStream getRewindInputStream() throws IOException;
 
+    /**
+     * Dispose all temporary data
+     * 
+     * @throws IOException
+     */
     protected abstract void dispose() throws IOException;
     
+    /**
+     * Get called after the rewind was complete
+     * 
+     * @throws IOException
+     */
     protected abstract void afterRewindComplete() throws IOException;
     
-    @Override
+    /**
+     * Close the stream and dispose all temporary data
+     * 
+     */
     public void close() throws IOException {
         try {
             in.close();
@@ -48,7 +94,9 @@ public abstract class AbstractRewindable
     }
 
     
-    
+    /**
+     * Read data and write and store it for later usage once the rewind was done
+     */
     @Override
     public int read() throws IOException {   
         int i;
@@ -70,6 +118,9 @@ public abstract class AbstractRewindable
         return i;
     }
 
+    /**
+     * Read data and write and store it for later usage once the rewind was done
+     */
     @Override
     public int read(byte[] b, int off, int len) throws IOException {
         if (len == 0) {
@@ -92,6 +143,9 @@ public abstract class AbstractRewindable
         return i;
     }
 
+    /**
+     * Read data and write and store it for later usage once the rewind was done
+     */
     @Override
     public void rewindIfNeeded() throws IOException {
         if (needsRewind()) {
@@ -100,7 +154,7 @@ public abstract class AbstractRewindable
             if (end == false) {
                 while ( read() != -1);
             }
-            // we don't need the original inputstream anymore so close it
+            // we don't need the original InputStream anymore so close it
             in.close();
             afterRewindComplete();
         }        

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/InputStreamContent.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/InputStreamContent.java?rev=938877&r1=938876&r2=938877&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/InputStreamContent.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/InputStreamContent.java Wed Apr 28 10:59:33 2010
@@ -53,19 +53,27 @@ public final class InputStreamContent im
      */
     public void writeTo(WritableByteChannel channel) throws IOException {
         
-        // rewind the stream before write it to the channel
-        in.rewind();
+        try {
+            // rewind the stream before write it to the channel
+            in.rewind();
+        
+            // read all the content of the underlying InputStream in 16384 byte chunks, wrap them
+            // in a ByteBuffer and finally write the Buffer to the channel
+            byte[] buf = new byte[16384];
+            int i = 0;
+            while ((i = in.read(buf)) != -1) {
+                ByteBuffer buffer = ByteBuffer.wrap(buf);
+                // set the limit of the buffer to the returned bytes
+                    buffer.limit(i);
+                    channel.write(buffer);
+            }
+        } finally {
+            // close the stream so temporary files could get delete
+            in.close();
+        }
         
-        // read all the content of the underlying InputStream in 16384 byte chunks, wrap them
-        // in a ByteBuffer and finally write the Buffer to the channel
-        byte[] buf = new byte[16384];
-        int i = 0;
-        while ((i = in.read(buf)) != -1) {
-            ByteBuffer buffer = ByteBuffer.wrap(buf);
-            // set the limit of the buffer to the returned bytes
-            buffer.limit(i);
-            channel.write(buffer);
-        }  
     }
+    
+   
 
 }

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/RewindableInputStream.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/RewindableInputStream.java?rev=938877&r1=938876&r2=938877&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/RewindableInputStream.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/RewindableInputStream.java Wed Apr 28 10:59:33 2010
@@ -28,6 +28,9 @@ import java.io.InputStream;
  * The rewinding will get delayed as long as possible. So if you call
  * rewind, it will only get performed when needed
  * 
+ * Be sure to call {@link #close()} to cleanup temporary data when you 
+ * are done with reading from the stream
+ * 
  *
  */
 public abstract class RewindableInputStream extends FilterInputStream{



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org