harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjanu...@apache.org
Subject svn commit: r644309 - in /harmony/enhanced/classlib/trunk/modules/pack200/src: main/java/org/apache/harmony/pack200/ main/java/org/apache/harmony/pack200/bytecode/ main/java5/org/apache/harmony/pack200/ test/java/org/apache/harmony/pack200/tests/
Date Thu, 03 Apr 2008 13:31:34 GMT
Author: sjanuary
Date: Thu Apr  3 06:31:33 2008
New Revision: 644309

URL: http://svn.apache.org/viewvc?rev=644309&view=rev
Log:
Pack200 - removed debug code and replaced with a logging implementation.  Also removed some
unused code.

Modified:
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Archive.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BandSet.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BcBands.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentHeader.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ByteCode.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java5/org/apache/harmony/pack200/Pack200UnpackerAdapter.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/AbstractBandsTestCase.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/BandSetTest.java

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Archive.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Archive.java?rev=644309&r1=644308&r2=644309&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Archive.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Archive.java
Thu Apr  3 06:31:33 2008
@@ -37,19 +37,13 @@
  */
 public class Archive {
 
-    private static final int LOG_LEVEL_VERBOSE = 2;
-
-    private static final int LOG_LEVEL_STANDARD = 1;
-
-    private static final int LOG_LEVEL_QUIET = 0;
-
     private InputStream inputStream;
 
     private final JarOutputStream outputStream;
 
     private boolean removePackFile;
 
-    private int logLevel = LOG_LEVEL_STANDARD;
+    private int logLevel = Segment.LOG_LEVEL_STANDARD;
 
     private FileOutputStream logFile;
 
@@ -59,6 +53,8 @@
 
     private String inputFileName;
 
+	private String outputFileName;
+
     /**
      * Creates an Archive with the given input and output file names.
      * @param inputFile
@@ -69,6 +65,7 @@
     public Archive(String inputFile, String outputFile)
             throws FileNotFoundException, IOException {
         this.inputFileName = inputFile;
+        this.outputFileName = outputFile;
         inputStream = new FileInputStream(inputFile);
         outputStream = new JarOutputStream(new FileOutputStream(outputFile));
     }
@@ -135,25 +132,42 @@
                     outputStream.closeEntry();
                 }
             } else {
+            	int i = 0;
                 while (available(inputStream)) {
+                	i++;
                     Segment segment = new Segment();
                     segment.setLogLevel(logLevel);
                     segment.setLogStream(logFile != null ? (OutputStream) logFile
                             : (OutputStream) System.out);
+                    if (i == 1) {
+						segment.log(Segment.LOG_LEVEL_VERBOSE,
+								"Unpacking from " + inputFileName + " to "
+										+ outputFileName);
+					}
+                    segment.log(Segment.LOG_LEVEL_VERBOSE, "Reading segment " + i);
                     if (overrideDeflateHint) {
                         segment.overrideDeflateHint(deflateHint);
                     }
                     segment.unpack(inputStream, outputStream);
                     outputStream.flush();
+
+                    if(inputStream instanceof FileInputStream) {
+                    	inputFileName = ((FileInputStream)inputStream).getFD().toString();
+                    }
                 }
             }
         } finally {
             try {
                 inputStream.close();
-            } catch (Exception e2) {}
+            } catch (Exception e) {}
             try {
                 outputStream.close();
-            } catch (Exception e2) {}
+            } catch (Exception e) {}
+            if(logFile != null) {
+            	try {
+            		logFile.close();
+            	} catch (Exception e) {}
+            }
         }
         if (removePackFile) {
             File file = new File(inputFileName);
@@ -183,17 +197,17 @@
 
     public void setVerbose(boolean verbose) {
         if (verbose) {
-            logLevel = LOG_LEVEL_VERBOSE;
-        } else if (logLevel == LOG_LEVEL_VERBOSE) {
-            logLevel = LOG_LEVEL_STANDARD;
+            logLevel = Segment.LOG_LEVEL_VERBOSE;
+        } else if (logLevel == Segment.LOG_LEVEL_VERBOSE) {
+            logLevel = Segment.LOG_LEVEL_STANDARD;
         }
     }
 
     public void setQuiet(boolean quiet) {
         if (quiet) {
-            logLevel = LOG_LEVEL_QUIET;
-        } else if (logLevel == LOG_LEVEL_QUIET) {
-            logLevel = LOG_LEVEL_QUIET;
+            logLevel = Segment.LOG_LEVEL_QUIET;
+        } else if (logLevel == Segment.LOG_LEVEL_QUIET) {
+            logLevel = Segment.LOG_LEVEL_QUIET;
         }
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BandSet.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BandSet.java?rev=644309&r1=644308&r2=644309&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BandSet.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BandSet.java
Thu Apr  3 06:31:33 2008
@@ -315,8 +315,7 @@
             }
         }
 
-        // TODO Remove debugging code
-        debug("Parsed *" + name + " (" + result.length + ")");
+        segment.log(Segment.LOG_LEVEL_VERBOSE, "Parsed *" + name + " (" + result.length +
")");
         return result;
     }
 
@@ -411,19 +410,6 @@
             pos += num;
         }
         return result;
-    }
-
-    /**
-     * This is a local debugging message to aid the developer in writing this
-     * class. It will be removed before going into production. If the property
-     * 'debug.pack200' is set, this will generate messages to stderr; otherwise,
-     * it will be silent.
-     *
-     * @param message
-     * @deprecated this should be removed from production code
-     */
-    protected void debug(String message) {
-        segment.debug(message);
     }
 
     public CPInteger[] parseCPIntReferences(String name, InputStream in, BHSDCodec codec,
int count) throws IOException, Pack200Exception {

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BcBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BcBands.java?rev=644309&r1=644308&r2=644309&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BcBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BcBands.java
Thu Apr  3 06:31:33 2008
@@ -262,7 +262,7 @@
                                     || nextInstruction == 169) {
                                 bcLocalCount ++;
                             } else {
-                                debug("Found unhandled " + ByteCode.getByteCode(nextInstruction));
+                            	segment.log(Segment.LOG_LEVEL_VERBOSE, "Found unhandled " +
ByteCode.getByteCode(nextInstruction));
                             }
                             i++;
                             break;
@@ -277,13 +277,11 @@
                        case 254: // byte_escape
                            bcEscCount++;
                            break;
-                       default: // unhandled specifically at this stage
+                       default:
                            if(endsWithLoad(codePacked) || endsWithStore(codePacked)) {
                                bcLocalCount++;
                            } else if (startsWithIf(codePacked)) {
                                bcLabelCount++;
-                           } else {
-                               debug("Found unhandled " + codePacked + " " + ByteCode.getByteCode(codePacked));
                            }
                        }
                    }
