changeset 3506:5bf920749b97

7018385: update javax.sql classes to use try-with-resources Reviewed-by: alanb, lancea, darcy
author smarks
date Fri, 18 Feb 2011 12:43:14 -0800
parents dd143033cef1
children 42e4205db024 dbdafe65af60
files src/share/classes/javax/sql/rowset/serial/SerialClob.java src/share/classes/javax/sql/rowset/spi/SyncFactory.java
diffstat 2 files changed, 50 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/javax/sql/rowset/serial/SerialClob.java	Fri Feb 18 12:07:46 2011 +0530
+++ b/src/share/classes/javax/sql/rowset/serial/SerialClob.java	Fri Feb 18 12:43:14 2011 -0800
@@ -57,10 +57,10 @@
     private char buf[];
 
     /**
-     * Internal Clob representation if SerialClob is intialized with a
-     * Clob
+     * Internal Clob representation if SerialClob is initialized with a
+     * Clob. Null if SerialClob is initialized with a char[].
      */
-    private Clob clob;
+    private final Clob clob;
 
     /**
      * The length in characters of this <code>SerialClob</code> object's
@@ -71,12 +71,12 @@
     private long len;
 
     /**
-     * The original length in characters of tgus <code>SerialClob</code>
-     * objects internal array of characters.
+     * The original length in characters of this <code>SerialClob</code>
+     * object's internal array of characters.
      *
      * @serial
      */
-    private long origLen;
+    private final long origLen;
 
     /**
      * Constructs a <code>SerialClob</code> object that is a serialized version of
@@ -104,6 +104,7 @@
            buf[i] = ch[i];
         }
         origLen = len;
+        clob = null;
     }
 
     /**
@@ -117,19 +118,19 @@
      * the database. Otherwise, the new <code>SerialClob</code> object
      * object will contain no data.
      * <p>
-     * Note: The <code>Clob</code> object supplied to this constructor cannot
-     * return <code>null</code> for the <code>Clob.getCharacterStream()</code>
+     * Note: The <code>Clob</code> object supplied to this constructor must
+     * return non-null for both the <code>Clob.getCharacterStream()</code>
      * and <code>Clob.getAsciiStream</code> methods. This <code>SerialClob</code>
-     * constructor cannot  serialize a <code>Clob</code> object in this instance
+     * constructor cannot serialize a <code>Clob</code> object in this instance
      * and will throw an <code>SQLException</code> object.
      *
      * @param  clob the <code>Clob</code> object from which this
      *     <code>SerialClob</code> object is to be constructed; cannot be null
      * @throws SerialException if an error occurs during serialization
      * @throws SQLException if a SQL error occurs in capturing the CLOB;
-     *     if the <code>Clob</code> object is a null; or if both the
+     *     if the <code>Clob</code> object is a null; or if either of the
      *     <code>Clob.getCharacterStream()</code> and <code>Clob.getAsciiStream()</code>
-     *     methods on the <code>Clob</code> return a null
+     *     methods on the <code>Clob</code> returns a null
      * @see java.sql.Clob
      */
     public SerialClob(Clob clob) throws SerialException, SQLException {
@@ -144,19 +145,27 @@
         int read = 0;
         int offset = 0;
 
-        BufferedReader reader;
-        if ( (((reader = new BufferedReader(clob.getCharacterStream())) == null)) &&
-            (clob.getAsciiStream() == null)) {
-            throw new SQLException("Invalid Clob object. Calls to getCharacterStream " +
-                "and getAsciiStream return null which cannot be serialized.");
-        }
+        try (Reader charStream = clob.getCharacterStream()) {
+            if (charStream == null) {
+                throw new SQLException("Invalid Clob object. The call to getCharacterStream " +
+                    "returned null which cannot be serialized.");
+            }
 
-        try {
-            do {
-                read = reader.read(buf, offset, (int)(len - offset));
-                offset += read;
-            } while (read > 0);
+            // Note: get an ASCII stream in order to null-check it,
+            // even though we don't do anything with it.
+            try (InputStream asciiStream = clob.getAsciiStream()) {
+                if (asciiStream == null) {
+                    throw new SQLException("Invalid Clob object. The call to getAsciiStream " +
+                        "returned null which cannot be serialized.");
+                }
+            }
 
+            try (Reader reader = new BufferedReader(charStream)) {
+                do {
+                    read = reader.read(buf, offset, (int)(len - offset));
+                    offset += read;
+                } while (read > 0);
+            }
         } catch (java.io.IOException ex) {
             throw new SerialException("SerialClob: " + ex.getMessage());
         }
@@ -207,13 +216,13 @@
      *     used to create this <code>SerialClob</code> object
      */
     public java.io.InputStream getAsciiStream() throws SerialException, SQLException {
-       if (this.clob != null) {
-             return this.clob.getAsciiStream();
-         } else {
-             throw new SerialException("Unsupported operation. SerialClob cannot " +
+        if (this.clob != null) {
+            return this.clob.getAsciiStream();
+        } else {
+            throw new SerialException("Unsupported operation. SerialClob cannot " +
                 "return a the CLOB value as an ascii stream, unless instantiated " +
                 "with a fully implemented Clob object.");
-         }
+        }
     }
 
     /**
--- a/src/share/classes/javax/sql/rowset/spi/SyncFactory.java	Fri Feb 18 12:07:46 2011 +0530
+++ b/src/share/classes/javax/sql/rowset/spi/SyncFactory.java	Fri Feb 18 12:43:14 2011 -0800
@@ -32,6 +32,7 @@
 import javax.sql.*;
 
 import java.io.FileInputStream;
+import java.io.InputStream;
 import java.io.IOException;
 import java.io.FileNotFoundException;
 
@@ -366,7 +367,9 @@
                     // Load user's implementation of SyncProvider
                     // here. -Drowset.properties=/abc/def/pqr.txt
                     ROWSET_PROPERTIES = strRowsetProperties;
-                    properties.load(new FileInputStream(ROWSET_PROPERTIES));
+                    try (FileInputStream fis = new FileInputStream(ROWSET_PROPERTIES)) {
+                        properties.load(fis);
+                    }
                     parseProperties(properties);
                 }
 
@@ -376,12 +379,19 @@
                 ROWSET_PROPERTIES = "javax" + strFileSep + "sql" +
                         strFileSep + "rowset" + strFileSep +
                         "rowset.properties";
-                // properties.load(
-                //                ClassLoader.getSystemResourceAsStream(ROWSET_PROPERTIES));
 
                 ClassLoader cl = Thread.currentThread().getContextClassLoader();
 
-                properties.load(cl.getResourceAsStream(ROWSET_PROPERTIES));
+                try (InputStream stream =
+                         (cl == null) ? ClassLoader.getSystemResourceAsStream(ROWSET_PROPERTIES)
+                                      : cl.getResourceAsStream(ROWSET_PROPERTIES)) {
+                    if (stream == null) {
+                        throw new SyncFactoryException(
+                            "Resource " + ROWSET_PROPERTIES + " not found");
+                    }
+                    properties.load(stream);
+                }
+
                 parseProperties(properties);
 
             // removed else, has properties should sum together