You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2011/10/28 22:22:04 UTC

svn commit: r1190544 - /maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java

Author: olamy
Date: Fri Oct 28 20:22:04 2011
New Revision: 1190544

URL: http://svn.apache.org/viewvc?rev=1190544&view=rev
Log:
ensure streams are closed

Modified:
    maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java

Modified: maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java
URL: http://svn.apache.org/viewvc/maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java?rev=1190544&r1=1190543&r2=1190544&view=diff
==============================================================================
--- maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java (original)
+++ maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java Fri Oct 28 20:22:04 2011
@@ -31,7 +31,9 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 
-/** @author rafale */
+/**
+ * @author rafale
+ */
 public class FileCharsetDetector
     extends AbstractLogEnabled
 {
@@ -44,72 +46,79 @@ public class FileCharsetDetector
     {
         nsDetector det = new nsDetector( nsPSMDetector.ALL );
 
-        det.Init(
-            new nsICharsetDetectionObserver()
+        det.Init( new nsICharsetDetectionObserver()
+        {
+            public void Notify( String charset )
             {
-                public void Notify( String charset )
-                {
-                    FileCharsetDetector.this.charset = charset;
-                    FileCharsetDetector.this.found = true;
-                }
+                FileCharsetDetector.this.charset = charset;
+                FileCharsetDetector.this.found = true;
             }
-        );
+        } );
 
-        BufferedInputStream imp = new BufferedInputStream( new FileInputStream( detectedFile ) );
+        FileInputStream fileInputStream = new FileInputStream( detectedFile );
+        BufferedInputStream imp = new BufferedInputStream( fileInputStream );
+        try
+        {
 
-        byte[] buf = new byte[1024];
-        int len;
-        boolean done = false;
-        boolean isAscii = true;
+            byte[] buf = new byte[1024];
+            int len;
+            boolean done = false;
+            boolean isAscii = true;
 
-        while ( ( len = imp.read( buf, 0, buf.length ) ) != -1 )
-        {
-            // Check if the stream is only ascii.
-            if ( isAscii )
+            while ( ( len = imp.read( buf, 0, buf.length ) ) != -1 )
             {
-                isAscii = det.isAscii( buf, len );
+                // Check if the stream is only ascii.
+                if ( isAscii )
+                {
+                    isAscii = det.isAscii( buf, len );
+                }
+
+                // DoIt if non-ascii and not done yet.
+                if ( !isAscii && !done )
+                {
+                    done = det.DoIt( buf, len, false );
+                    found = done;
+                }
             }
+            det.DataEnd();
 
-            // DoIt if non-ascii and not done yet.
-            if ( !isAscii && !done )
+            if ( !isFound() )
             {
-                done = det.DoIt( buf, len, false );
-                found = done;
-            }
-        }
-        det.DataEnd();
+                String[] prob = det.getProbableCharsets();
 
-        if ( !isFound() )
-        {
-            String[] prob = det.getProbableCharsets();
+                if ( prob.length > 0 )
+                {
+                    charset = prob[0];
+                }
+            }
 
-            if ( prob.length > 0 )
+            if ( isAscii )
             {
-                charset = prob[0];
+                charset = "ASCII";
             }
         }
-
-        if ( isAscii )
+        finally
         {
-            charset = "ASCII";
+            imp.close();
+            fileInputStream.close();
         }
     }
 
+
+
     public FileCharsetDetector( InputStream detectedStream )
         throws FileNotFoundException, IOException
     {
         nsDetector det = new nsDetector( nsPSMDetector.ALL );
 
-        det.Init(
-            new nsICharsetDetectionObserver()
+        det.Init( new nsICharsetDetectionObserver()
+        {
+            public void Notify( String charset )
             {
-                public void Notify( String charset )
-                {
-                    FileCharsetDetector.this.charset = charset;
-                    FileCharsetDetector.this.found = true;
-                }
+                FileCharsetDetector.this.charset = charset;
+                FileCharsetDetector.this.found = true;
             }
-        );
+        } );
 
         BufferedInputStream imp = new BufferedInputStream( detectedStream );