harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjanu...@apache.org
Subject svn commit: r632003 - in /harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200: BandSet.java ClassBands.java Codec.java PopulationCodec.java Segment.java
Date Thu, 28 Feb 2008 15:25:50 GMT
Author: sjanuary
Date: Thu Feb 28 07:25:44 2008
New Revision: 632003

URL: http://svn.apache.org/viewvc?rev=632003&view=rev
Log:
Fix for HARMONY-5507 ([pack200][classlib] LocalVariableTableAttribute localVariableTableSpanO
can be negative) and some changes to Codec that avoid copying lots of long[]s to int[]s.

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/ClassBands.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Codec.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.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=632003&r1=632002&r2=632003&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 Feb 28 07:25:44 2008
@@ -68,17 +68,99 @@
      *             invalid
      */
     public int[] decodeBandInt(String name, InputStream in,
-            BHSDCodec defaultCodec, int count) throws IOException,
+            BHSDCodec codec, int count) throws IOException,
             Pack200Exception {
-        // TODO Might be able to improve this directly.
-        int[] result = new int[count];
-        long[] longResult = decodeBandLong(name, in, defaultCodec, count);
-        for (int i = 0; i < count; i++) {
-            result[i] = (int) longResult[i];
+        return decodeBandInt(name, in, codec, count, true);
+    }
+
+    /**
+     * Decode a band and return an array of <code>int</code> values
+     *
+     * @param name
+     *            the name of the band (primarily for logging/debugging
+     *            purposes)
+     * @param in
+     *            the InputStream to decode from
+     * @param defaultCodec
+     *            the default codec for this band
+     * @param count
+     *            the number of elements to read
+     * @return an array of decoded <code>int</code> values
+     * @throws IOException
+     *             if there is a problem reading from the underlying input
+     *             stream
+     * @throws Pack200Exception
+     *             if there is a problem decoding the value or that the value is
+     *             invalid
+     */
+    public int[] decodeBandInt(String name, InputStream in,
+            BHSDCodec codec, int count, boolean negativesAllowed) throws IOException,
+            Pack200Exception {
+        int[] band;
+        Codec codecUsed = codec;
+        if (codec.getB() == 1 || count == 0) {
+            return codec.decodeInts(count, in);
         }
-        return result;
+        int[] getFirst = codec.decodeInts(1, in);
+        if (getFirst.length == 0) {
+            return getFirst;
+        }
+        int first = getFirst[0];
+        if (codec.isSigned() && first >= -256 && first <= -1) {
+            // Non-default codec should be used
+            codecUsed = CodecEncoding.getCodec((-1 - first),
+                    header.getBandHeadersInputStream(), codec);
+            band = codecUsed.decodeInts(count, in);
+        } else if (!codec.isSigned() && first >= codec.getL()
+                && first <= codec.getL() + 255) {
+            // Non-default codec should be used
+            codecUsed = CodecEncoding.getCodec(first
+                    - codec.getL(), header.getBandHeadersInputStream(), codec);
+            band = codecUsed.decodeInts(count, in);
+        } else {
+            // First element should not be discarded
+            band = codec.decodeInts(count - 1, in, first);
+        }
+        if(!negativesAllowed && codec != codecUsed) {
+            if(codecUsed instanceof BHSDCodec && ((BHSDCodec)codecUsed).isSigned())
{
+                for (int i = 0; i < band.length; i++) {
+                    while(band[i] < 0) {
+                        band[i] += ((BHSDCodec)codecUsed).cardinality();
+                    }
+                }
+            }
+        }
+        return band;
+    }
+
+    /**
+     * Decode a band and return an array of <code>int[]</code> values
+     *
+     * @param name
+     *            the name of the band (primarily for logging/debugging
+     *            purposes)
+     * @param in
+     *            the InputStream to decode from
+     * @param defaultCodec
+     *            the default codec for this band
+     * @param counts
+     *            the numbers of elements to read for each int array within the
+     *            array to be returned
+     * @return an array of decoded <code>int[]</code> values
+     * @throws IOException
+     *             if there is a problem reading from the underlying input
+     *             stream
+     * @throws Pack200Exception
+     *             if there is a problem decoding the value or that the value is
+     *             invalid
+     */
+    public int[][] decodeBandInt(String name, InputStream in,
+            BHSDCodec defaultCodec, int[] counts)
+            throws IOException, Pack200Exception {
+        return decodeBandInt(name, in, defaultCodec, counts, true);
     }
 
+    // TODO: Use this version for all bands that shouldn't have negatives.
     /**
      * Decode a band and return an array of <code>int[]</code> values
      *
@@ -100,13 +182,15 @@
      *             if there is a problem decoding the value or that the value is
      *             invalid
      */
-    public int[][] decodeBandInt(String name, InputStream in, BHSDCodec defaultCodec, int[]
counts) throws IOException, Pack200Exception {
+    public int[][] decodeBandInt(String name, InputStream in,
+            BHSDCodec defaultCodec, int[] counts, boolean negativesAllowed)
+            throws IOException, Pack200Exception {
         int[][] result = new int[counts.length][];
         int totalCount = 0;
         for (int i = 0; i < counts.length; i++) {
             totalCount += counts[i];
         }
-        int[] twoDResult = decodeBandInt(name, in, defaultCodec, totalCount);
+        int[] twoDResult = decodeBandInt(name, in, defaultCodec, totalCount, negativesAllowed);
         int index = 0;
         for (int i = 0; i < result.length; i++) {
             result[i] = new int[counts[i]];
@@ -325,37 +409,33 @@
     }
 
     private int[] decodeBandInt(String name, InputStream in, BHSDCodec codec, int count,
int maxValue) throws IOException, Pack200Exception {
-        long[] band;
+        int[] band;
         Codec codecUsed = codec;
         if (codec.getB() == 1 || count == 0) {
-            band = codec.decode(count, in);
+            band = codec.decodeInts(count, in);
         } else {
-            long[] getFirst = codec.decode(1, in);
+            int[] getFirst = codec.decodeInts(1, in);
             if (getFirst.length == 0) {
                 return new int[0];
             }
-            long first = getFirst[0];
+            int first = getFirst[0];
             if (codec.isSigned() && first >= -256 && first <= -1) {
                 // Non-default codec should be used
-                codecUsed = CodecEncoding.getCodec((int) (-1 - first),
+                codecUsed = CodecEncoding.getCodec((-1 - first),
                         header.getBandHeadersInputStream(), codec);
-                band = codecUsed.decode(count, in);
+                band = codecUsed.decodeInts(count, in);
             } else if (!codec.isSigned() && first >= codec.getL()
                     && first <= codec.getL() + 255) {
                 // Non-default codec should be used
-                codecUsed = CodecEncoding.getCodec((int) first
+                codecUsed = CodecEncoding.getCodec(first
                         - codec.getL(), header.getBandHeadersInputStream(), codec);
-                band = codecUsed.decode(count, in);
+                band = codecUsed.decodeInts(count, in);
             } else {
                 // First element should not be discarded
-                band = codec.decode(count - 1, in, first);
+                band = codec.decodeInts(count - 1, in, first);
             }
         }
 
-        int[] returnBand = new int[band.length];
-        for (int i = 0; i < returnBand.length; i++) {
-            returnBand[i] = (int)band[i];
-        }
 
         /*
          * Note - this is not in the spec, but seems to be used as an
@@ -365,35 +445,34 @@
          * inside the range anyway.
          */
         if (codecUsed instanceof BHSDCodec) {
-            for (int i = 0; i < returnBand.length; i++) {
-                while (returnBand[i] < 0) {
-                    returnBand[i] += ((BHSDCodec) codecUsed).cardinality();
+            for (int i = 0; i < band.length; i++) {
+                while (band[i] < 0) {
+                    band[i] += ((BHSDCodec) codecUsed).cardinality();
                 }
-                while (returnBand[i] > maxValue) {
-                    returnBand[i] -= ((BHSDCodec) codecUsed).cardinality();
+                while (band[i] > maxValue) {
+                    band[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;
+            for (int i = 0; i < band.length; i++) {
+                if(band[i] < 0 || band[i] > maxValue) {
+                    boolean favouredValue = Arrays.binarySearch(favoured, band[i]) > -1;
                     Codec theCodec = favouredValue ? popCodec.getFavouredCodec(): popCodec.getUnvafouredCodec();
                     if(theCodec instanceof BHSDCodec) {
-                        while (returnBand[i] < 0) {
-                            returnBand[i] +=  ((BHSDCodec) theCodec).cardinality();
+                        while (band[i] < 0) {
+                            band[i] +=  ((BHSDCodec) theCodec).cardinality();
                         }
-                        while (returnBand[i] > maxValue) {
-                            returnBand[i] -= ((BHSDCodec) theCodec).cardinality();
+                        while (band[i] > maxValue) {
+                            band[i] -= ((BHSDCodec) theCodec).cardinality();
                         }
                     }
                 }
             }
         }
-
-        return returnBand;
+        return band;
     }
 
     /**

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=632003&r1=632002&r2=632003&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
Thu Feb 28 07:25:44 2008
@@ -760,7 +760,7 @@
                 localVariableTableN);
         int[][] localVariableTableSpanO = decodeBandInt(
                 "code_LocalVariableTable_span_O", in, Codec.BRANCH5,
-                localVariableTableN);
+                localVariableTableN, false);
         CPUTF8[][] localVariableTableNameRU = stringsToCPUTF8(parseReferences(
                 "code_LocalVariableTable_name_RU", in, Codec.UNSIGNED5,
                 localVariableTableN, cpBands.getCpUTF8()));

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Codec.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Codec.java?rev=632003&r1=632002&r2=632003&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Codec.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Codec.java
Thu Feb 28 07:25:44 2008
@@ -94,133 +94,162 @@
  * canonical number. {@link CodecEncoding#canonicalCodec})
  */
 public abstract class Codec {
-	/**
-	 * BCI5 = (5,4): Used for storing branching information in bytecode.
-	 */
-	public static final BHSDCodec BCI5 = new BHSDCodec(5, 4);
-
-	/**
-	 * BRANCH5 = (5,4,2): Used for storing branching information in bytecode.
-	 */
-	public static final BHSDCodec BRANCH5 = new BHSDCodec(5, 4, 2);
-
-	/**
-	 * BYTE1 = (1,256): Used for storing plain bytes.
-	 */
-	public static final BHSDCodec BYTE1 = new BHSDCodec(1, 256);
-
-	/**
-	 * CHAR3 = (3,128): Used for storing text (UTF-8) strings. NB This isn't
-	 * quite the same as UTF-8, but has similar properties; ASCII characters
-	 * &lt; 127 are stored in a single byte.
-	 */
-	public static final BHSDCodec CHAR3 = new BHSDCodec(3, 128);
-
-	/**
-	 * DELTA5 = (5,64,1,1): Used for the majority of numerical codings where
-	 * there is a correlated sequence of signed values.
-	 */
-	public static final BHSDCodec DELTA5 = new BHSDCodec(5, 64, 1, 1);
-
-	/**
-	 * MDELTA5 = (5,64,2,1): Used for the majority of numerical codings where
-	 * there is a correlated sequence of signed values, but where most of them
-	 * are expected to be non-negative.
-	 */
-	public static final BHSDCodec MDELTA5 = new BHSDCodec(5, 64, 2, 1);
-
-	/**
-	 * SIGNED5 = (5,64,1): Used for small signed values.
-	 */
-	public static final BHSDCodec SIGNED5 = new BHSDCodec(5, 64, 1);
-
-	/**
-	 * UDELTA5 = (5,64,0,1): Used for the majority of numerical codings where
-	 * there is a correlated sequence of unsigned values.
-	 */
-	public static final BHSDCodec UDELTA5 = new BHSDCodec(5, 64, 0, 1);
-
-	/**
-	 * UNSIGNED5 = (5,64): Used for small unsigned values.
-	 */
-	public static final BHSDCodec UNSIGNED5 = new BHSDCodec(5, 64);
-
-	/**
-	 * Decode a sequence of bytes from the given input stream, returning the
-	 * value as a long. Note that this method can only be applied for non-delta
-	 * encodings.
-	 *
-	 * @param in
-	 *            the input stream to read from
-	 * @return the value as a long
-	 * @throws IOException
-	 *             if there is a problem reading from the underlying input
-	 *             stream
-	 * @throws Pack200Exception
-	 *             if the encoding is a delta encoding
-	 */
-	public abstract long decode(InputStream in) throws IOException,
-			Pack200Exception;
-
-	/**
-	 * Decode a sequence of bytes from the given input stream, returning the
-	 * value as a long. If this encoding is a delta encoding (d=1) then the
-	 * previous value must be passed in as a parameter. If it is a non-delta
-	 * encoding, then it does not matter what value is passed in, so it makes
-	 * sense for the value to be passed in by default using code similar to:
-	 *
-	 * <pre>
-	 * long last = 0;
-	 * while (condition) {
-	 * 	last = codec.decode(in, last);
-	 * 	// do something with last
-	 * }
-	 * </pre>
-	 *
-	 * @param in
-	 *            the input stream to read from
-	 * @param last
-	 *            the previous value read, which must be supplied if the codec
-	 *            is a delta encoding
-	 * @return the value as a long
-	 * @throws IOException
-	 *             if there is a problem reading from the underlying input
-	 *             stream
-	 * @throws Pack200Exception
-	 *             if there is a problem decoding the value or that the value is
-	 *             invalid
-	 */
-	public abstract long decode(InputStream in, long last) throws IOException,
-			Pack200Exception;
-
-	/**
-	 * Decodes a sequence of <code>n</code> values from <code>in</code>.
-	 * This should probably be used in most cases, since some codecs
-	 * (such as @{link PopCodec}) only work when the number of values
-	 * to be read is known.
-	 *
-	 * @param n
-	 *            the number of values to decode
-	 * @param in
-	 *            the input stream to read from
-	 * @return an array of <code>long</code> values corresponding to values
-	 *         decoded
-	 * @throws IOException
-	 *             if there is a problem reading from the underlying input
-	 *             stream
-	 * @throws Pack200Exception
-	 *             if there is a problem decoding the value or that the value is
-	 *             invalid
-	 */
-	public long[] decode(int n, InputStream in) throws IOException,
-			Pack200Exception {
-		long result[] = new long[n];
-		long last = 0;
-		for(int i=0;i<n;i++) {
-			result[i] = last = decode(in,last);
-		}
-		return result;
-	}
+    /**
+     * BCI5 = (5,4): Used for storing branching information in bytecode.
+     */
+    public static final BHSDCodec BCI5 = new BHSDCodec(5, 4);
+
+    /**
+     * BRANCH5 = (5,4,2): Used for storing branching information in bytecode.
+     */
+    public static final BHSDCodec BRANCH5 = new BHSDCodec(5, 4, 2);
+
+    /**
+     * BYTE1 = (1,256): Used for storing plain bytes.
+     */
+    public static final BHSDCodec BYTE1 = new BHSDCodec(1, 256);
+
+    /**
+     * CHAR3 = (3,128): Used for storing text (UTF-8) strings. NB This isn't
+     * quite the same as UTF-8, but has similar properties; ASCII characters
+     * &lt; 127 are stored in a single byte.
+     */
+    public static final BHSDCodec CHAR3 = new BHSDCodec(3, 128);
+
+    /**
+     * DELTA5 = (5,64,1,1): Used for the majority of numerical codings where
+     * there is a correlated sequence of signed values.
+     */
+    public static final BHSDCodec DELTA5 = new BHSDCodec(5, 64, 1, 1);
+
+    /**
+     * MDELTA5 = (5,64,2,1): Used for the majority of numerical codings where
+     * there is a correlated sequence of signed values, but where most of them
+     * are expected to be non-negative.
+     */
+    public static final BHSDCodec MDELTA5 = new BHSDCodec(5, 64, 2, 1);
+
+    /**
+     * SIGNED5 = (5,64,1): Used for small signed values.
+     */
+    public static final BHSDCodec SIGNED5 = new BHSDCodec(5, 64, 1);
+
+    /**
+     * UDELTA5 = (5,64,0,1): Used for the majority of numerical codings where
+     * there is a correlated sequence of unsigned values.
+     */
+    public static final BHSDCodec UDELTA5 = new BHSDCodec(5, 64, 0, 1);
+
+    /**
+     * UNSIGNED5 = (5,64): Used for small unsigned values.
+     */
+    public static final BHSDCodec UNSIGNED5 = new BHSDCodec(5, 64);
+
+    /**
+     * Decode a sequence of bytes from the given input stream, returning the
+     * value as a long. Note that this method can only be applied for non-delta
+     * encodings.
+     *
+     * @param in
+     *            the input stream to read from
+     * @return the value as a long
+     * @throws IOException
+     *             if there is a problem reading from the underlying input
+     *             stream
+     * @throws Pack200Exception
+     *             if the encoding is a delta encoding
+     */
+    public abstract long decode(InputStream in) throws IOException,
+            Pack200Exception;
+
+    /**
+     * Decode a sequence of bytes from the given input stream, returning the
+     * value as a long. If this encoding is a delta encoding (d=1) then the
+     * previous value must be passed in as a parameter. If it is a non-delta
+     * encoding, then it does not matter what value is passed in, so it makes
+     * sense for the value to be passed in by default using code similar to:
+     *
+     * <pre>
+     * long last = 0;
+     * while (condition) {
+     *  last = codec.decode(in, last);
+     *  // do something with last
+     * }
+     * </pre>
+     *
+     * @param in
+     *            the input stream to read from
+     * @param last
+     *            the previous value read, which must be supplied if the codec
+     *            is a delta encoding
+     * @return the value as a long
+     * @throws IOException
+     *             if there is a problem reading from the underlying input
+     *             stream
+     * @throws Pack200Exception
+     *             if there is a problem decoding the value or that the value is
+     *             invalid
+     */
+    public abstract long decode(InputStream in, long last) throws IOException,
+            Pack200Exception;
+
+    /**
+     * Decodes a sequence of <code>n</code> values from <code>in</code>.
+     * This should probably be used in most cases, since some codecs
+     * (such as @{link PopCodec}) only work when the number of values
+     * to be read is known.
+     *
+     * @param n
+     *            the number of values to decode
+     * @param in
+     *            the input stream to read from
+     * @return an array of <code>long</code> values corresponding to values
+     *         decoded
+     * @throws IOException
+     *             if there is a problem reading from the underlying input
+     *             stream
+     * @throws Pack200Exception
+     *             if there is a problem decoding the value or that the value is
+     *             invalid
+     */
+    public long[] decode(int n, InputStream in) throws IOException,
+            Pack200Exception {
+        long result[] = new long[n];
+        long last = 0;
+        for(int i=0;i<n;i++) {
+            result[i] = last = decode(in,last);
+        }
+        return result;
+    }
+
+    /**
+     * Decodes a sequence of <code>n</code> values from <code>in</code>.
+     * This should probably be used in most cases, since some codecs
+     * (such as @{link PopCodec}) only work when the number of values
+     * to be read is known.
+     *
+     * @param n
+     *            the number of values to decode
+     * @param in
+     *            the input stream to read from
+     * @return an array of <code>int</code> values corresponding to values
+     *         decoded
+     * @throws IOException
+     *             if there is a problem reading from the underlying input
+     *             stream
+     * @throws Pack200Exception
+     *             if there is a problem decoding the value or that the value is
+     *             invalid
+     */
+    public int[] decodeInts(int n, InputStream in) throws IOException,
+            Pack200Exception {
+        int result[] = new int[n];
+        int last = 0;
+        for(int i=0;i<n;i++) {
+            result[i] = last = (int) decode(in,last);
+        }
+        return result;
+    }
 
     /**
      * Decodes a sequence of <code>n</code> values from <code>in</code>.
@@ -247,6 +276,35 @@
         long last = firstValue;
         for(int i=1;i<n+1;i++) {
             result[i] = last = decode(in,last);
+        }
+        return result;
+    }
+
+    /**
+     * Decodes a sequence of <code>n</code> values from <code>in</code>.
+     *
+     * @param n
+     *            the number of values to decode
+     * @param in
+     *            the input stream to read from
+     * @param firstValue
+     *            the first value in the band if it has already been read
+     * @return an array of <code>int</code> values corresponding to values
+     *         decoded, with firstValue as the first value in the array.
+     * @throws IOException
+     *             if there is a problem reading from the underlying input
+     *             stream
+     * @throws Pack200Exception
+     *             if there is a problem decoding the value or that the value is
+     *             invalid
+     */
+    public int[] decodeInts(int n, InputStream in, int firstValue) throws IOException,
+            Pack200Exception {
+        int result[] = new int[n + 1];
+        result[0] = firstValue;
+        int last = firstValue;
+        for(int i=1;i<n+1;i++) {
+            result[i] = last = (int) decode(in,last);
         }
         return result;
     }

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=632003&r1=632002&r2=632003&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
Thu Feb 28 07:25:44 2008
@@ -20,77 +20,77 @@
 import java.io.InputStream;
 
 public class PopulationCodec extends Codec {
-	private Codec favouredCodec;
-	private Codec tokenCodec;
-	private Codec unvafouredCodec;
-	private int l;
+    private final Codec favouredCodec;
+    private Codec tokenCodec;
+    private final Codec unvafouredCodec;
+    private int l;
     private long[] favoured;
 
-	public PopulationCodec(Codec favouredCodec, Codec tableCodec, Codec unvafouredCodec) {
-		this.favouredCodec = favouredCodec;
-		this.tokenCodec = tableCodec;
-		this.unvafouredCodec = unvafouredCodec;
-	}
-
-	public PopulationCodec(Codec favouredCodec, int l, Codec unvafouredCodec) {
-		if (l >= 256 || l <=0)
-			throw new IllegalArgumentException("L must be between 1..255");
-		this.favouredCodec = favouredCodec;
-		this.l = l;
-		this.unvafouredCodec = unvafouredCodec;
-	}
-
-
-	public long decode(InputStream in) throws IOException, Pack200Exception {
-		throw new Pack200Exception("Population encoding does not work unless the number of elements
are known");
-	}
-
-
-	public long decode(InputStream in, long last) throws IOException,
-			Pack200Exception {
-		throw new Pack200Exception("Population encoding does not work unless the number of elements
are known");
-	}
-
-
-	public long[] decode(int n, InputStream in) throws IOException, Pack200Exception {
-		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;
-		long last = 0;
-		long value = 0; // TODO Are these sensible starting points?
-		int k = -1;
-		while( true ) {
-			last = value;
-			value = favouredCodec.decode(in,last);
-			if ( value == smallest || value == last)
-				break;
-			favoured[++k] = value;
-			if (Math.abs(smallest) > Math.abs(value)) {
-				smallest = value;
-			} else if (Math.abs(smallest) == Math.abs(value) ) {
-				// 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) {
-				tokenCodec = Codec.BYTE1;
-			} else {
-				// if k >= 256, b >= 2
-				int b = 1;
-				while(++b < 5 && tokenCodec == null) {
-					BHSDCodec codec = new BHSDCodec(b,256-l,0);
-					if (codec.encodes(k))
-						tokenCodec = codec;
-				}
-				if (tokenCodec == null)
-					throw new Pack200Exception("Cannot calculate token codec from " + k + " and " + l);
-			}
-		}
-		// read favorites
-		result = tokenCodec.decode(n, in);
+    public PopulationCodec(Codec favouredCodec, Codec tableCodec, Codec unvafouredCodec)
{
+        this.favouredCodec = favouredCodec;
+        this.tokenCodec = tableCodec;
+        this.unvafouredCodec = unvafouredCodec;
+    }
+
+    public PopulationCodec(Codec favouredCodec, int l, Codec unvafouredCodec) {
+        if (l >= 256 || l <=0)
+            throw new IllegalArgumentException("L must be between 1..255");
+        this.favouredCodec = favouredCodec;
+        this.l = l;
+        this.unvafouredCodec = unvafouredCodec;
+    }
+
+
+    public long decode(InputStream in) throws IOException, Pack200Exception {
+        throw new Pack200Exception("Population encoding does not work unless the number of
elements are known");
+    }
+
+
+    public long decode(InputStream in, long last) throws IOException,
+            Pack200Exception {
+        throw new Pack200Exception("Population encoding does not work unless the number of
elements are known");
+    }
+
+
+    public long[] decode(int n, InputStream in) throws IOException, Pack200Exception {
+        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;
+        long last = 0;
+        long value = 0; // TODO Are these sensible starting points?
+        int k = -1;
+        while( true ) {
+            last = value;
+            value = favouredCodec.decode(in,last);
+            if ( value == smallest || value == last)
+                break;
+            favoured[++k] = value;
+            if (Math.abs(smallest) > Math.abs(value)) {
+                smallest = value;
+            } else if (Math.abs(smallest) == Math.abs(value) ) {
+                // 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) {
+                tokenCodec = Codec.BYTE1;
+            } else {
+                // if k >= 256, b >= 2
+                int b = 1;
+                while(++b < 5 && tokenCodec == null) {
+                    BHSDCodec codec = new BHSDCodec(b,256-l,0);
+                    if (codec.encodes(k))
+                        tokenCodec = codec;
+                }
+                if (tokenCodec == null)
+                    throw new Pack200Exception("Cannot calculate token codec from " + k +
" and " + l);
+            }
+        }
+        // read favorites
+        result = tokenCodec.decode(n, in);
         // read unfavorites
         last = 0;
         for(int i = 0; i < n; i++) {
@@ -101,7 +101,17 @@
                 result[i] = favoured[index-1];
             }
         }
-		return result;
+        return result;
+    }
+
+
+    public int[] decodeInts(int n, InputStream in) throws IOException, Pack200Exception {
+        long[] result = decode(n, in);
+        int[] intRes = new int[result.length];
+        for (int i = 0; i < intRes.length; i++) {
+            intRes[i] = (int)result[i];
+        }
+        return intRes;
     }
 
     public long[] getFavoured() {

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=632003&r1=632002&r2=632003&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 Feb 28 07:25:44 2008
@@ -206,13 +206,13 @@
 		if(ic_local_sent && (ic_local.length == 0)) {
 		    addInnerClassesAttr = false;
 		}
-		
+
 		// If ic_local is not sent and ic_relevant is empty,
 		// don't add the inner class attribute.
 		if(!ic_local_sent && (ic_relevant.length == 0)) {
 		    addInnerClassesAttr = false;
 		}
-		
+
 		if(addInnerClassesAttr) {
 		    // Need to add the InnerClasses attribute to the
 		    // existing classFile attributes.
@@ -253,7 +253,7 @@
 	 * calculate what should be added as ic_stored.
 	 * @param ic_local IcTuple[] array of local transmitted tuples
 	 * @param ic_relevant IcTuple[] array of relevant tuples
-	 * @return IcTuple[] array of tuples to be stored. If ic_local 
+	 * @return IcTuple[] array of tuples to be stored. If ic_local
 	 *     is null or empty, the values returned may not be correct.
 	 *     The caller will have to determine if this is the case.
 	 */
@@ -266,22 +266,22 @@
 	        // If ic_local is null, this code doesn't get
 	        // executed - which means the list ends up being
 	        // ic_relevant.
-	        for(int index=0; index < ic_local.length; index++) { 
+	        for(int index=0; index < ic_local.length; index++) {
 	            result.add(ic_local[index]);
 	            resultCopy.add(ic_local[index]);
 	            localList.add(ic_local[index]);
 	        }
 	    }
-	    for(int index=0; index < ic_relevant.length; index++) { 
+	    for(int index=0; index < ic_relevant.length; index++) {
 	        result.add(ic_relevant[index]);
 	        resultCopy.add(ic_relevant[index]);
 	        relevantList.add(ic_relevant[index]);
 	    }
-	    
+
 	    // Since we're removing while iterating, iterate over
 	    // a copy.
 	    Iterator it = resultCopy.iterator();
-	    
+
 	    while(it.hasNext()) {
 	        IcTuple tuple = (IcTuple)it.next();
 	        if(localList.contains(tuple) && relevantList.contains(tuple)) {
@@ -339,7 +339,7 @@
     public void unpack(InputStream in, JarOutputStream out) throws IOException,
             Pack200Exception {
         if (!in.markSupported())
-            in = new BufferedInputStream(in);        
+            in = new BufferedInputStream(in);
         parseSegment(in);
         writeJar(out);
     }



Mime
View raw message