You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2014/07/01 11:35:51 UTC

svn commit: r1607026 - in /manifoldcf/trunk: connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/jdbc/ framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/

Author: kwright
Date: Tue Jul  1 09:35:51 2014
New Revision: 1607026

URL: http://svn.apache.org/r1607026
Log:
Rework TempFileInput and TempFileCharacterInput classes to separate IO exceptions for reading input stream vs. dealing with temporary file

Modified:
    manifoldcf/trunk/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/jdbc/JDBCConnection.java
    manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/TempFileCharacterInput.java
    manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/TempFileInput.java

Modified: manifoldcf/trunk/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/jdbc/JDBCConnection.java
URL: http://svn.apache.org/viewvc/manifoldcf/trunk/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/jdbc/JDBCConnection.java?rev=1607026&r1=1607025&r2=1607026&view=diff
==============================================================================
--- manifoldcf/trunk/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/jdbc/JDBCConnection.java (original)
+++ manifoldcf/trunk/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/jdbc/JDBCConnection.java Tue Jul  1 09:35:51 2014
@@ -512,7 +512,16 @@ public class JDBCConnection
           {
             InputStream bis = rs.getBinaryStream(colnum);
             if (bis != null)
-              value = new TempFileInput(bis);
+            {
+              try
+              {
+                value = new TempFileInput(bis);
+              }
+              catch (IOException e)
+              {
+                handleIOException(e,"reading binary data");
+              }
+            }
           }
           else if (isBLOB(rsmd,colnum))
           {
@@ -522,13 +531,31 @@ public class JDBCConnection
             // Cleanup should happen by the user of the resultset.
             // System.out.println(" Blob length = "+Long.toString(blob.length()));
             if (blob != null)
-              value = new TempFileInput(blob.getBinaryStream(),blob.length());
+            {
+              try
+              {
+                value = new TempFileInput(blob.getBinaryStream(),blob.length());
+              }
+              catch (IOException e)
+              {
+                handleIOException(e,"reading blob");
+              }
+            }
           }
           else if (isCLOB(rsmd,colnum))
           {
             Clob clob = getCLOB(rs,colnum);
             if (clob != null)
-              value = new TempFileCharacterInput(clob.getCharacterStream(),clob.length());
+            {
+              try
+              {
+                value = new TempFileCharacterInput(clob.getCharacterStream(),clob.length());
+              }
+              catch (IOException e)
+              {
+                handleIOException(e,"reading clob");
+              }
+            }
           }
           else
           {
@@ -548,6 +575,14 @@ public class JDBCConnection
     }
   }
 
