Author: rdonkin
Date: Thu Dec  5 20:33:26 2013
New Revision: 1548278

URL: http://svn.apache.org/r1548278
Log:
Reduce visibility of methods, preparing to factor out object

Modified:
    
creadur/rat/branches/gsoc/apache-rat-core/src/main/java/org/apache/rat/document/impl/guesser/BinaryGuesser.java
    
creadur/rat/branches/gsoc/apache-rat-core/src/test/java/org/apache/rat/document/impl/guesser/BinaryGuesserTest.java

Modified: 
creadur/rat/branches/gsoc/apache-rat-core/src/main/java/org/apache/rat/document/impl/guesser/BinaryGuesser.java
URL: 
http://svn.apache.org/viewvc/creadur/rat/branches/gsoc/apache-rat-core/src/main/java/org/apache/rat/document/impl/guesser/BinaryGuesser.java?rev=1548278&r1=1548277&r2=1548278&view=diff
==============================================================================
--- 
creadur/rat/branches/gsoc/apache-rat-core/src/main/java/org/apache/rat/document/impl/guesser/BinaryGuesser.java
 (original)
+++ 
creadur/rat/branches/gsoc/apache-rat-core/src/main/java/org/apache/rat/document/impl/guesser/BinaryGuesser.java
 Thu Dec  5 20:33:26 2013
@@ -15,7 +15,7 @@
  * KIND, either express or implied.  See the License for the    *
  * specific language governing permissions and limitations      *
  * under the License.                                           *
- */ 
+ */
 package org.apache.rat.document.impl.guesser;
 
 import java.io.IOException;
