Author: grossws
Date: Wed Sep 30 16:38:17 2015
New Revision: 1706072

URL: http://svn.apache.org/viewvc?rev=1706072&view=rev
Log:
TIKA-1752: use j.n.f.Path in o.a.tika.detect

Fixes #TIKA-1752.

Contributed by Yaniv Kunda <yaniv at kundas dot net>

Modified:
    
tika/trunk/tika-core/src/main/java/org/apache/tika/detect/NNExampleModelDetector.java
    
tika/trunk/tika-core/src/main/java/org/apache/tika/detect/TrainedModelDetector.java

Modified: 
tika/trunk/tika-core/src/main/java/org/apache/tika/detect/NNExampleModelDetector.java
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/detect/NNExampleModelDetector.java?rev=1706072&r1=1706071&r2=1706072&view=diff
==============================================================================
--- 
tika/trunk/tika-core/src/main/java/org/apache/tika/detect/NNExampleModelDetector.java
 (original)
+++ 
tika/trunk/tika-core/src/main/java/org/apache/tika/detect/NNExampleModelDetector.java
 Wed Sep 30 16:38:17 2015
@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
+import java.nio.file.Path;
 
 import org.apache.tika.mime.MediaType;
 
@@ -37,6 +38,10 @@ public class NNExampleModelDetector exte
                super();
        }
 
+       public NNExampleModelDetector(final Path modelFile) {
+               loadDefaultModels(modelFile);
+       }
+
        public NNExampleModelDetector(final File modelFile) {
                loadDefaultModels(modelFile);
        }
@@ -64,7 +69,6 @@ public class NNExampleModelDetector exte
                        throw new RuntimeException(
                                        "Unable to read the default media type 
registry", e);
                }
-
        }
 
        /**
@@ -85,21 +89,11 @@ public class NNExampleModelDetector exte
                // Get the core URL, and all the extensions URLs
                URL modelURL = classLoader.getResource(classPrefix
                                + EXAMPLE_NNMODEL_FILE);
-               InputStream stream = null;
-               try {
-                       stream = modelURL.openStream();
+               try (InputStream stream = modelURL.openStream()) {
                        loadDefaultModels(stream);
-
                } catch (IOException e) {
                        throw new RuntimeException(
                                        "Unable to read the default media type 
registry", e);
-               } finally {
-                       try {
-                               stream.close();
-                       } catch (IOException e) {
-                               throw new RuntimeException(
-                                               "Unable to read the default 
media type registry", e);
-                       }
                }
 
        }
@@ -114,11 +108,10 @@ public class NNExampleModelDetector exte
         */
        private void readDescription(final NNTrainedModelBuilder builder,
                        final String line) {
-               int numInputs = 0;
-               int numHidden = 0;
-               int numOutputs = 0;
-               String tline = line;
-               String[] sarr = tline.split("\t");
+               int numInputs;
+               int numHidden;
+               int numOutputs;
+               String[] sarr = line.split("\t");
 
                try {
                        MediaType type = MediaType.parse(sarr[1]);
@@ -134,7 +127,6 @@ public class NNExampleModelDetector exte
                        throw new RuntimeException(
                                        "Unable to parse the model 
configuration", e);
                }
-
        }
 
        /**
@@ -146,8 +138,7 @@ public class NNExampleModelDetector exte
         */
        private void readNNParams(final NNTrainedModelBuilder builder,
                        final String line) {
-               String tline = line;
-               String[] sarr = tline.split("\t");
+               String[] sarr = line.split("\t");
                int n = sarr.length;
                float[] params = new float[n];
                try {

Modified: 
tika/trunk/tika-core/src/main/java/org/apache/tika/detect/TrainedModelDetector.java
URL: 
http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/detect/TrainedModelDetector.java?rev=1706072&r1=1706071&r2=1706072&view=diff
==============================================================================
--- 
tika/trunk/tika-core/src/main/java/org/apache/tika/detect/TrainedModelDetector.java
 (original)
+++ 
tika/trunk/tika-core/src/main/java/org/apache/tika/detect/TrainedModelDetector.java
 Wed Sep 30 16:38:17 2015
@@ -17,16 +17,14 @@
 package org.apache.tika.detect;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.OutputStreamWriter;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Writer;
 import java.nio.ByteBuffer;
 import java.nio.channels.Channels;
 import java.nio.channels.ReadableByteChannel;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
@@ -84,7 +82,7 @@ public abstract class TrainedModelDetect
 
        /**
         * read the inputstream and build a byte frequence histogram
-        * 
+        *
         * @param input
         * @return
         * @throws IOException
@@ -92,7 +90,7 @@ public abstract class TrainedModelDetect
        protected float[] readByteFrequencies(final InputStream input)
                        throws IOException {
 
-               ReadableByteChannel inputChannel = null;
+               ReadableByteChannel inputChannel;
                try {
                        inputChannel = Channels.newChannel(input);
                        // long inSize = inputChannel.size();
@@ -139,45 +137,36 @@ public abstract class TrainedModelDetect
        }
 
        /**
-        * on testing purpose; this method write the histogram vector to a file.
-        * 
+        * for testing purposes; this method write the histogram vector to a 
file.
+        *
         * @param histogram
         * @throws IOException
         */
-       private synchronized void writeHisto(final float[] histogram)
+       private void writeHisto(final float[] histogram)
                        throws IOException {
-               String histPath = new 
TemporaryResources().createTemporaryFile().getAbsolutePath();
-               Writer writer = new OutputStreamWriter(new 
FileOutputStream(histPath), UTF_8);
-               int n = histogram.length;// excluding the last one for storing 
the
-                                                                       // max 
value
-               for (int i = 0; i < n; i++) {
-                       writer.write(new 
StringBuffer().append(histogram[i]).append("\t")
-                                       .toString());
-                       // writer.write(i + "\t");
+               Path histPath = new TemporaryResources().createTempFile();
+               try (Writer writer = Files.newBufferedWriter(histPath, UTF_8)) {
+                       for (float bin : histogram) {
+                               writer.write(String.valueOf(bin) + "\t");
+                               // writer.write(i + "\t");
+                       }
+                       writer.write("\r\n");
                }
-
-               writer.write("\r\n");
-               writer.flush();
        }
 
-       public void loadDefaultModels(final File modelFile) {
-               FileInputStream in = null;
-               try {
-                       in = new FileInputStream(modelFile);
+       public void loadDefaultModels(Path modelFile) {
+               try (InputStream in = Files.newInputStream(modelFile)) {
                        loadDefaultModels(in);
-               } catch (FileNotFoundException e) {
+               } catch (IOException e) {
                        throw new RuntimeException(
                                        "Unable to read the default media type 
registry", e);
-               } finally {
-                       try {
-                               in.close();
-                       } catch (IOException e) {
-                               throw new RuntimeException(
-                                               "Unable to read the default 
media type registry", e);
-                       }
                }
        }
 
+       public void loadDefaultModels(File modelFile) {
+               loadDefaultModels(modelFile.toPath());
+       }
+
        public abstract void loadDefaultModels(final InputStream modelStream);
 
        public abstract void loadDefaultModels(final ClassLoader classLoader);


Reply via email to