+  protected static void handleIOException(IOException e, String context)
+    throws ManifoldCFException
+  {
+    if (e instanceof InterruptedIOException)
+      throw new ManifoldCFException(e.getMessage(),e,ManifoldCFException.INTERRUPTED);
+    throw new ManifoldCFException("IO exception while "+context+": "+e.getMessage(),e);
+  }
+  
   protected static String[] readColumnNames(ResultSetMetaData rsmd, boolean useName)
     throws ManifoldCFException, ServiceInterruption
   {

Modified: manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/TempFileCharacterInput.java
URL: http://svn.apache.org/viewvc/manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/TempFileCharacterInput.java?rev=1607026&r1=1607025&r2=1607026&view=diff
==============================================================================
--- manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/TempFileCharacterInput.java (original)
+++ manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/TempFileCharacterInput.java Tue Jul  1 09:35:51 2014
@@ -48,7 +48,7 @@ public class TempFileCharacterInput exte
   *          and hash value for the data.
   */
   public TempFileCharacterInput(Reader is)
-    throws ManifoldCFException
+    throws ManifoldCFException, IOException
   {
     this(is,-1L);
   }
@@ -59,7 +59,7 @@ public class TempFileCharacterInput exte
   *@param length is the length limit to transfer, or -1 if no limit
   */
   public TempFileCharacterInput(Reader is, long length)
-    throws ManifoldCFException
+    throws ManifoldCFException, IOException
   {
     this(is,length,DEFAULT_MAX_MEM_SIZE);
   }
@@ -72,7 +72,7 @@ public class TempFileCharacterInput exte
   *        saved in memory will be guaranteed less than this size.
   */
   public TempFileCharacterInput(Reader is, long length, int maxInMemoryLength)
-    throws ManifoldCFException
+    throws ManifoldCFException, IOException
   {
     super();
     
@@ -86,32 +86,25 @@ public class TempFileCharacterInput exte
     char[] buffer = new char[chunkSize];
     int chunkTotal = 0;
     boolean eofSeen = false;
-    try
+    while (true)
     {
-      while (true)
+      int chunkAmount;
+      if (length == -1L || length > chunkSize)
+        chunkAmount = chunkSize-chunkTotal;
+      else
       {
-        int chunkAmount;
-        if (length == -1L || length > chunkSize)
-          chunkAmount = chunkSize-chunkTotal;
-        else
-        {
-          chunkAmount = (int)(length-chunkTotal);
-          eofSeen = true;
-        }
-        if (chunkAmount == 0)
-          break;
-        int readsize = is.read(buffer,chunkTotal,chunkAmount);
-        if (readsize == -1)
-        {
-          eofSeen = true;
-          break;
-        }
-        chunkTotal += readsize;
+        chunkAmount = (int)(length-chunkTotal);
+        eofSeen = true;
       }
-    }
-    catch (IOException e)
-    {
-      handleIOException(e,"reading character stream");
+      if (chunkAmount == 0)
+        break;
+      int readsize = is.read(buffer,chunkTotal,chunkAmount);
+      if (readsize == -1)
+      {
+        eofSeen = true;
+        break;
+      }
+      chunkTotal += readsize;
     }
     
     // Set up hash digest, and calculate the initial hash.
@@ -141,80 +134,114 @@ public class TempFileCharacterInput exte
       inMemoryBuffer = null;
       // Create a temporary file!
       long totalMoved = 0;
+      
+      // Create a temporary file to put the stuff in
+      File outfile;
+      try
+      {
+        outfile = File.createTempFile("_MC_","");
+      }
+      catch (IOException e)
+      {
+        handleIOException(e,"creating backing file");
+        outfile = null;
+      }
       try
       {
-        // Create a temporary file to put the stuff in
-        File outfile = File.createTempFile("_MC_","");
+        // Register the file for autodeletion, using our infrastructure.
+        ManifoldCF.addFile(outfile);
+        // deleteOnExit() causes memory leakage!
+        // outfile.deleteOnExit();
+
+        FileOutputStream outStream;
+        OutputStreamWriter outWriter;
         try
         {
-          // Register the file for autodeletion, using our infrastructure.
-          ManifoldCF.addFile(outfile);
-          // deleteOnExit() causes memory leakage!
-          // outfile.deleteOnExit();
-
-          FileOutputStream outStream = new FileOutputStream(outfile);
+          outStream = new FileOutputStream(outfile);
           // Create a Writer corresponding to the file output stream, and encode using utf-8
-          OutputStreamWriter outWriter = new OutputStreamWriter(outStream,StandardCharsets.UTF_8);
+          outWriter = new OutputStreamWriter(outStream,StandardCharsets.UTF_8);
+        }
+        catch (IOException e)
+        {
+          handleIOException(e,"opening backing file");
+          outStream = null;
+          outWriter = null;
+        }
+        try
+        {
+          //  Transfor what we've already read.
           try
           {
-            //  Transfor what we've already read.
             outWriter.write(buffer,0,chunkTotal);
-            totalMoved += chunkTotal;
-            // Now, transfer the remainder
-            while (true)
+          }
+          catch (IOException e)
+          {
+            handleIOException(e,"writing backing file");
+          }
+          totalMoved += chunkTotal;
+          // Now, transfer the remainder
+          while (true)
+          {
+            int moveAmount;
+            if (length == -1L || length-totalMoved > chunkSize)
+              moveAmount = chunkSize;
+            else
+              moveAmount = (int)(length-totalMoved);
+            if (moveAmount == 0)
+              break;
+            // Read character data in 64K chunks
+            int readsize = is.read(buffer,0,moveAmount);
+            if (readsize == -1)
+              break;
+            try
             {
-              int moveAmount;
-              if (length == -1L || length-totalMoved > chunkSize)
-                moveAmount = chunkSize;
-              else
-                moveAmount = (int)(length-totalMoved);
-              if (moveAmount == 0)
-                break;
-              // Read character data in 64K chunks
-              int readsize = is.read(buffer,0,moveAmount);
-              if (readsize == -1)
-                break;
               outWriter.write(buffer,0,readsize);
-              ManifoldCF.addToHash(md,new String(buffer,0,readsize));
-              totalMoved += readsize;
             }
-
+            catch (IOException e)
+            {
+              handleIOException(e,"writing backing file");
+            }
+            ManifoldCF.addToHash(md,new String(buffer,0,readsize));
+            totalMoved += readsize;
           }
-          finally
+
+        }
+        finally
+        {
+          try
           {
             outWriter.close();
           }
+          catch (IOException e)
+          {
+            handleIOException(e,"closing backing file");
+          }
+        }
 
-          // Now, create the input stream.
-          // Save the file name
-          file = outfile;
-          charLength = totalMoved;
-          hashValue = ManifoldCF.getHashValue(md);
+        // Now, create the input stream.
+        // Save the file name
+        file = outfile;
+        charLength = totalMoved;
+        hashValue = ManifoldCF.getHashValue(md);
 
-        }
-        catch (Throwable e)
-        {
-          // Delete the temp file we created on any error condition
-          // outfile.delete();
-          ManifoldCF.deleteFile(outfile);
-          if (e instanceof Error)
-            throw (Error)e;
-          if (e instanceof RuntimeException)
-            throw (RuntimeException)e;
-          if (e instanceof ManifoldCFException)
-            throw (ManifoldCFException)e;
-          if (e instanceof IOException)
-            throw (IOException)e;
-          throw new RuntimeException("Unexpected throwable of type "+e.getClass().getName()+": "+e.getMessage(),e);
-        }
       }
-      catch (IOException e)
+      catch (Throwable e)
       {
-        handleIOException(e,"writing temporary file");
+        // Delete the temp file we created on any error condition
+        // outfile.delete();
+        ManifoldCF.deleteFile(outfile);
+        if (e instanceof Error)
+          throw (Error)e;
+        if (e instanceof RuntimeException)
+          throw (RuntimeException)e;
+        if (e instanceof ManifoldCFException)
+          throw (ManifoldCFException)e;
+        if (e instanceof IOException)
+          throw (IOException)e;
+        throw new RuntimeException("Unexpected throwable of type "+e.getClass().getName()+": "+e.getMessage(),e);
       }
     }
     
