You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2008/01/16 14:52:56 UTC

svn commit: r612458 - in /harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200: BandSet.java CodecEncoding.java PopulationCodec.java

Author: tellison
Date: Wed Jan 16 05:52:49 2008
New Revision: 612458

URL: http://svn.apache.org/viewvc?rev=612458&view=rev
Log:
Apply patch HARMONY-5392 ([classlib] [pack200] Population Codec doesn't always work properly)

Modified:
    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/CodecEncoding.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java

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=612458&r1=612457&r2=612458&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 Wed Jan 16 05:52:49 2008
@@ -19,6 +19,7 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Arrays;
 
 import org.apache.harmony.pack200.bytecode.CPClass;
 import org.apache.harmony.pack200.bytecode.CPDouble;
@@ -359,9 +360,9 @@
         /*
          * Note - this is not in the spec, but seems to be used as an
          * optimization by the RI for bands where the minimum and maximum values
-         * are known (ie reference bands). It will not hurt any encoding that is
-         * following the spec because all the values decoded will be inside the
-         * range anyway.
+         * are known (ie reference bands). It will not hurt any implementation
+         * that is following the spec because all the values decoded will be
+         * inside the range anyway.
          */
         if (codecUsed instanceof BHSDCodec) {
             for (int i = 0; i < returnBand.length; i++) {
@@ -370,6 +371,24 @@
                 }
                 while (returnBand[i] > maxValue) {
                     returnBand[i] -= ((BHSDCodec) codecUsed).cardinality();
+                }
+            }
+        } else if (codecUsed instanceof PopulationCodec) {
+            PopulationCodec popCodec = (PopulationCodec)codecUsed;
+            long[] favoured = (long[]) popCodec.getFavoured().clone();
+            Arrays.sort(favoured);
+            for (int i = 0; i < returnBand.length; i++) {
+                if(returnBand[i] < 0 || returnBand[i] > maxValue) {
+                    boolean favouredValue = Arrays.binarySearch(favoured, returnBand[i]) > -1;
+                    Codec theCodec = favouredValue ? popCodec.getFavouredCodec(): popCodec.getUnvafouredCodec();
+                    if(theCodec instanceof BHSDCodec) {
+                        while (returnBand[i] < 0) {
+                            returnBand[i] +=  ((BHSDCodec) theCodec).cardinality();
+                        }
+                        while (returnBand[i] > maxValue) {
+                            returnBand[i] -= ((BHSDCodec) theCodec).cardinality();
+                        }
+                    }
                 }
             }
         }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CodecEncoding.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CodecEncoding.java?rev=612458&r1=612457&r2=612458&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CodecEncoding.java (original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CodecEncoding.java Wed Jan 16 05:52:49 2008
@@ -123,7 +123,7 @@
 			int h = code + 1;
 			// This handles the special cases for invalid combinations of data.
 			return new BHSDCodec(b,h,s,d);			
-		} else if (value >= 117 && value <= 140) {
+		} else if (value >= 117 && value <= 140) { // Run codec
 			int offset = value - 117;
 			int kx = offset & 3;
 			boolean kbflag = (offset >> 2 & 1) == 1;
@@ -146,7 +146,7 @@
 				bCodec = getCodec(in.read(),in,defaultCodec); 
 			}
 			return new RunCodec(k,aCodec,bCodec);
-		} else if (value >= 141 && value <= 188) {
+		} else if (value >= 141 && value <= 188) { // Population Codec
 			int offset = value - 141;
 			boolean fdef = (offset & 1) == 1; 
 			boolean udef = (offset >> 1 & 1) == 1;
@@ -166,9 +166,9 @@
 				return new PopulationCodec(fCodec,l,uCodec);
 			} else {
 				Codec fCodec = (fdef ? defaultCodec : getCodec(in.read(),in,defaultCodec) );
+                Codec tCodec = getCodec(in.read(),in,defaultCodec);
 				Codec uCodec = (udef ? defaultCodec : getCodec(in.read(),in,defaultCodec) );
-				Codec tCodec = getCodec(in.read(),in,defaultCodec);
-				return new PopulationCodec(fCodec,uCodec,tCodec);
+				return new PopulationCodec(fCodec,tCodec,uCodec);
 			}
 		} else {
 			throw new Pack200Exception("Invalid codec encoding byte (" + value + ") found" );

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java?rev=612458&r1=612457&r2=612458&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java (original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java Wed Jan 16 05:52:49 2008
@@ -24,6 +24,7 @@
 	private Codec tokenCodec;
 	private Codec unvafouredCodec;
 	private int l;
+    private long[] favoured;
 	
 	public PopulationCodec(Codec favouredCodec, Codec tableCodec, Codec unvafouredCodec) {
 		this.favouredCodec = favouredCodec;
@@ -52,7 +53,7 @@
 
 	
 	public long[] decode(int n, InputStream in) throws IOException, Pack200Exception {
-		long favoured[] = new long[n]; // there must be <= n  values, but probably a lot less
+		favoured = new long[n]; // there must be <= n  values, but probably a lot less
 		long result[];
 		// read table of favorites first
 		long smallest = Long.MAX_VALUE;
@@ -71,7 +72,7 @@
 				// ensure that -X and +X -> +X
 				smallest = Math.abs(smallest);
 			}
-		} 
+		}
 		// if tokenCodec needs to be derived from the T, L and K values
 		if (tokenCodec == null) {
 			if (k < 256) {
@@ -101,5 +102,17 @@
             }
         }
 		return result;
+    }
+
+    public long[] getFavoured() {
+        return favoured;
+    }
+
+    public Codec getFavouredCodec() {
+        return favouredCodec;
+    }
+
+    public Codec getUnvafouredCodec() {
+        return unvafouredCodec;
     }
 }