@@ -36,104 +36,102 @@ import org.apache.rat.api.Document;
  */
 public class BinaryGuesser {
 
-    private static boolean isBinaryDocument(Document document) {
+    private static boolean isBinaryDocument(final Document document) {
         boolean result = false;
         InputStream stream = null;
-        try
-        {
+        try {
             stream = document.inputStream();
             result = isBinary(stream);
-        }
-        catch (IOException e)
-        {
+        } catch (final IOException e) {
             result = false;
-        }
-        finally
-        {
-            try
-            {
-                if (stream != null)
-                {
+        } finally {
+            try {
+                if (stream != null) {
                     stream.close();
                 }
-            }
-            catch (IOException e)
-            {
+            } catch (final IOException e) {
                 // SWALLOW
-            }   
+            }
         }
         return result;
     }
-    
-    private static boolean isBinary(CharSequence taste) {
+
+    private static boolean isBinary(final CharSequence taste) {
         int highBytes = 0;
         final int length = taste.length();
         for (int i = 0; i < length; i++) {
-            char c = taste.charAt(i);
+            final char c = taste.charAt(i);
             if (c > BinaryGuesser.NON_ASCII_THREASHOLD
-                || c <= BinaryGuesser.ASCII_CHAR_THREASHOLD) {
+                    || c <= BinaryGuesser.ASCII_CHAR_THREASHOLD) {
                 highBytes++;
             }
         }
-        return highBytes * BinaryGuesser.HIGH_BYTES_RATIO
-            > length * BinaryGuesser.TOTAL_READ_RATIO;
+        return highBytes * BinaryGuesser.HIGH_BYTES_RATIO > length
+                * BinaryGuesser.TOTAL_READ_RATIO;
     }
 
     /**
      * Do the first few bytes of the stream hint at a binary file?
-     *
-     * <p>Any IOException is swallowed internally and the test returns
-     * false.</p>
-     *
-     * <p>This method may lead to false negatives if the reader throws
-     * an exception because it can't read characters according to the
-     * reader's encoding from the underlying stream.</p>
+     * 
+     * <p>
+     * Any IOException is swallowed internally and the test returns false.
+     * </p>
+     * 
+     * <p>
+     * This method may lead to false negatives if the reader throws an 
exception
+     * because it can't read characters according to the reader's encoding from
+     * the underlying stream.
+     * </p>
      */
-    public static boolean isBinary(Reader in) {
-        char[] taste = new char[100];
+    private static boolean isBinary(final Reader in) {
+        final char[] taste = new char[100];
         try {
-            int bytesRead = in.read(taste);
+            final int bytesRead = in.read(taste);
             if (bytesRead > 0) {
                 return isBinary(new String(taste, 0, bytesRead));
             }
-        } catch (IOException e) {
-            // SWALLOW 
+        } catch (final IOException e) {
+            // SWALLOW
         }
         return false;
     }
 
     /**
      * Do the first few bytes of the stream hint at a binary file?
-     *
-     * <p>Any IOException is swallowed internally and the test returns
-     * false.</p>
-     *
-     * <p>This method will try to read bytes from the stream and
-     * translate them to characters according to the platform's
-     * default encoding.  If any bytes can not be translated to
-     * characters it will assume the original data must be binary and
-     * return true.</p>
+     * 
+     * <p>
+     * Any IOException is swallowed internally and the test returns false.
+     * </p>
+     * 
+     * <p>
+     * This method will try to read bytes from the stream and translate them to
+     * characters according to the platform's default encoding. If any bytes 
can
+     * not be translated to characters it will assume the original data must be
+     * binary and return true.
+     * </p>
      */
-    public static boolean isBinary(InputStream in) {
+    private static boolean isBinary(final InputStream in) {
         try {
-            byte[] taste = new byte[200];
-            int bytesRead = in.read(taste);
+            final byte[] taste = new byte[200];
+            final int bytesRead = in.read(taste);
             if (bytesRead > 0) {
-                ByteBuffer bytes = ByteBuffer.wrap(taste, 0, bytesRead);
+                final ByteBuffer bytes = ByteBuffer.wrap(taste, 0, bytesRead);
                 CharBuffer chars = CharBuffer.allocate(2 * bytesRead);
-                Charset cs = 
Charset.forName(System.getProperty("file.encoding"));
-                CharsetDecoder cd = cs.newDecoder()
-                    .onMalformedInput(CodingErrorAction.REPORT)
-                    .onUnmappableCharacter(CodingErrorAction.REPORT);
+                final Charset cs =
+                        Charset.forName(System.getProperty("file.encoding"));
+                final CharsetDecoder cd =
+                        cs.newDecoder()
+                                .onMalformedInput(CodingErrorAction.REPORT)
+                                
.onUnmappableCharacter(CodingErrorAction.REPORT);
                 while (bytes.remaining() > 0) {
-                    CoderResult res = cd.decode(bytes, chars, true);
+                    final CoderResult res = cd.decode(bytes, chars, true);
                     if (res.isMalformed() || res.isUnmappable()) {
                         return true;
                     } else if (res.isOverflow()) {
                         chars.limit(chars.position());
                         chars.rewind();
-                        int c = chars.capacity() * 2;
-                        CharBuffer on = CharBuffer.allocate(c);
+                        final int c = chars.capacity() * 2;
+                        final CharBuffer on = CharBuffer.allocate(c);
                         on.put(chars);
                         chars = on;
                     }
@@ -142,207 +140,120 @@ public class BinaryGuesser {
                 chars.rewind();
                 return isBinary(chars);
             }
-        } catch (IOException e) {
-            // SWALLOW 
+        } catch (final IOException e) {
+            // SWALLOW
         }
         return false;
     }
 
-    public static final boolean isBinaryData(final String name) {
+    private static final boolean isBinaryData(final String name) {
         return extensionMatches(name, DATA_EXTENSIONS);
     }
 
     /**
      * Is a file by that name a known non-binary file?
      */
-    public static final boolean isNonBinary(final String name) {
-        if (name == null) {return false;}
+    private static final boolean isNonBinary(final String name) {
+        if (name == null) {
+            return false;
+        }
         return extensionMatches(name.toUpperCase(Locale.US),
-                                BinaryGuesser.NON_BINARY_EXTENSIONS);
+                BinaryGuesser.NON_BINARY_EXTENSIONS);
     }
 
-    public static final boolean isExecutable(final String name) {
-        return name.equals(BinaryGuesser.JAVA) || extensionMatches(name, 
EXE_EXTENSIONS)
-            || containsExtension(name, EXE_EXTENSIONS);
+    private static final boolean isExecutable(final String name) {
+        return name.equals(BinaryGuesser.JAVA)
+                || extensionMatches(name, EXE_EXTENSIONS)
+                || containsExtension(name, EXE_EXTENSIONS);
     }
 
-    public static boolean containsExtension(final String name,
-                                             final String[] exts) {
-        for (int i = 0; i < exts.length; i++) {
-            if (name.indexOf("." + exts[i] + ".") >= 0) {
+    private static boolean containsExtension(final String name,
+            final String[] exts) {
+        for (final String ext : exts) {
+            if (name.indexOf("." + ext + ".") >= 0) {
                 return true;
             }
         }
         return false;
     }
 
-    public static boolean extensionMatches(final String name,
-                                            final String[] exts) {
-        for (int i = 0; i < exts.length; i++) {
-            if (name.endsWith("." + exts[i])) {
+    private static boolean extensionMatches(final String name,
+            final String[] exts) {
+        for (final String ext : exts) {
+            if (name.endsWith("." + ext)) {
                 return true;
             }
         }
         return false;
     }
 
-    public static boolean isBytecode(final String name) {
+    private static boolean isBytecode(final String name) {
         return BinaryGuesser.extensionMatches(name, BYTECODE_EXTENSIONS);
     }
 
-    public static final boolean isImage(final String name) {
+    private static final boolean isImage(final String name) {
         return BinaryGuesser.extensionMatches(name, IMAGE_EXTENSIONS);
     }
 
-    public static final boolean isKeystore(final String name) {
+    private static final boolean isKeystore(final String name) {
         return BinaryGuesser.extensionMatches(name, KEYSTORE_EXTENSIONS);
     }
-    
+
     /**
      * Is a file by that name a known binary file?
      */
-    public static final boolean isBinary(final String name) {
-        if (name == null) {return false;}
-        String normalisedName = GuessUtils.normalise(name);
-        return BinaryGuesser.JAR_MANIFEST.equals(name) || 
BinaryGuesser.isImage(normalisedName)
-            || BinaryGuesser.isKeystore(normalisedName) || 
BinaryGuesser.isBytecode(normalisedName)
-            || BinaryGuesser.isBinaryData(normalisedName) || 
BinaryGuesser.isExecutable(normalisedName);
-    }
-
-    public static final String[] DATA_EXTENSIONS = {
-        "DAT", "DOC",
-        "NCB", "IDB",
-        "SUO", "XCF",
-        "RAJ", "CERT",
-        "KS", "TS",
-        "ODP",
-    };
-    public static final String[] EXE_EXTENSIONS = {
-        "EXE", "DLL",
-        "LIB", "SO",
-        "A", "EXP",
-    };
-    public static final String[] KEYSTORE_EXTENSIONS = {
-        "JKS", "KEYSTORE", "PEM", "CRL"
-    };
-    public static final String[] IMAGE_EXTENSIONS = {
-        "PNG", "PDF",
-        "GIF", "GIFF",
-        "TIF", "TIFF",
-        "JPG", "JPEG",
-        "ICO", "ICNS",
-    };
-    public static final String[] BYTECODE_EXTENSIONS = {
-        "CLASS", "PYD",
-        "OBJ", "PYC",
-    };
-    
+    private static final boolean isBinary(final String name) {
+        if (name == null) {
+            return false;
+        }
+        final String normalisedName = GuessUtils.normalise(name);
+        return BinaryGuesser.JAR_MANIFEST.equals(name)
+                || BinaryGuesser.isImage(normalisedName)
+                || BinaryGuesser.isKeystore(normalisedName)
+                || BinaryGuesser.isBytecode(normalisedName)
+                || BinaryGuesser.isBinaryData(normalisedName)
+                || BinaryGuesser.isExecutable(normalisedName);
+    }
+
+    private static final String[] DATA_EXTENSIONS = { "DAT", "DOC", "NCB",
+            "IDB", "SUO", "XCF", "RAJ", "CERT", "KS", "TS", "ODP", };
+    private static final String[] EXE_EXTENSIONS = { "EXE", "DLL", "LIB", "SO",
+            "A", "EXP", };
+    private static final String[] KEYSTORE_EXTENSIONS = { "JKS", "KEYSTORE",
+            "PEM", "CRL" };
+    private static final String[] IMAGE_EXTENSIONS = { "PNG", "PDF", "GIF",
+            "GIFF", "TIF", "TIFF", "JPG", "JPEG", "ICO", "ICNS", };
+    private static final String[] BYTECODE_EXTENSIONS = { "CLASS", "PYD",
+            "OBJ", "PYC", };
+
     /**
      * Based on http://www.apache.org/dev/svn-eol-style.txt
      */
-    public static final String[] NON_BINARY_EXTENSIONS = {
-        "AART",
-        "AC",
-        "AM",
-        "BAT",
-        "C",
-        "CAT",
-        "CGI",
-        "CLASSPATH",
-        "CMD",
-        "CONFIG",
-        "CPP",
-        "CSS",
-        "CWIKI",
-        "DATA",
-        "DCL",
-        "DTD",
-        "EGRM",
-        "ENT",
-        "FT", 
-        "FN",
-        "FV", 
-        "GRM",
-        "G",
-        "H",
-        "HTACCESS",
-        "HTML",
-        "IHTML",
-        "IN",
-        "JAVA",
-        "JMX", 
-        "JSP",
-        "JS",
-        "JUNIT",
-        "JX", 
-        "MANIFEST",
-        "M4",
-        "MF",
-        "MF",
-        "META",
-        "MOD",
-        "N3",
-        "PEN",
-        "PL",
-        "PM",
-        "POD",
-        "POM",
-        "PROJECT",
-        "PROPERTIES",
-        "PY",
-        "RB",
-        "RDF",
-        "RNC",
-        "RNG",
-        "RNX",
-        "ROLES",
-        "RSS",
-        "SH",
-        "SQL",
-        "SVG",
-        "TLD",
-        "TXT",
-        "TYPES",
-        "VM",
-        "VSL",
-        "WSDD",
-        "WSDL",
-        "XARGS",
-        "XCAT",
-        "XCONF",
-        "XEGRM",
-        "XGRM",
-        "XLEX",
-        "XLOG",
-        "XMAP",
-        "XML",
-        "XROLES",
-        "XSAMPLES",
-        "XSD",
-        "XSL",
-        "XSLT",
-        "XSP",
-        "XUL",
-        "XWEB",
-        "XWELCOME",
-    };
-    public static final String JAR_MANIFEST = "MANIFEST.MF";
-    public static final String JAVA = "JAVA";
-    public static final int HIGH_BYTES_RATIO = 100;
-    public static final int TOTAL_READ_RATIO = 30;
-    public static final int NON_ASCII_THREASHOLD = 256;
-    public static final int ASCII_CHAR_THREASHOLD = 8;
+    private static final String[] NON_BINARY_EXTENSIONS = { "AART", "AC", "AM",
+            "BAT", "C", "CAT", "CGI", "CLASSPATH", "CMD", "CONFIG", "CPP",
+            "CSS", "CWIKI", "DATA", "DCL", "DTD", "EGRM", "ENT", "FT", "FN",
+            "FV", "GRM", "G", "H", "HTACCESS", "HTML", "IHTML", "IN", "JAVA",
+            "JMX", "JSP", "JS", "JUNIT", "JX", "MANIFEST", "M4", "MF", "MF",
+            "META", "MOD", "N3", "PEN", "PL", "PM", "POD", "POM", "PROJECT",
+            "PROPERTIES", "PY", "RB", "RDF", "RNC", "RNG", "RNX", "ROLES",
+            "RSS", "SH", "SQL", "SVG", "TLD", "TXT", "TYPES", "VM", "VSL",
+            "WSDD", "WSDL", "XARGS", "XCAT", "XCONF", "XEGRM", "XGRM", "XLEX",
+            "XLOG", "XMAP", "XML", "XROLES", "XSAMPLES", "XSD", "XSL", "XSLT",
+            "XSP", "XUL", "XWEB", "XWELCOME", };
+    private static final String JAR_MANIFEST = "MANIFEST.MF";
+    private static final String JAVA = "JAVA";
+    private static final int HIGH_BYTES_RATIO = 100;
+    private static final int TOTAL_READ_RATIO = 30;
+    private static final int NON_ASCII_THREASHOLD = 256;
+    private static final int ASCII_CHAR_THREASHOLD = 8;
 
     public static final boolean isBinary(final Document document) {
         // TODO: reimplement the binary test algorithm?
         // TODO: more efficient to move into standard analysis
         // TODO: then use binary as default
-        return isBinary(document.getName())
-            ||
-            // try a taste
-            isBinaryDocument(document);
+        return isBinary(document.getName()) ||
+        // try a taste
+                isBinaryDocument(document);
     }
 
-
-
 }

Modified: 
creadur/rat/branches/gsoc/apache-rat-core/src/test/java/org/apache/rat/document/impl/guesser/BinaryGuesserTest.java
URL: 
http://svn.apache.org/viewvc/creadur/rat/branches/gsoc/apache-rat-core/src/test/java/org/apache/rat/document/impl/guesser/BinaryGuesserTest.java?rev=1548278&r1=1548277&r2=1548278&view=diff
==============================================================================
--- 
creadur/rat/branches/gsoc/apache-rat-core/src/test/java/org/apache/rat/document/impl/guesser/BinaryGuesserTest.java
 (original)
+++ 
creadur/rat/branches/gsoc/apache-rat-core/src/test/java/org/apache/rat/document/impl/guesser/BinaryGuesserTest.java
 Thu Dec  5 20:33:26 2013
@@ -18,75 +18,69 @@
  */
 package org.apache.rat.document.impl.guesser;
 
-import org.apache.rat.document.MockDocument;
-import org.apache.rat.document.impl.FileDocument;
-import org.junit.Test;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 import java.io.IOException;
 import java.io.Reader;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import org.apache.rat.document.MockDocument;
+import org.apache.rat.document.impl.FileDocument;
+import org.junit.Test;
 
 public class BinaryGuesserTest {
 
     @Test
     public void testMatches() {
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("image.png")));
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("image.pdf")));
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("image.gif")));
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("image.giff")));
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("image.tif")));
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("image.tiff")));
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("image.jpg")));
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("image.jpeg")));
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("image.exe")));
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("Whatever.class")));
-        assertTrue(BinaryGuesser.isBinary(new MockDocument("data.dat")));
-        assertTrue(BinaryGuesser.isBinary(new 
MockDocument("libicudata.so.34.")));
-    }
-
-    public void testIsBinary() {
-        assertTrue(BinaryGuesser.isBinary("image.png"));
-        assertTrue(BinaryGuesser.isBinary("image.pdf"));
-        assertTrue(BinaryGuesser.isBinary("image.gif"));
-        assertTrue(BinaryGuesser.isBinary("image.giff"));
-        assertTrue(BinaryGuesser.isBinary("image.tif"));
-        assertTrue(BinaryGuesser.isBinary("image.tiff"));
-        assertTrue(BinaryGuesser.isBinary("image.jpg"));
-        assertTrue(BinaryGuesser.isBinary("image.jpeg"));
-        assertTrue(BinaryGuesser.isBinary("image.exe"));
-        assertTrue(BinaryGuesser.isBinary("Whatever.class"));
-        assertTrue(BinaryGuesser.isBinary("data.dat"));
-        assertTrue(BinaryGuesser.isBinary("libicudata.so.34."));
+        assertThatDocumentIsBinary("image.png");
+        assertThatDocumentIsBinary("image.pdf");
+        assertThatDocumentIsBinary("image.gif");
+        assertThatDocumentIsBinary("image.giff");
+        assertThatDocumentIsBinary("image.tif");
+        assertThatDocumentIsBinary("image.tiff");
+        assertThatDocumentIsBinary("image.jpg");
+        assertThatDocumentIsBinary("image.jpeg");
+        assertThatDocumentIsBinary("image.exe");
+        assertThatDocumentIsBinary("Whatever.class");
+        assertThatDocumentIsBinary("data.dat");
+        assertThatDocumentIsBinary("libicudata.so.34.");
+    }
+
+    private void assertThatDocumentIsBinary(final String name) {
+        assertTrue(BinaryGuesser.isBinary(new MockDocument(name)));
     }
 
     /**
-     * Used to swallow a MalformedInputException and return false
-     * because the encoding of the stream was different from the
-     * platform's default encoding.
-     *
+     * Used to swallow a MalformedInputException and return false because the
+     * encoding of the stream was different from the platform's default
+     * encoding.
+     * 
      * @see "RAT-81"
      */
     @Test
     public void binaryWithMalformedInputRAT81() throws Throwable {
-        FileDocument doc = new FileDocument(new 
File("src/test/resources/binaries/UTF16_with_signature.xml"));
+        FileDocument doc =
+                new FileDocument(new File(
+                        
"src/test/resources/binaries/UTF16_with_signature.xml"));
         Reader r = null;
         try {
-            char[] dummy = new char[100];
+            final char[] dummy = new char[100];
             r = doc.reader();
             r.read(dummy);
             // if we get here, the UTF-16 encoded file didn't throw
             // any exception, try the UTF-8 encoded one
             r.close();
-            doc = new FileDocument(new 
File("src/test/resources/binaries/UTF8_with_signature.xml"));
+            doc =
+                    new FileDocument(
+                            new File(
+                                    
"src/test/resources/binaries/UTF8_with_signature.xml"));
             r = doc.reader();
             r.read(dummy);
-            // still here?  can't test on this platform
+            // still here? can't test on this platform
             System.err.println("Skipping testBinaryWithMalformedInput");
-        } catch (IOException e) {
-            if (r!= null) {
+        } catch (final IOException e) {
+            if (r != null) {
                 r.close();
             }
             r = null;
@@ -102,17 +96,20 @@ public class BinaryGuesserTest {
     public void realBinaryContent() {
         // This test is not accurate on all platforms
         if (System.getProperty("file.encoding").startsWith("ANSI")) {
-            assertTrue(BinaryGuesser.isBinary(new FileDocument(new 
File("src/test/resources/binaries/Image-png.not"))));
+            assertTrue(BinaryGuesser.isBinary(new FileDocument(new File(
+                    "src/test/resources/binaries/Image-png.not"))));
         }
     }
 
     @Test
     public void textualContent() {
-        assertFalse(BinaryGuesser.isBinary(new FileDocument(new 
File("src/test/resources/elements/Text.txt"))));
+        assertFalse(BinaryGuesser.isBinary(new FileDocument(new File(
+                "src/test/resources/elements/Text.txt"))));
     }
 
     @Test
     public void emptyFile() {
-        assertFalse(BinaryGuesser.isBinary(new FileDocument(new 
File("src/test/resources/elements/sub/Empty.txt"))));
+        assertFalse(BinaryGuesser.isBinary(new FileDocument(new File(
+                "src/test/resources/elements/sub/Empty.txt"))));
     }
 }


Reply via email to