From 0947834ac950fb4dd59b719cbdddc1726e68674b Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Thu, 11 Jan 2007 23:19:44 +0000 Subject: Fixes for bugs found using FindBugs. --- .../com/amazon/carbonado/lob/AbstractBlob.java | 58 +++++++++++++--------- .../com/amazon/carbonado/lob/AbstractClob.java | 34 +++++++------ 2 files changed, 54 insertions(+), 38 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/lob') diff --git a/src/main/java/com/amazon/carbonado/lob/AbstractBlob.java b/src/main/java/com/amazon/carbonado/lob/AbstractBlob.java index 3da80e9..6440978 100644 --- a/src/main/java/com/amazon/carbonado/lob/AbstractBlob.java +++ b/src/main/java/com/amazon/carbonado/lob/AbstractBlob.java @@ -90,30 +90,34 @@ public abstract class AbstractBlob implements Blob { char[] buffer = new char[(int) charLength]; - Reader r = new InputStreamReader(openInputStream(), decoder); - try { - int offset = 0; - int amt; - while ((amt = r.read(buffer, offset, buffer.length - offset)) >= 0) { - offset += amt; - if (amt == 0 && offset >= buffer.length) { - // Expand capacity. - charLength *= 2; - if (charLength >= Integer.MAX_VALUE) { - charLength = Integer.MAX_VALUE; - } - if (charLength <= buffer.length) { - throw new IllegalArgumentException - ("Blob is too long to fit in a String"); + Reader r = new InputStreamReader(openInputStream(), decoder); + + try { + int offset = 0; + int amt; + while ((amt = r.read(buffer, offset, buffer.length - offset)) >= 0) { + offset += amt; + if (amt == 0 && offset >= buffer.length) { + // Expand capacity. + charLength *= 2; + if (charLength >= Integer.MAX_VALUE) { + charLength = Integer.MAX_VALUE; + } + if (charLength <= buffer.length) { + throw new IllegalArgumentException + ("Blob is too long to fit in a String"); + } + char[] newBuffer = new char[(int) charLength]; + System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); + buffer = newBuffer; } - char[] newBuffer = new char[(int) charLength]; - System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); - buffer = newBuffer; } - } - return new String(buffer, 0, offset); + return new String(buffer, 0, offset); + } finally { + r.close(); + } } catch (IOException e) { throw AbstractClob.toFetchException(e); } @@ -145,8 +149,11 @@ public abstract class AbstractBlob implements Blob { setLength(0); try { Writer w = new OutputStreamWriter(openOutputStream(), charset); - w.write(value); - w.close(); + try { + w.write(value); + } finally { + w.close(); + } } catch (IOException e) { throw AbstractClob.toPersistException(e); } @@ -159,8 +166,11 @@ public abstract class AbstractBlob implements Blob { try { DataOutputStream out = new DataOutputStream(openOutputStream()); Writer w = new OutputStreamWriter(out, charset); - w.write(value); - w.close(); + try { + w.write(value); + } finally { + w.close(); + } newLength = out.size(); } catch (IOException e) { throw AbstractClob.toPersistException(e); diff --git a/src/main/java/com/amazon/carbonado/lob/AbstractClob.java b/src/main/java/com/amazon/carbonado/lob/AbstractClob.java index 3ccb0f7..6e4e273 100644 --- a/src/main/java/com/amazon/carbonado/lob/AbstractClob.java +++ b/src/main/java/com/amazon/carbonado/lob/AbstractClob.java @@ -91,19 +91,22 @@ public abstract class AbstractClob implements Clob { try { Reader r = openReader(); - char[] buf = new char[iLen]; - int offset = 0; - int amt; - while ((amt = r.read(buf, offset, iLen - offset)) > 0) { - offset += amt; - } - r.close(); - - if (offset <= 0) { - return ""; + try { + char[] buf = new char[iLen]; + int offset = 0; + int amt; + while ((amt = r.read(buf, offset, iLen - offset)) > 0) { + offset += amt; + } + + if (offset <= 0) { + return ""; + } + + return new String(buf, 0, offset); + } finally { + r.close(); } - - return new String(buf, 0, offset); } catch (IOException e) { throw toFetchException(e); } @@ -140,8 +143,11 @@ public abstract class AbstractClob implements Clob { try { Writer w = openWriter(); - w.write(value); - w.close(); + try { + w.write(value); + } finally { + w.close(); + } } catch (IOException e) { throw toPersistException(e); } -- cgit v1.2.3