@@ -291,7 +289,7 @@
             }
         }
         // other bytecode bands
-        debug("Parsed *bc_codes (" + bcParsed + ")");
+       segment.log(Segment.LOG_LEVEL_VERBOSE, "Parsed *bc_codes (" + bcParsed + ")");
         bcCaseCount = decodeBandInt("bc_case_count", in, Codec.UNSIGNED5, bcCaseCountCount);
         int bcCaseValueCount = 0;
         for (int i = 0; i < bcCaseCount.length; i++) {

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java?rev=644309&r1=644308&r2=644309&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java
Thu Apr  3 06:31:33 2008
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -69,30 +70,11 @@
  */
 public class Segment {
 
+    public static final int LOG_LEVEL_VERBOSE = 2;
 
-	/**
-     * TODO: Do we need this method now we have Archive as the main entry point?
-     *
-	 * Decode a segment from the given input stream. This does not attempt to
-	 * re-assemble or export any class files, but it contains enough information
-	 * to be able to re-assemble class files by external callers.
-	 *
-	 * @param in
-	 *            the input stream to read from
-	 * @return a segment parsed from the input stream
-	 * @throws IOException
-	 *             if a problem occurs during reading from the underlying stream
-	 * @throws Pack200Exception
-	 *             if a problem occurs with an unexpected value or unsupported
-	 *             codec
-	 */
-	public static Segment parse(InputStream in) throws IOException,
-			Pack200Exception {
-		Segment segment = new Segment();
-        segment.parseSegment(in);
-		return segment;
-	}
+    public static final int LOG_LEVEL_STANDARD = 1;
 
+    public static final int LOG_LEVEL_QUIET = 0;
 
     private SegmentHeader header;
 
@@ -112,6 +94,10 @@
 
     private boolean deflateHint;
 
+	private int logLevel;
+
+	private PrintWriter logStream;
+
 	private ClassFile buildClassFile(int classNum) throws Pack200Exception {
 		ClassFile classFile = new ClassFile();
 		classFile.major = header.getDefaultClassMajorVersion(); // TODO If
@@ -215,9 +201,8 @@
 
 		// add inner class attribute (if required)
 		boolean addInnerClassesAttr = false;
-		// TODO: remove the debug info below
         String debugClass = getClassBands().getClassThis()[classNum];
-        SegmentUtils.debug("buildClassFile: class is " + debugClass);
+        log(LOG_LEVEL_VERBOSE, "Building the class file for: " + debugClass);
 		IcTuple[] ic_local = getClassBands().getIcLocal()[classNum];
 		boolean ic_local_sent = false;
 		if(ic_local != null) {
@@ -277,9 +262,9 @@
 		// sort CP according to cp_All
 		cp.resolve(this);
 		// print out entries
-		debug("Constant pool looks like:");
+		log(LOG_LEVEL_VERBOSE, "Constant pool looks like:");
 		for (i = 1; i <= cp.size(); i++) {
-			debug(String.valueOf(i) + ":" + String.valueOf(cp.get(i)));
+			log(LOG_LEVEL_VERBOSE, String.valueOf(i) + ":" + String.valueOf(cp.get(i)));
 		}
 		// NOTE the indexOf is only valid after the cp.resolve()
 		// build up remainder of file
@@ -359,8 +344,8 @@
 	 */
 	private void parseSegment(InputStream in) throws IOException,
 			Pack200Exception {
-		debug("-------");
-        header = new SegmentHeader();
+		log(LOG_LEVEL_VERBOSE, "-------");
+        header = new SegmentHeader(this);
         header.unpack(in);
         cpBands = new CpBands(this);
         cpBands.unpack(in);
@@ -393,23 +378,6 @@
         writeJar(out);
     }
 
-    /**
-     * This is a local debugging message to aid the developer in writing this
-     * class. It will be removed before going into production. If the property
-     * 'debug.pack200' is set, this will generate messages to stderr; otherwise,
-     * it will be silent.
-     *
-     * @param message
-     * @deprecated this should be removed from production code
-     */
-    protected void debug(String message) {
-        if (System.getProperty("debug.pack200") != null) {
-            System.err.println(message);
-        }
-    }
-
-
-
 	/**
 	 * Writes the segment to an output stream. The output stream should be
 	 * pre-buffered for efficiency. Also takes the same input stream for
@@ -520,15 +488,17 @@
 
 
     public void setLogLevel(int logLevel) {
-
+    	this.logLevel = logLevel;
     }
 
-    public void setLogStream(OutputStream stream) {
-
+    public void setLogStream(OutputStream logStream) {
+    	this.logStream = new PrintWriter(logStream);
     }
 
     public void log(int logLevel, String message) {
-
+    	if (this.logLevel >= logLevel) {
+    		logStream.println(message);
+    	}
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentHeader.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentHeader.java?rev=644309&r1=644308&r2=644309&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentHeader.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentHeader.java
Thu Apr  3 06:31:33 2008
@@ -75,12 +75,18 @@
 
     private SegmentOptions options;
 
+	private final Segment segment;
+
 
     /**
      * The magic header for a Pack200 Segment is 0xCAFED00D. I wonder where they
      * get their inspiration from ...
      */
     private static final int[] magic = { 0xCA, 0xFE, 0xD0, 0x0D };
+    
+    public SegmentHeader(Segment segment) {
+		this.segment = segment;
+	}
 
     public void unpack(InputStream in) throws IOException,
             Pack200Exception, Error, Pack200Exception {
@@ -328,8 +334,7 @@
      */
     private long[] decodeScalar(String name, InputStream in, BHSDCodec codec,
             int n) throws IOException, Pack200Exception {
-        // TODO Remove debugging code
-        debug("Parsed #" + name + " (" + n + ")");
+        segment.log(Segment.LOG_LEVEL_VERBOSE, "Parsed #" + name + " (" + n + ")");
         return codec.decode(n, in);
     }
 
@@ -355,7 +360,7 @@
     private long decodeScalar(String name, InputStream in, BHSDCodec codec)
             throws IOException, Pack200Exception {
         long ret =  codec.decode(in);
-        debug("Parsed #" + name + " as " + ret);
+        segment.log(Segment.LOG_LEVEL_VERBOSE, "Parsed #" + name + " as " + ret);
         return ret;
     }
 
@@ -394,7 +399,7 @@
      *             if a problem occurs with an unexpected value or unsupported
      *             codec
      */
-    private static void readFully(InputStream in, byte[] data)
+    private void readFully(InputStream in, byte[] data)
             throws IOException, Pack200Exception {
         int total = in.read(data);
         if (total == -1)
@@ -411,12 +416,4 @@
     public int getBandHeadersSize() {
         return bandHeadersSize;
     }
-
-    protected void debug(String message) {
-      if (System.getProperty("debug.pack200") != null) {
-          System.err.println(message);
-      }
-  }
-
-
 }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ByteCode.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ByteCode.java?rev=644309&r1=644308&r2=644309&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ByteCode.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ByteCode.java
Thu Apr  3 06:31:33 2008
@@ -20,7 +20,6 @@
 import java.io.IOException;
 
 import org.apache.harmony.pack200.Segment;
-import org.apache.harmony.pack200.SegmentUtils;
 import org.apache.harmony.pack200.bytecode.forms.ByteCodeForm;
 
 public class ByteCode extends ClassFileEntry {
@@ -134,7 +133,7 @@
 					break;
 
 				default:
-					SegmentUtils.debug("Unhandled resolve " + this);
+					throw new Error("Unhandled resolve " + this);
 				}
 			}
 		}

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java5/org/apache/harmony/pack200/Pack200UnpackerAdapter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java5/org/apache/harmony/pack200/Pack200UnpackerAdapter.java?rev=644309&r1=644308&r2=644309&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java5/org/apache/harmony/pack200/Pack200UnpackerAdapter.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java5/org/apache/harmony/pack200/Pack200UnpackerAdapter.java
Thu Apr  3 06:31:33 2008
@@ -24,9 +24,6 @@
 import java.util.jar.JarOutputStream;
 import java.util.jar.Pack200.Unpacker;
 
-import org.apache.harmony.pack200.Pack200Exception;
-import org.apache.harmony.pack200.Segment;
-
 /**
  * This class provides the binding between the standard Pack200 interface and
  * the internal interface for (un)packing. As this uses generics for the
@@ -47,8 +44,7 @@
 		completed(0);
 		try {
 			while (in.available() > 0) {
-				Segment s = Segment.parse(in);
-				s.writeJar(out);
+			    new Segment().unpack(in, out);
 				out.flush();
 			}
 		} catch (Pack200Exception e) {

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/AbstractBandsTestCase.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/AbstractBandsTestCase.java?rev=644309&r1=644308&r2=644309&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/AbstractBandsTestCase.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/AbstractBandsTestCase.java
Thu Apr  3 06:31:33 2008
@@ -34,7 +34,11 @@
     protected int[] numMethods = {1};
 
     public class MockSegmentHeader extends SegmentHeader {
-        public int getClassCount() {
+        public MockSegmentHeader(Segment segment) {
+			super(segment);
+		}
+
+		public int getClassCount() {
             return numClasses;
         }
 
@@ -73,7 +77,7 @@
         }
 
         public SegmentHeader getSegmentHeader() {
-            return new MockSegmentHeader();
+            return new MockSegmentHeader(this);
         }
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/BandSetTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/BandSetTest.java?rev=644309&r1=644308&r2=644309&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/BandSetTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/BandSetTest.java
Thu Apr  3 06:31:33 2008
@@ -19,7 +19,6 @@
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 
 import junit.framework.TestCase;
 
@@ -34,14 +33,11 @@
 
     public class MockSegment extends Segment {
         public SegmentHeader getSegmentHeader() {
-            return new SegmentHeader();
+            return new SegmentHeader(this);
         }
     }
 
-    private BandSet bandSet = new BandSet(new MockSegment()) {
-
-        public void pack(OutputStream outputStream) {
-        }
+    private final BandSet bandSet = new BandSet(new MockSegment()) {
 
         public void unpack(InputStream inputStream) throws IOException, Pack200Exception
{
         }



Mime
View raw message