harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjanu...@apache.org
Subject svn commit: r663163 - /harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/
Date Wed, 04 Jun 2008 15:10:30 GMT
Author: sjanuary
Date: Wed Jun  4 08:10:29 2008
New Revision: 663163

URL: http://svn.apache.org/viewvc?rev=663163&view=rev
Log:
Pack200 - more code for creating the segment header

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/AttributeDefinitionBands.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/ClassBands.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CpBands.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/FileBands.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/IcBands.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

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=663163&r1=663162&r2=663163&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
Wed Jun  4 08:10:29 2008
@@ -52,32 +52,26 @@
 
     public void pack() throws Pack200Exception, IOException {
         List classes = new ArrayList();
+        List files = new ArrayList();
         if(inputStream != null) {
             while(inputStream.available() > 0) {
                 JarEntry jarEntry = inputStream.getNextJarEntry();
                 if(jarEntry != null) {
-                    addJarEntry(jarEntry, inputStream, classes);
+                    addJarEntry(jarEntry, inputStream, classes, files);
                 }
             }
         } else {
             Enumeration jarEntries = jarFile.entries();
             while(jarEntries.hasMoreElements()) {
                 JarEntry jarEntry = (JarEntry) jarEntries.nextElement();
-                addJarEntry(jarEntry, jarFile.getInputStream(jarEntry), classes);
+                addJarEntry(jarEntry, jarFile.getInputStream(jarEntry), classes, files);
             }
         }
-        new Segment().pack(classes, outputStream);  // TODO: Multiple segments
+        new Segment().pack(classes, files, outputStream);  // TODO: Multiple segments
     }
 