-
   }
 
   /** Construct from an existing temporary fle.

Modified: manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/TempFileInput.java
URL: http://svn.apache.org/viewvc/manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/TempFileInput.java?rev=1607026&r1=1607025&r2=1607026&view=diff
==============================================================================
--- manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/TempFileInput.java (original)
+++ manifoldcf/trunk/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/TempFileInput.java Tue Jul  1 09:35:51 2014
@@ -44,7 +44,7 @@ public class TempFileInput extends Binar
   *@param is is the input stream to use to construct the temporary file.
   */
   public TempFileInput(InputStream is)
-    throws ManifoldCFException
+    throws ManifoldCFException, IOException
   {
     this(is,-1L);
   }
@@ -54,7 +54,7 @@ public class TempFileInput extends Binar
   *@param length is the maximum number of bytes to transfer, or -1 if no limit.
   */
   public TempFileInput(InputStream is, long length)
-    throws ManifoldCFException
+    throws ManifoldCFException, IOException
   {
     this(is,length,DEFAULT_MAX_MEM_SIZE);
   }
@@ -65,7 +65,7 @@ public class TempFileInput extends Binar
   *@param maxMemSize is the maximum bytes we keep in memory in lieu of using a file.
   */
   public TempFileInput(InputStream is, long length, int maxMemSize)
