You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by pk...@apache.org on 2016/08/04 08:22:40 UTC

svn commit: r1755146 - in /uima/uimaj/trunk/uimaj-core/src: main/java/org/apache/uima/util/CasIOUtils.java test/java/org/apache/uima/util/CasIOUtilsTest.java

Author: pkluegl
Date: Thu Aug  4 08:22:40 2016
New Revision: 1755146

URL: http://svn.apache.org/viewvc?rev=1755146&view=rev
Log:
UIMA-4685
- take more care about streams
- remove additional methods
- make some methods private

Modified:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasIOUtils.java
    uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasIOUtils.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasIOUtils.java?rev=1755146&r1=1755145&r2=1755146&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasIOUtils.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/CasIOUtils.java Thu Aug  4 08:22:40 2016
@@ -26,16 +26,15 @@ import static org.apache.uima.cas.impl.S
 import static org.apache.uima.cas.impl.Serialization.serializeWithCompression;
 
 import java.io.BufferedInputStream;
+import java.io.Closeable;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.net.URL;
-import java.nio.file.Path;
 
 import org.apache.uima.cas.CAS;
 import org.apache.uima.cas.SerialFormat;
@@ -57,80 +56,6 @@ public class CasIOUtils {
 
   /**
    * 
-   * @param casPath
-   *          The path containing the CAS
-   * @param aCAS
-   *          The CAS that should be filled
-   * @return the SerialFormat of the loaded CAS
-   * @throws IOException
-   *           - Problem loading from given Path
-   */
-  public static SerialFormat load(Path casPath, CAS aCAS) throws IOException {
-
-    return load(casPath, null, aCAS, false);
-  }
-
-  /**
-   * 
-   * @param casPath
-   *          The path containing the CAS
-   * @param tsPath
-   *          The optional path containing the type system
-   * @param aCAS
-   *          The CAS that should be filled
-   * @param leniently
-   *          ignore feature structures of non-existing types
-   * @return the SerialFormat of the loaded CAS
-   * @throws IOException
-   *           - Problem loading from given Path
-   */
-  public static SerialFormat load(Path casPath, Path tsPath, CAS aCAS, boolean leniently)
-          throws IOException {
-
-    URL casUrl = casPath.toUri().toURL();
-    URL tsUrl = tsPath == null ? null : tsPath.toUri().toURL();
-    return load(casUrl, tsUrl, aCAS, leniently);
-  }
-
-  /**
-   * 
-   * @param casFile
-   *          The file containing the CAS
-   * @param aCAS
-   *          The CAS that should be filled
-   * @return the SerialFormat of the loaded CAS
-   * @throws IOException
-   *           - Problem loading from given File
-   */
-  public static SerialFormat load(File casFile, CAS aCAS) throws IOException {
-
-    return load(casFile, null, aCAS, false);
-  }
-
-  /**
-   * 
-   * @param casFile
-   *          The file containing the CAS
-   * @param tsFile
-   *          The optional file containing the type system
-   * @param aCAS
-   *          The CAS that should be filled
-   * @param leniently
-   *          ignore feature structures of non-existing types
-   * @return the SerialFormat of the loaded CAS
-   * @throws IOException
-   *           - Problem loading from given File
-   */
-  public static SerialFormat load(File casFile, File tsFile, CAS aCAS, boolean leniently)
-          throws IOException {
-
-    URL casUrl = casFile.toURI().toURL();
-    URL tsUrl = tsFile == null ? null : tsFile.toURI().toURL();
-    return load(casUrl, tsUrl, aCAS, leniently);
-  }
-
-  /**
-   * 
    * @param casUrl
    *          The url containing the CAS
    * @param aCAS
@@ -160,25 +85,41 @@ public class CasIOUtils {
    */
   public static SerialFormat load(URL casUrl, URL tsUrl, CAS aCAS, boolean leniently)
           throws IOException {
+    SerialFormat result = SerialFormat.UNKNOWN;
     String path = casUrl.getPath().toLowerCase();
-    if (path.endsWith(".xmi")) {
+
+    if (path.endsWith(SerialFormat.XMI.getDefaultFileExtension())) {
+      InputStream casIS = casUrl.openStream();
       try {
-        XmiCasDeserializer.deserialize(casUrl.openStream(), aCAS, leniently);
-        return SerialFormat.XMI;
+        XmiCasDeserializer.deserialize(casIS, aCAS, leniently);
+        result = SerialFormat.XMI;
       } catch (SAXException e) {
         throw new IOException(e);
+      } finally {
+        closeQuitely(casIS);
       }
-    } else if (path.endsWith(".xcas") || path.endsWith(".xml")) {
+    } else if (path.endsWith(SerialFormat.XCAS.getDefaultFileExtension())
+            || path.endsWith(".xml")) {
+      InputStream casIS = casUrl.openStream();
       try {
-        XCASDeserializer.deserialize(casUrl.openStream(), aCAS, leniently);
-        return SerialFormat.XCAS;
+        XCASDeserializer.deserialize(casIS, aCAS, leniently);
+        result = SerialFormat.XCAS;
       } catch (SAXException e) {
         throw new IOException(e);
+      } finally {
+        closeQuitely(casIS);
       }
+    } else {
+      InputStream casIS = casUrl.openStream();
+      InputStream tsIS = tsUrl == null ? null : tsUrl.openStream();
+      result = loadBinary(casIS, tsIS, aCAS);
+      closeQuitely(casIS);
+      closeQuitely(tsIS);
     }
-    return loadBinary(casUrl.openStream(), tsUrl == null ? null : tsUrl.openStream(), aCAS);
+    return result;
   }
 
+
   /**
    * This method tries to guess the format of the input stream. It supports binary format and XMI
    * but not XCAS
@@ -210,7 +151,8 @@ public class CasIOUtils {
    * @return the SerialFormat of the loaded CAS
    * @throws IOException
    *           - Problem loading from given InputStream
-   * @throws IllegalArgumentException - when trying to load XCAS   
+   * @throws IllegalArgumentException
+   *           - when trying to load XCAS
    */
   public static SerialFormat load(InputStream casInputStream, InputStream tsInputStream, CAS aCAS,
           boolean leniently) throws IOException {
@@ -237,21 +179,6 @@ public class CasIOUtils {
    * 
    * @param is
    *          The input stream of the CAS
-   * @param aCAS
-   *          the CAS in which the inpout stream will be deserialized
-   * @return the SerialFormat of the loaded CAS
-   * @throws IOException
-   *           - Problem loading from given InputStream
-   */
-  public static SerialFormat loadBinary(InputStream is, CAS aCAS) throws IOException {
-    return loadBinary(is, (CASMgrSerializer) null, aCAS);
-  }
-
-  /**
-   * Read CAS from the specified stream.
-   * 
-   * @param is
-   *          The input stream of the CAS
    * @param typeIS
    *          Optional stream from which typesystem information may be read. This is only used if
    *          the binary format read from the primary input stream does not already contain
@@ -262,7 +189,7 @@ public class CasIOUtils {
    * @throws IOException
    *           - Problem loading from given InputStream
    */
-  public static SerialFormat loadBinary(InputStream is, InputStream typeIS, CAS aCAS)
+  private static SerialFormat loadBinary(InputStream is, InputStream typeIS, CAS aCAS)
           throws IOException {
     CASMgrSerializer casMgr = null;
     if (typeIS != null) {
@@ -286,7 +213,7 @@ public class CasIOUtils {
    * @throws IOException
    *           - Problem loading from given InputStream
    */
-  public static SerialFormat loadBinary(InputStream is, CASMgrSerializer casMgr, CAS aCAS)
+  private static SerialFormat loadBinary(InputStream is, CASMgrSerializer casMgr, CAS aCAS)
           throws IOException {
     try {
       TypeSystemImpl ts = null;
@@ -360,9 +287,7 @@ public class CasIOUtils {
     } catch (ClassNotFoundException e) {
       throw new IOException(e);
     } finally {
-      if (is != null) {
-        is.close();
-      }
+      closeQuitely(is);
     }
   }
 
@@ -373,23 +298,6 @@ public class CasIOUtils {
    *          The CAS that should be serialized and stored
    * @param docOS
    *          The output stream for the CAS
-   * @param formatName
-   *          The format string in which the CAS should be stored.
-   * @throws IOException
-   *           - Problem saving to the given InputStream
-   */
-  public static void save(CAS aCas, OutputStream docOS, String formatName) throws IOException {
-    SerialFormat format = SerialFormat.valueOf(formatName);
-    save(aCas, docOS, null, format);
-  }
-
-  /**
-   * Write the CAS in the specified format.
-   * 
-   * @param aCas
-   *          The CAS that should be serialized and stored
-   * @param docOS
-   *          The output stream for the CAS
    * @param format
    *          The SerialFormat in which the CAS should be stored.
    * @throws IOException
@@ -481,7 +389,6 @@ public class CasIOUtils {
       throw new IOException(e);
     }
 
-    // To support writing to ZIPs, the type system must be written separately from the CAS data
     if (typeOS != null && !typeSystemWritten) {
       writeTypeSystem(aCas, typeOS);
       typeSystemWritten = true;
@@ -507,4 +414,14 @@ public class CasIOUtils {
     typeOS.writeObject(casMgrSerializer);
     typeOS.flush();
   }
+  
+  private static void closeQuitely(Closeable closeable) {
+    if (closeable != null) {
+      try {
+        closeable.close();
+      } catch (IOException e) {
+        // do nothing
+      }
+    }
+  }
 }

Modified: uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java?rev=1755146&r1=1755145&r2=1755146&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java Thu Aug  4 08:22:40 2016
@@ -57,18 +57,23 @@ public class CasIOUtilsTest extends Test
     FsIndexDescription[] indexes = UIMAFramework.getXMLParser().parseFsIndexCollection(new XMLInputSource(indexesFile))
             .getFsIndexes();
     cas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
-    CasIOUtils.load(JUnitExtension.getFile("ExampleCas/simpleCas.xmi"), cas);
+    FileInputStream casInputStream = new FileInputStream(JUnitExtension.getFile("ExampleCas/simpleCas.xmi"));
+    CasIOUtils.load(casInputStream, cas);
+    if(casInputStream != null) {
+      casInputStream.close();
+    }
   }
   
   public void testXMI() throws Exception {
     File casFile = new File("target/temp-test-output/simpleCas.xmi");
     casFile.getParentFile().mkdirs();
-    CasIOUtils.save(cas, new FileOutputStream(casFile), SerialFormat.XMI);
-    cas.reset();
-    CasIOUtils.load(casFile, cas);
-    Assert.assertEquals(SIMPLE_CAS_DEFAULT_INDEX_SIZE, cas.getAnnotationIndex().size());
-    cas.reset();
-    CasIOUtils.load(new FileInputStream(casFile), cas);
+    FileOutputStream docOS = new FileOutputStream(casFile);
+    CasIOUtils.save(cas, docOS, SerialFormat.XMI);
+    docOS.close();
+    cas.reset();
+    FileInputStream casInputStream = new FileInputStream(casFile);
+    CasIOUtils.load(casInputStream, cas);
+    casInputStream.close();
     Assert.assertEquals(SIMPLE_CAS_DEFAULT_INDEX_SIZE, cas.getAnnotationIndex().size());
     cas.reset();
     CasIOUtils.load(casFile.toURI().toURL(), cas);
@@ -78,10 +83,9 @@ public class CasIOUtilsTest extends Test
   public void testXCAS() throws Exception {
     File casFile = new File("target/temp-test-output/simpleCas.xcas");
     casFile.getParentFile().mkdirs();
-    CasIOUtils.save(cas, new FileOutputStream(casFile), SerialFormat.XCAS);
-    cas.reset();
-    CasIOUtils.load(casFile, cas);
-    Assert.assertEquals(SIMPLE_CAS_DEFAULT_INDEX_SIZE, cas.getAnnotationIndex().size());
+    FileOutputStream docOS = new FileOutputStream(casFile);
+    CasIOUtils.save(cas, docOS, SerialFormat.XCAS);
+    docOS.close();
     cas.reset();
     CasIOUtils.load(casFile.toURI().toURL(), cas);
     Assert.assertEquals(SIMPLE_CAS_DEFAULT_INDEX_SIZE, cas.getAnnotationIndex().size());
@@ -114,9 +118,13 @@ public class CasIOUtilsTest extends Test
   private void testFormat(SerialFormat format, String fileEnding) throws Exception {
     File casFile = new File("target/temp-test-output/simpleCas."+ fileEnding);
     casFile.getParentFile().mkdirs();
-    CasIOUtils.save(cas, new FileOutputStream(casFile), format);
-    cas.reset();
-    SerialFormat loadedFormat = CasIOUtils.load(new FileInputStream(casFile), cas);
+    FileOutputStream docOS = new FileOutputStream(casFile);
+    CasIOUtils.save(cas, docOS, format);
+    docOS.close();
+    cas.reset();
+    FileInputStream casInputStream = new FileInputStream(casFile);
+    SerialFormat loadedFormat = CasIOUtils.load(casInputStream, cas);
+    casInputStream.close();
     Assert.assertEquals(format, loadedFormat);
     Assert.assertEquals(SIMPLE_CAS_DEFAULT_INDEX_SIZE, cas.getAnnotationIndex().size());
   }
@@ -129,10 +137,13 @@ public class CasIOUtilsTest extends Test
     out.writeObject(new String("WRONG OBJECT"));
 
     byte[] casBytes = byteArrayOutputStream.toByteArray();
+    out.close();
+    ByteArrayInputStream casInputStream = new ByteArrayInputStream(casBytes);
     try {
-      CasIOUtils.load(new ByteArrayInputStream(casBytes), cas);
+      CasIOUtils.load(casInputStream, cas);
     } catch (Exception e) {
       Assert.assertTrue(e instanceof IOException);
+      casInputStream.close();
       return;
     }
     Assert.fail("An exception should have been thrown for wrong input.");
@@ -141,7 +152,7 @@ public class CasIOUtilsTest extends Test
   public void testWrongFormat() throws Exception {
     File casFile = new File("target/temp-test-output/simpleCas.wrong");
     try {
-      CasIOUtils.save(cas, new FileOutputStream(casFile), "WRONG");
+      CasIOUtils.save(cas, new FileOutputStream(casFile), SerialFormat.UNKNOWN);
     } catch (Exception e) {
 //      Assert.assertTrue(e instanceof IllegalArgumentException);
       return;