Author: dbrosius
Date: Thu Apr  7 20:00:12 2005
New Revision: 160509

URL: http://svn.apache.org/viewcvs?view=rev&rev=160509
Log:
Bug 34318: Move owned stream allocation out of the constructors, and into parse 
so that the lifetime of the stream can be controlled thru try/finally. Also 
don't close a stream if the ClassParser didn't open it.

Modified:
    jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassParser.java

Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassParser.java
URL: 
http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassParser.java?view=diff&r1=160508&r2=160509
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassParser.java 
(original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/classfile/ClassParser.java Thu 
Apr  7 20:00:12 2005
@@ -37,8 +37,9 @@
  */
 public final class ClassParser {
   private DataInputStream file;
-  private ZipFile         zip;
+  private boolean                fileOwned;
   private String          file_name;
+  private String          zip_file;
   private int             class_name_index, superclass_name_index;
   private int             major, minor; // Compiler version
   private int             access_flags; // Access rights of parsed class
@@ -59,7 +60,7 @@
    */
   public ClassParser(InputStream file, String file_name) {
     this.file_name = file_name;
-
+    fileOwned = false;
     String clazz = file.getClass().getName(); // Not a very clean solution ...
     is_zip = clazz.startsWith("java.util.zip.") || 
clazz.startsWith("java.util.jar.");
 
@@ -72,31 +73,25 @@
   /** Parse class from given .class file.
    *
    * @param file_name file name
-   * @throws IOException
    */
-  public ClassParser(String file_name) throws IOException
+  public ClassParser(String file_name)
   {    
     is_zip = false;
     this.file_name = file_name;
-    file = new DataInputStream(new BufferedInputStream
-                              (new FileInputStream(file_name), BUFSIZE));
+    fileOwned = true;
   }
 
   /** Parse class from given .class file in a ZIP-archive
    *
+   * @param zip_file zip file name
    * @param file_name file name
-   * @throws IOException
    */
-  public ClassParser(String zip_file, String file_name) throws IOException
+  public ClassParser(String zip_file, String file_name)
   {    
     is_zip = true;
-    zip = new ZipFile(zip_file);
-    ZipEntry entry = zip.getEntry(file_name);
-                  
+    fileOwned = true;  
+    this.zip_file = zip_file;
     this.file_name = file_name;
-
-    file = new DataInputStream(new 
BufferedInputStream(zip.getInputStream(entry),
-                                                      BUFSIZE));
   }
 
   /**
@@ -112,54 +107,71 @@
    */  
   public JavaClass parse() throws IOException, ClassFormatException
   {
-    /****************** Read headers ********************************/
-    // Check magic tag of class file
-    readID();
-
-    // Get compiler version
-    readVersion();
-
-    /****************** Read constant pool and related **************/
-    // Read constant pool entries
-    readConstantPool();
-       
-    // Get class information
-    readClassInfo();
-
-    // Get interface information, i.e., implemented interfaces
-    readInterfaces();
-
-    /****************** Read class fields and methods ***************/ 
-    // Read class fields, i.e., the variables of the class
-    readFields();
-
-    // Read class methods, i.e., the functions in the class
-    readMethods();
-
-    // Read class attributes
-    readAttributes();
-
-    // Check for unknown variables
-    //Unknown[] u = Unknown.getUnknownAttributes();
-    //for(int i=0; i < u.length; i++)
-    //  System.err.println("WARNING: " + u[i]);
-
-    // Everything should have been read now
-    //      if(file.available() > 0) {
-    //        int bytes = file.available();
-    //        byte[] buf = new byte[bytes];
-    //        file.read(buf);
+    ZipFile         zip = null;
     
-    //        if(!(is_zip && (buf.length == 1))) {
-    //         System.err.println("WARNING: Trailing garbage at end of " + 
file_name);
-    //         System.err.println(bytes + " extra bytes: " + 
Utility.toHexString(buf));
-    //        }
-    //      }
-
-    // Read everything of interest, so close the file
-    file.close();
-    if(zip != null)
-      zip.close();
+       try {
+               if (fileOwned) {
+                       if (is_zip) {
+                           zip = new ZipFile(zip_file);
+                           ZipEntry entry = zip.getEntry(file_name);
+                           file = new DataInputStream(new 
BufferedInputStream(zip.getInputStream(entry),
+                                              BUFSIZE));
+                       } else {
+                           file = new DataInputStream(new BufferedInputStream
+                                      (new FileInputStream(file_name), 
BUFSIZE));
+                       }
+               }
+           /****************** Read headers ********************************/
+           // Check magic tag of class file
+           readID();
+       
+           // Get compiler version
+           readVersion();
+       
+           /****************** Read constant pool and related **************/
+           // Read constant pool entries
+           readConstantPool();
+               
+           // Get class information
+           readClassInfo();
+       
+           // Get interface information, i.e., implemented interfaces
+           readInterfaces();
+       
+           /****************** Read class fields and methods ***************/ 
+           // Read class fields, i.e., the variables of the class
+           readFields();
+       
+           // Read class methods, i.e., the functions in the class
+           readMethods();
+       
+           // Read class attributes
+           readAttributes();
+       
+           // Check for unknown variables
+           //Unknown[] u = Unknown.getUnknownAttributes();
+           //for(int i=0; i < u.length; i++)
+           //  System.err.println("WARNING: " + u[i]);
+       
+           // Everything should have been read now
+           //      if(file.available() > 0) {
+           //        int bytes = file.available();
+           //        byte[] buf = new byte[bytes];
+           //        file.read(buf);
+           
+           //        if(!(is_zip && (buf.length == 1))) {
+           //          System.err.println("WARNING: Trailing garbage at end of 
" + file_name);
+           //          System.err.println(bytes + " extra bytes: " + 
Utility.toHexString(buf));
+           //        }
+           //      }
+       } finally {
+           // Read everything of interest, so close the file
+               if (fileOwned) {
+                       file.close();
+                       if (zip != null)
+                               zip.close();
+               }
+       }
 
     // Return the information we have gathered in a new object
     return new JavaClass(class_name_index, superclass_name_index, 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to