Commits

David Carr committed 487aa69

Improve closing of streams.

  • Participants
  • Parent commits 634d69b

Comments (0)

Files changed (2)

File engine/src/main/java/deepdiff/scope/SortedZipDiffScope.java

 import deepdiff.core.DiffUnitProcessor;
 
 /**
- * An implementation of {@link DiffScope} that compares two Zip files. These Zip
- * files do not need to be physical files; any {@link InputStream} can be used.
- * Thus, it is possible to process Zip files within Zip files.
+ * An implementation of {@link DiffScope} that compares two Zip files. These Zip files do not need
+ * to be physical files; any {@link InputStream} can be used. Thus, it is possible to process Zip
+ * files within Zip files.
  */
 public class SortedZipDiffScope implements DiffScope {
     private static final Logger log = Logger.getLogger(SortedZipDiffScope.class);
-    
+
     private SortedMap<String, CachedZipEntry> leftEntries;
     private SortedMap<String, CachedZipEntry> rightEntries;
 
 
     /**
      * Initializes a new ZipDiffScope object.
-     *
+     * 
      * @param path the scoped path to the Zip files
      * @param isLeft the {@link InputStream} for the Zip file on the left
      * @param isRight the {@link InputStream} for the Zip file on the right
 
     /**
      * Scans the Zip archive for DiffUnits and processes them
-     *
-     * @param unitProcessor the unit processor to process the DiffUnits found in
-     *                      the scan
-     * @param pointProcessor the point processor to pass to the unit processor
-     *                       when processing units
+     * 
+     * @param unitProcessor the unit processor to process the DiffUnits found in the scan
+     * @param pointProcessor the point processor to pass to the unit processor when processing units
      */
-    public void scan(DiffUnitProcessor unitProcessor,
-                     DiffPointProcessor pointProcessor) {
-        log.debug("Starting to scan scope " + path);
-        loadEntries(isLeft, leftEntries);
-        loadEntries(isRight, rightEntries);
-        SortedSet<String> keySet = new TreeSet<String>();
-        keySet.addAll(leftEntries.keySet());
-        keySet.addAll(rightEntries.keySet());
-        for(String path : keySet) {
-            CachedZipEntry leftEntry = leftEntries.get(path);
-            CachedZipEntry rightEntry = rightEntries.get(path);
-            CachedZipDiffUnit diffUnit =
-                new CachedZipDiffUnit(this, leftEntry, rightEntry);
-            unitProcessor.processDiffUnit(diffUnit, pointProcessor);
-            if(leftEntry != null) {
-                leftEntry.cleanup();
+    public void scan(DiffUnitProcessor unitProcessor, DiffPointProcessor pointProcessor) {
+        try {
+            log.debug("Starting to scan scope " + path);
+            loadEntries(isLeft, leftEntries);
+            loadEntries(isRight, rightEntries);
+            SortedSet<String> keySet = new TreeSet<String>();
+            keySet.addAll(leftEntries.keySet());
+            keySet.addAll(rightEntries.keySet());
+            for (String path : keySet) {
+                CachedZipEntry leftEntry = leftEntries.get(path);
+                CachedZipEntry rightEntry = rightEntries.get(path);
+                CachedZipDiffUnit diffUnit = new CachedZipDiffUnit(this, leftEntry, rightEntry);
+                unitProcessor.processDiffUnit(diffUnit, pointProcessor);
+                if (leftEntry != null) {
+                    leftEntry.cleanup();
+                }
+                if (rightEntry != null) {
+                    rightEntry.cleanup();
+                }
             }
-            if(rightEntry != null) {
-                rightEntry.cleanup();
+            leftEntries.clear();
+            rightEntries.clear();
+            log.debug("Completed scan of scope " + path);
+        } finally {
+            if (isLeft != null) {
+                try {
+                    isLeft.close();
+                } catch (Exception ex) {
+                    // Ignore exception on close
+                }
+            }
+            if (isRight != null) {
+                try {
+                    isRight.close();
+                } catch (Exception ex) {
+                    // Ignore exception on close
+                }
             }
         }
-        leftEntries.clear();
-        rightEntries.clear();
-        log.debug("Completed scan of scope " + path);
     }
 
     /**
      * Returns the scoped path to the Zip files
-     *
+     * 
      * @return the scoped path to the Zip files
      */
     public String getPath() {
         return path;
     }
-    
+
     private void loadEntries(InputStream is, Map<String, CachedZipEntry> cachedEntries) {
         ZipInputStream zis = new ZipInputStream(is);
         ZipEntry entry = null;
         boolean validEntry = false;
         try {
-            while(!validEntry || entry != null) {
+            while (!validEntry || entry != null) {
                 try {
                     entry = zis.getNextEntry();
                     validEntry = true;
-                    if(entry != null) {
+                    if (entry != null) {
                         String path = entry.getName();
                         CachedZipEntry cachedEntry = new CachedZipEntry(entry, zis);
                         cachedEntries.put(path, cachedEntry);
                     }
-                } catch(ZipException ze) {
-                    if("encrypted ZIP entry not supported".equals(ze.getMessage())) {
-                        log.warn(path + " contains encrypted ZIP entries; comparison of this file not supported");
+                } catch (ZipException ze) {
+                    if ("encrypted ZIP entry not supported".equals(ze.getMessage())) {
+                        log.warn(path
+                                + " contains encrypted ZIP entries; comparison of this file not supported");
                     } else {
                         log.error("Failure scanning scope: " + path, ze);
                     }
-                } catch(IOException ioe) {
+                } catch (IOException ioe) {
                     validEntry = false;
                     log.error("Failure scanning scope: " + path, ioe);
                 }
             try {
                 zis.close();
             } catch (Exception ex) {
-                //Ignore exception on close
+                // Ignore exception on close
             }
         }
     }
-    
+
     private static class CachedZipEntry extends ZipEntry {
         private String name;
         private boolean directory;
         private File tmpFile;
         private byte[] data;
         int length;
-        
+
         public CachedZipEntry(ZipEntry ze, ZipInputStream zis) throws IOException {
             super(ze.getName());
             this.name = ze.getName();
             this.directory = ze.isDirectory();
-            if(!this.directory) {
+            if (!this.directory) {
                 readAll(zis, ze);
             }
         }
 
         public void cleanup() {
-            if(tmpFile != null) {
-                if(!tmpFile.delete()) {
+            if (tmpFile != null) {
+                if (!tmpFile.delete()) {
                     tmpFile.deleteOnExit();
                 }
             } else {
         }
 
         public InputStream getStream() throws IOException {
-            if(tmpFile != null) {
+            if (tmpFile != null) {
                 return new FileInputStream(tmpFile);
             } else {
                 return new ByteArrayInputStream(data, 0, length);
             }
         }
-        
+
         @Override
         public String getName() {
             return name;
         }
-        
+
         @Override
         public boolean isDirectory() {
             return directory;
         }
-        
+
         private void readAll(ZipInputStream zis, ZipEntry entry) throws IOException {
             final int maxMemSize = 100000;
             long totalSize = entry.getSize();
-            if(totalSize >= maxMemSize || totalSize < 0) {
+            if (totalSize >= maxMemSize || totalSize < 0) {
                 tmpFile = File.createTempFile("ZipContent", null);
                 FileOutputStream fos = new FileOutputStream(tmpFile);
                 byte[] buf = new byte[1024];
                 int size = zis.read(buf);
-                while(size != -1) {
+                while (size != -1) {
                     fos.write(buf, 0, size);
                     size = zis.read(buf);
                 }
                 data = new byte[length];
             } else {
                 length = (int) totalSize;
-                //Allow an extra byte in the buffer so we can request an extra
-                //byte at the end, to get confirmation that there's no more data
-                //left
-                data = new byte[length+1];
+                // Allow an extra byte in the buffer so we can request an extra
+                // byte at the end, to get confirmation that there's no more data
+                // left
+                data = new byte[length + 1];
                 int off = 0;
                 int size = zis.read(data, off, data.length - off);
-                while(size != -1) {
+                while (size != -1) {
                     off += size;
                     size = zis.read(data, off, data.length - off);
                 }
             }
         }
     }
-    
+
     public static class CachedZipDiffUnit implements DiffUnit {
         private CachedZipEntry e1;
         private CachedZipEntry e2;
 
         /**
          * Returns whether the left entry exists
-         *
+         * 
          * @return whether the left entry exists
          */
         public boolean leftExists() {
 
         /**
          * Returns whether the right entry exists
-         *
+         * 
          * @return whether the right entry exists
          */
         public boolean rightExists() {
 
         /**
          * Returns whether the left entry represents a directory
-         *
+         * 
          * @return whether the left entry represents a directory
          */
         public boolean leftIsDir() {
 
         /**
          * Returns whether the right entry represents a directory
-         *
+         * 
          * @return whether the right entry represents a directory
          */
         public boolean rightIsDir() {
 
         /**
          * Returns a stream to access the content of the left entry
-         *
+         * 
          * @return a stream to access the content of the left entry
-         *
+         * 
          * @throws IOException if there was an error creating the stream
          */
         public InputStream getLeftInputStream() throws IOException {
 
         /**
          * Returns a stream to access the content of the right entry
-         *
+         * 
          * @return a stream to access the content of the right entry
-         *
+         * 
          * @throws IOException if there was an error creating the stream
          */
         public InputStream getRightInputStream() throws IOException {
         }
 
         /**
-         * Returns the scoped path for the unit. This is the path of the Zip scope,
-         * followed by the path of the entry within the Zip scope
-         *
+         * Returns the scoped path for the unit. This is the path of the Zip scope, followed by the
+         * path of the entry within the Zip scope
+         * 
          * @return the scoped path for the unit
          */
         public String getScopedPath() {

File engine/src/main/java/deepdiff/scope/XMLDiffScope.java

             log.error("Malformed XML file: " + path, saxe);
         } catch (IOException ioe) {
             log.error("Failure scanning scope " + path, ioe);
+        } finally {
+            if (is1 != null) {
+                try {
+                    is1.close();
+                } catch (Exception ex) {
+                    //Ignore exception on close
+                }
+            }
+            if (is2 != null) {
+                try {
+                    is2.close();
+                } catch (Exception ex) {
+                    //Ignore exception on close
+                }
+            }
         }
         log.debug("Completed scan of scope " + path);
     }