From abef4b9fb6d0490c8a8f41d976133239228f679e Mon Sep 17 00:00:00 2001 From: chiba Date: Wed, 9 Jul 2003 08:17:32 +0000 Subject: [PATCH] enforced close() in a finally block. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@28 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- src/main/javassist/ClassPool.java | 9 ++++++-- src/main/javassist/ClassPoolTail.java | 10 ++++++-- src/main/javassist/URLClassPath.java | 33 +++++++++++++++------------ 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index 83788953..61314777 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -418,8 +418,13 @@ public class ClassPool { { ByteArrayOutputStream barray = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(barray); - write(classname, out, true); - out.close(); + try { + write(classname, out, true); + } + finally { + out.close(); + } + return barray.toByteArray(); } diff --git a/src/main/javassist/ClassPoolTail.java b/src/main/javassist/ClassPoolTail.java index 678badd2..0756cf44 100644 --- a/src/main/javassist/ClassPoolTail.java +++ b/src/main/javassist/ClassPoolTail.java @@ -256,8 +256,14 @@ final class ClassPoolTail extends ClassPool { throws NotFoundException, IOException { InputStream fin = openClassfile(classname); - byte[] b = readStream(fin); - fin.close(); + byte[] b; + try { + b = readStream(fin); + } + finally { + fin.close(); + } + return b; } diff --git a/src/main/javassist/URLClassPath.java b/src/main/javassist/URLClassPath.java index 9d56635b..279b20f1 100644 --- a/src/main/javassist/URLClassPath.java +++ b/src/main/javassist/URLClassPath.java @@ -98,23 +98,26 @@ public class URLClassPath implements ClassPath { directory + classname.replace('.', '/') + ".class"); int size = con.getContentLength(); InputStream s = con.getInputStream(); - if (size <= 0) - b = ClassPoolTail.readStream(s); - else { - b = new byte[size]; - int len = 0; - do { - int n = s.read(b, len, size - len); - if (n < 0) { - s.close(); - throw new IOException("the stream was closed: " - + classname); - } - len += n; - } while (len < size); + try { + if (size <= 0) + b = ClassPoolTail.readStream(s); + else { + b = new byte[size]; + int len = 0; + do { + int n = s.read(b, len, size - len); + if (n < 0) + throw new IOException("the stream was closed: " + + classname); + + len += n; + } while (len < size); + } + } + finally { + s.close(); } - s.close(); return b; } -- 2.39.5