-    private void addJarEntry(JarEntry jarEntry, InputStream stream, List javaClasses) throws
IOException, Pack200Exception {
+    private void addJarEntry(JarEntry jarEntry, InputStream stream, List javaClasses, List
files) throws IOException, Pack200Exception {
         String name = jarEntry.getName();
-//        long size = jarEntry.getSize();
-//        long compressedSize = jarEntry.getCompressedSize();
-//        char[] bytes = new char[(int)size];
-//        int bytesRead = new InputStreamReader(stream).read(bytes);
-//        if(bytesRead != size) {
-//            throw new Pack200Exception("An error occurred reading from the Jar file");
-//        }
         if(name.endsWith(".class")) {
             ClassParser classParser = new ClassParser(stream, name);
             JavaClass javaClass = classParser.parse();

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/AttributeDefinitionBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/AttributeDefinitionBands.java?rev=663163&r1=663162&r2=663163&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/AttributeDefinitionBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/AttributeDefinitionBands.java
Wed Jun  4 08:10:29 2008
@@ -17,20 +17,41 @@
 package org.apache.harmony.pack200;
 
 import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 import org.apache.bcel.classfile.Unknown;
 
 
 public class AttributeDefinitionBands extends BandSet {
 
-    public void pack(OutputStream out) {
-        // TODO Auto-generated method stub
+    private final SegmentHeader segmentHeader;
+    
+    private final Map namesToAttributes = new HashMap();
+
+    public AttributeDefinitionBands(SegmentHeader segmentHeader) {
+        this.segmentHeader = segmentHeader;
+    }
 
+    public void finaliseBands() {
+        segmentHeader.setAttribute_definition_count(namesToAttributes.keySet().size());
     }
 
-    public void addUnknownAttribute(Unknown obj) {
+    public void pack(OutputStream out) {
         // TODO Auto-generated method stub
 
     }
 
+    public void addUnknownAttribute(Unknown attribute) {
+        String name = attribute.getName();
+        List attributes = (List) namesToAttributes.get(name);
+        if(attributes == null) {
+            attributes = new ArrayList();
+            namesToAttributes.put(name, attributes);
+        }
+        attributes.add(attribute);
+    }
+
 }

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=663163&r1=663162&r2=663163&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
Wed Jun  4 08:10:29 2008
@@ -23,6 +23,10 @@
 
 public class BcBands extends BandSet {
 
+    public void finaliseBands() {
+        
+    }
+
     public void pack(OutputStream out) {
         // TODO Auto-generated method stub
 

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/ClassBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/ClassBands.java?rev=663163&r1=663162&r2=663163&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/ClassBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/ClassBands.java
Wed Jun  4 08:10:29 2008
@@ -50,6 +50,10 @@
     private final CPNameAndType[][] field_descr;
     private final CPNameAndType[][] method_descr;
 
+    public void finaliseBands() {
+        
+    }
+
     public void pack(OutputStream out) {
         // TODO Auto-generated method stub
 
@@ -64,7 +68,6 @@
             class_interface[index][i] = cpBands.getCPClass(interfaces[i]);
         }
 
-
         Field[] fields = obj.getFields();
         class_field_count[index] = fields.length;
         field_descr[index] = new CPNameAndType[fields.length];

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CpBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CpBands.java?rev=663163&r1=663162&r2=663163&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CpBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CpBands.java
Wed Jun  4 08:10:29 2008
@@ -37,6 +37,8 @@
 
 public class CpBands extends BandSet {
 
+    private final SegmentHeader segmentHeader;
+
     private final Set cp_Utf8 = new TreeSet();
     private final Set cp_Int = new TreeSet();
     private final Set cp_Float = new TreeSet();
@@ -55,6 +57,11 @@
     private final Map stringsToCpNameAndType = new HashMap();
     private final Map stringsToCpString = new HashMap();
     private final Map stringsToCpSignature = new HashMap();
+    
+    
+    public CpBands(SegmentHeader segmentHeader) {
+        this.segmentHeader = segmentHeader;
+    }
 
     public void pack(OutputStream out) {
         // TODO Auto-generated method stub
@@ -65,9 +72,22 @@
         this.currentConstantPool = cp;
     }
 
-    public void sortPool() {
+    public void finaliseBands() {
 
         System.out.println("pool");
+        
+        segmentHeader.setCp_Utf8_count(cp_Utf8.size());
+        segmentHeader.setCp_Int_count(cp_Int.size());
+        segmentHeader.setCp_Float_count(cp_Float.size());
+        segmentHeader.setCp_Long_count(cp_Long.size());
+        segmentHeader.setCp_Double_count(cp_Double.size());
+        segmentHeader.setCp_String_count(cp_String.size());
+        segmentHeader.setCp_Class_count(cp_Class.size());
+        segmentHeader.setCp_Signature_count(cp_Signature.size());
+        segmentHeader.setCp_Descr_count(cp_Descr.size());
+        segmentHeader.setCp_Field_count(cp_Field.size());
+        segmentHeader.setCp_Method_count(cp_Method.size());
+        segmentHeader.setCp_Imethod_count(cp_Imethod.size());
     }
 
     public void addConstantClass(ConstantClass constant) {

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/FileBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/FileBands.java?rev=663163&r1=663162&r2=663163&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/FileBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/FileBands.java
Wed Jun  4 08:10:29 2008
@@ -17,10 +17,23 @@
 package org.apache.harmony.pack200;
 
 import java.io.OutputStream;
+import java.util.List;
 
 
 public class FileBands extends BandSet {
 
+    private final List files;
+    private final SegmentHeader segmentHeader;
+
+    public FileBands(SegmentHeader segmentHeader, List files) {
+        this.segmentHeader = segmentHeader;
+        this.files = files;
+        int size = files.size();
+        if(size > 0) {
+            
+        }
+    }
+
     public void pack(OutputStream out) {
         // TODO Auto-generated method stub
 

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/IcBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/IcBands.java?rev=663163&r1=663162&r2=663163&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/IcBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/IcBands.java
Wed Jun  4 08:10:29 2008
@@ -26,6 +26,15 @@
 public class IcBands extends BandSet {
 
     private final List innerClasses = new ArrayList();
+    private final SegmentHeader segmentHeader;
+
+    public IcBands(SegmentHeader segmentHeader) {
+        this.segmentHeader = segmentHeader;
+    }
+
+    public void finaliseBands() {
+        segmentHeader.setIc_count(innerClasses.size());
+    }
 
     public void pack(OutputStream out) {
         // TODO Auto-generated method stub

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=663163&r1=663162&r2=663163&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
Wed Jun  4 08:10:29 2008
@@ -56,27 +56,33 @@
 import org.apache.bcel.classfile.Unknown;
 import org.apache.bcel.classfile.Visitor;
 
+
 public class Segment implements Visitor {
 
+    private SegmentHeader segmentHeader;
+    private CpBands cpBands;
     private AttributeDefinitionBands attributeDefinitionBands;
-    private BcBands bcBands;
+    private IcBands icBands;
     private ClassBands classBands;
-    private CpBands cpBands;
+    private BcBands bcBands;
     private FileBands fileBands;
-    private IcBands icBands;
-    private SegmentHeader segmentHeader;
 
-    public void pack(List classes, OutputStream out) throws IOException,
-            Pack200Exception {
+    public void pack(List classes, List files, OutputStream out) throws IOException, Pack200Exception
{
         segmentHeader = new SegmentHeader();
-        cpBands = new CpBands();
-        attributeDefinitionBands = new AttributeDefinitionBands();
-        icBands = new IcBands();
+        cpBands = new CpBands(segmentHeader);
+        attributeDefinitionBands = new AttributeDefinitionBands(segmentHeader);
+        icBands = new IcBands(segmentHeader);
         classBands = new ClassBands(cpBands, classes.size());
         bcBands = new BcBands();
-        fileBands = new FileBands();
+        fileBands = new FileBands(segmentHeader, files);
 
         processClasses(classes);
+        
+        cpBands.finaliseBands();
+        attributeDefinitionBands.finaliseBands();
+        icBands.finaliseBands();
+        classBands.finaliseBands();
+        bcBands.finaliseBands();
 
         segmentHeader.pack(out);
         cpBands.pack(out);
@@ -179,6 +185,8 @@
 
     public void visitJavaClass(JavaClass obj) {
         classBands.addClass(obj);
+        segmentHeader.addMinorVersion(obj.getMinor());
+        segmentHeader.addMajorVersion(obj.getMajor());
     }
 
     public void visitLineNumber(LineNumber obj) {
@@ -210,7 +218,6 @@
 
     public void visitSourceFile(SourceFile obj) {
         // TODO Auto-generated method stub
-
     }
 
     public void visitStackMap(StackMap obj) {

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=663163&r1=663162&r2=663163&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
Wed Jun  4 08:10:29 2008
@@ -64,9 +64,9 @@
     private boolean  have_code_flags_hi;
 
     private int ic_count;
-    private int default_class_minver;
-    private int default_class_majver;
     private int class_count;
+    private final Counter minverCounter = new Counter();
+    private final Counter majverCounter = new Counter();
 
     /**
      * Encode and write the SegmentHeader bands to the OutputStream
@@ -173,11 +173,6 @@
         cp_Imethod_count = count;
     }
 
-    public void setAttributeDefinition_count(int count) {
-        attribute_definition_count = count;
-    }
-
-
     public void setAttribute_definition_count(int attribute_definition_count) {
         this.attribute_definition_count = attribute_definition_count;
     }
@@ -257,17 +252,6 @@
         this.ic_count = ic_count;
     }
 
-
-    public void setDefault_class_minver(int default_class_minver) {
-        this.default_class_minver = default_class_minver;
-    }
-
-
-    public void setDefault_class_majver(int default_class_majver) {
-        this.default_class_majver = default_class_majver;
-    }
-
-
     public void setClass_count(int class_count) {
         this.class_count = class_count;
     }
@@ -290,6 +274,8 @@
     }
 
     private void writeClassCounts(OutputStream out) throws IOException, Pack200Exception
{
+        int default_class_minver = minverCounter.getMostCommon();
+        int default_class_majver = majverCounter.getMostCommon();
         out.write(encodeScalar(ic_count, Codec.UNSIGNED5));
         out.write(encodeScalar(default_class_minver, Codec.UNSIGNED5));
         out.write(encodeScalar(default_class_majver, Codec.UNSIGNED5));
@@ -313,4 +299,51 @@
         }
     }
 
+    public void addMinorVersion(int minor) {
+        minverCounter.add(minor);
+    }
+
+    public void addMajorVersion(int major) {
+        majverCounter.add(major);
+    }
+    
+    /**
+     * Counter for major/minor class file numbers so we can work out the default
+     */
+    private class Counter {
+        
+        private final int[] objs = new int[8];
+        private final int[] counts = new int[8];
+        private int length;
+        
+        public void add(int obj) {
+            boolean found = false;
+            for (int i = 0; i < length; i++) {
+                if(objs[i] == obj) {
+                    counts[i]++;
+                    found = true;
+                }
+            }
+            if(!found) {
+                objs[length] = obj;
+                counts[length] = 1;
+                length ++;
+                if(length > objs.length - 1) {
+                    Object[] newArray = new Object[objs.length + 8];
+                    System.arraycopy(objs, 0, newArray, 0, length);
+                }
+            }
+        }
+
+        public int getMostCommon() {
+            int returnIndex = 0;
+            for (int i = 0; i < length; i++) {
+                if(counts[i] > counts[returnIndex]) {
+                    returnIndex = i;
+                }
+            }
+            return objs[returnIndex];
+        }
+    }
+
 }
\ No newline at end of file



Mime
View raw message