-    throws ManifoldCFException
+    throws ManifoldCFException, IOException
   {
     super();
     
@@ -78,32 +78,25 @@ public class TempFileInput extends Binar
     byte[] buffer = new byte[chunkSize];
     int chunkTotal = 0;
     boolean eofSeen = false;
-    try
+    while (true)
     {
-      while (true)
+      int chunkAmount;
+      if (length == -1L || length > chunkSize)
+        chunkAmount = chunkSize-chunkTotal;
+      else
       {
-        int chunkAmount;
-        if (length == -1L || length > chunkSize)
-          chunkAmount = chunkSize-chunkTotal;
-        else
-        {
-          chunkAmount = (int)(length-chunkTotal);
-          eofSeen = true;
-        }
-        if (chunkAmount == 0)
-          break;
-        int readsize = is.read(buffer,chunkTotal,chunkAmount);
-        if (readsize == -1)
-        {
-          eofSeen = true;
-          break;
-        }
-        chunkTotal += readsize;
+        chunkAmount = (int)(length-chunkTotal);
+        eofSeen = true;
       }
-    }
-    catch (IOException e)
-    {
-      handleIOException(e,"reading byte stream");
+      if (chunkAmount == 0)
+        break;
+      int readsize = is.read(buffer,chunkTotal,chunkAmount);
+      if (readsize == -1)
+      {
+        eofSeen = true;
+        break;
+      }
+      chunkTotal += readsize;
     }
 
     if (eofSeen && chunkTotal < maxMemSize)
@@ -120,73 +113,105 @@ public class TempFileInput extends Binar
     else
     {
       inMemoryBuffer = null;
+      // Create a temporary file to put the stuff in
+      File outfile;
       try
       {
-        // Create a temporary file to put the stuff in
-        File outfile = File.createTempFile("_MC_","");
+        outfile = File.createTempFile("_MC_","");
+      }
+      catch (IOException e)
+      {
+        handleIOException(e,"creating backing file");
+        outfile = null;
+      }
+      try
+      {
+        // Register the file for autodeletion, using our infrastructure.
+        ManifoldCF.addFile(outfile);
+        // deleteOnExit() causes memory leakage!
+        // outfile.deleteOnExit();
+        FileOutputStream outStream;
+        try
+        {
+          outStream = new FileOutputStream(outfile);
+        }
+        catch (IOException e)
+        {
+          handleIOException(e,"opening backing file");
+          outStream = null;
+        }
         try
         {
-          // Register the file for autodeletion, using our infrastructure.
-          ManifoldCF.addFile(outfile);
-          // deleteOnExit() causes memory leakage!
-          // outfile.deleteOnExit();
-          FileOutputStream outStream = new FileOutputStream(outfile);
+          long totalMoved = 0;
+            
+          //  Transfor what we've already read.
           try
           {
-            long totalMoved = 0;
-            
-            //  Transfor what we've already read.
             outStream.write(buffer,0,chunkTotal);
-            totalMoved += chunkTotal;
+          }
+          catch (IOException e)
+          {
+            handleIOException(e,"writing backing file");
+          }
+          totalMoved += chunkTotal;
 
-            while (true)
+          while (true)
+          {
+            int moveAmount;
+            if (length == -1L || length-totalMoved > chunkSize)
+              moveAmount = chunkSize;
+            else
+              moveAmount = (int)(length-totalMoved);
+            if (moveAmount == 0)
+              break;
+            // Read binary data in 64K chunks
+            int readsize = is.read(buffer,0,moveAmount);
+            if (readsize == -1)
+              break;
+            try
             {
-              int moveAmount;
-              if (length == -1L || length-totalMoved > chunkSize)
-                moveAmount = chunkSize;
-              else
-                moveAmount = (int)(length-totalMoved);
-              if (moveAmount == 0)
-                break;
-              // Read binary data in 64K chunks
-              int readsize = is.read(buffer,0,moveAmount);
-              if (readsize == -1)
-                break;
               outStream.write(buffer,0,readsize);
-              totalMoved += readsize;
             }
-            // System.out.println(" Moved "+Long.toString(totalMoved));
+            catch (IOException e)
+            {
+              handleIOException(e,"writing backing file");
+            }
+            totalMoved += readsize;
           }
-          finally
+          // System.out.println(" Moved "+Long.toString(totalMoved));
+        }
+        finally
+        {
+          try
           {
             outStream.close();
           }
-
-          // Now, create the input stream.
-          // Save the file name
-          file = outfile;
-          this.length = file.length();
-
-        }
-        catch (Throwable e)
-        {
-          // Delete the temp file we created on any error condition
-          // outfile.delete();
-          ManifoldCF.deleteFile(outfile);
-          if (e instanceof Error)
-            throw (Error)e;
-          if (e instanceof RuntimeException)
-            throw (RuntimeException)e;
-          if (e instanceof ManifoldCFException)
-            throw (ManifoldCFException)e;
-          if (e instanceof IOException)
-            throw (IOException)e;
-          throw new RuntimeException("Unexpected throwable of type "+e.getClass().getName()+": "+e.getMessage(),e);
+          catch (IOException e)
+          {
+            handleIOException(e,"closing backing file");
+          }
         }
-      }
-      catch (IOException e)
-      {
-        handleIOException(e,"writing temporary file");
+
+        // Now, create the input stream.
+        // Save the file name
+        file = outfile;
+        this.length = file.length();
+
+      }
+      catch (Throwable e)
+      {
+        // Delete the temp file we created on any error condition
+        // outfile.delete();
+        ManifoldCF.deleteFile(outfile);
+        if (e instanceof Error)
+          throw (Error)e;
+        if (e instanceof RuntimeException)
+          throw (RuntimeException)e;
+        if (e instanceof ManifoldCFException)
+          throw (ManifoldCFException)e;
+        if (e instanceof IOException)
+          throw (IOException)e;
+        throw new RuntimeException("Unexpected throwable of type "+e.getClass().getName()+": "+e.getMessage(),e);
       }
     }
   }