7018385: update javax.sql classes to use try-with-resources

Reviewed-by: alanb, lancea, darcy
This commit is contained in:
Stuart Marks 2011-02-18 12:43:14 -08:00
parent 437b0f5f71
commit f0730a655c
2 changed files with 50 additions and 31 deletions

View File

@ -57,10 +57,10 @@ public class SerialClob implements Clob, Serializable, Cloneable {
private char buf[]; private char buf[];
/** /**
* Internal Clob representation if SerialClob is intialized with a * Internal Clob representation if SerialClob is initialized with a
* Clob * 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 * The length in characters of this <code>SerialClob</code> object's
@ -71,12 +71,12 @@ public class SerialClob implements Clob, Serializable, Cloneable {
private long len; private long len;
/** /**
* The original length in characters of tgus <code>SerialClob</code> * The original length in characters of this <code>SerialClob</code>
* objects internal array of characters. * object's internal array of characters.
* *
* @serial * @serial
*/ */
private long origLen; private final long origLen;
/** /**
* Constructs a <code>SerialClob</code> object that is a serialized version of * Constructs a <code>SerialClob</code> object that is a serialized version of
@ -104,6 +104,7 @@ public class SerialClob implements Clob, Serializable, Cloneable {
buf[i] = ch[i]; buf[i] = ch[i];
} }
origLen = len; origLen = len;
clob = null;
} }
/** /**
@ -117,8 +118,8 @@ public class SerialClob implements Clob, Serializable, Cloneable {
* the database. Otherwise, the new <code>SerialClob</code> object * the database. Otherwise, the new <code>SerialClob</code> object
* object will contain no data. * object will contain no data.
* <p> * <p>
* Note: The <code>Clob</code> object supplied to this constructor cannot * Note: The <code>Clob</code> object supplied to this constructor must
* return <code>null</code> for the <code>Clob.getCharacterStream()</code> * return non-null for both the <code>Clob.getCharacterStream()</code>
* and <code>Clob.getAsciiStream</code> methods. This <code>SerialClob</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. * and will throw an <code>SQLException</code> object.
@ -127,9 +128,9 @@ public class SerialClob implements Clob, Serializable, Cloneable {
* <code>SerialClob</code> object is to be constructed; cannot be null * <code>SerialClob</code> object is to be constructed; cannot be null
* @throws SerialException if an error occurs during serialization * @throws SerialException if an error occurs during serialization
* @throws SQLException if a SQL error occurs in capturing the CLOB; * @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> * <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 * @see java.sql.Clob
*/ */
public SerialClob(Clob clob) throws SerialException, SQLException { public SerialClob(Clob clob) throws SerialException, SQLException {
@ -144,19 +145,27 @@ public class SerialClob implements Clob, Serializable, Cloneable {
int read = 0; int read = 0;
int offset = 0; int offset = 0;
BufferedReader reader; try (Reader charStream = clob.getCharacterStream()) {
if ( (((reader = new BufferedReader(clob.getCharacterStream())) == null)) && if (charStream == null) {
(clob.getAsciiStream() == null)) { throw new SQLException("Invalid Clob object. The call to getCharacterStream " +
throw new SQLException("Invalid Clob object. Calls to getCharacterStream " + "returned null which cannot be serialized.");
"and getAsciiStream return null which cannot be serialized.");
} }
try { // 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 { do {
read = reader.read(buf, offset, (int)(len - offset)); read = reader.read(buf, offset, (int)(len - offset));
offset += read; offset += read;
} while (read > 0); } while (read > 0);
}
} catch (java.io.IOException ex) { } catch (java.io.IOException ex) {
throw new SerialException("SerialClob: " + ex.getMessage()); throw new SerialException("SerialClob: " + ex.getMessage());
} }

View File

@ -32,6 +32,7 @@ import java.sql.*;
import javax.sql.*; import javax.sql.*;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -366,7 +367,9 @@ public class SyncFactory {
// Load user's implementation of SyncProvider // Load user's implementation of SyncProvider
// here. -Drowset.properties=/abc/def/pqr.txt // here. -Drowset.properties=/abc/def/pqr.txt
ROWSET_PROPERTIES = strRowsetProperties; ROWSET_PROPERTIES = strRowsetProperties;
properties.load(new FileInputStream(ROWSET_PROPERTIES)); try (FileInputStream fis = new FileInputStream(ROWSET_PROPERTIES)) {
properties.load(fis);
}
parseProperties(properties); parseProperties(properties);
} }
@ -376,12 +379,19 @@ public class SyncFactory {
ROWSET_PROPERTIES = "javax" + strFileSep + "sql" + ROWSET_PROPERTIES = "javax" + strFileSep + "sql" +
strFileSep + "rowset" + strFileSep + strFileSep + "rowset" + strFileSep +
"rowset.properties"; "rowset.properties";
// properties.load(
// ClassLoader.getSystemResourceAsStream(ROWSET_PROPERTIES));
ClassLoader cl = Thread.currentThread().getContextClassLoader(); 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); parseProperties(properties);
// removed else, has properties should sum together // removed else, has properties should sum together