You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by sj...@apache.org on 2009/04/16 11:29:28 UTC
svn commit: r765535 - in
/harmony/enhanced/classlib/trunk/modules/pack200/src:
main/java/org/apache/harmony/pack200/
test/java/org/apache/harmony/pack200/tests/
Author: sjanuary
Date: Thu Apr 16 09:29:27 2009
New Revision: 765535
URL: http://svn.apache.org/viewvc?rev=765535&view=rev
Log:
Pack200 - code and tests for creating specifiers for run and population codecs
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
harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/RunCodec.java
harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodecEncodingTest.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=765535&r1=765534&r2=765535&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 16 09:29:27 2009
@@ -46,7 +46,7 @@
Codec betterCodec = lookForBetterCodec(name, ints, defaultCodec);
if(betterCodec != null) {
if(betterCodec instanceof BHSDCodec) {
- int[] specifierBand = CodecEncoding.getSpecifier(betterCodec);
+ int[] specifierBand = CodecEncoding.getSpecifier(betterCodec, defaultCodec);
int specifier = specifierBand[0];
if(specifierBand.length > 0) {
for (int i = 1; i < specifierBand.length; i++) {
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=765535&r1=765534&r2=765535&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 Thu Apr 16 09:29:27 2009
@@ -19,6 +19,7 @@
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -215,10 +216,10 @@
}
public static int getSpecifierForDefaultCodec(BHSDCodec defaultCodec) {
- return getSpecifier(defaultCodec)[0];
+ return getSpecifier(defaultCodec, null)[0];
}
- public static int[] getSpecifier(Codec codec) {
+ public static int[] getSpecifier(Codec codec, Codec defaultForBand) {
// lazy initialization
if(canonicalCodecsToSpecifiers == null) {
canonicalCodecsToSpecifiers = new HashMap();
@@ -238,6 +239,97 @@
* bhsdCodec.getS() + 8 * (bhsdCodec.getB()-1);
specifiers[2] = bhsdCodec.getH() - 1;
return specifiers;
+ } else if (codec instanceof RunCodec) {
+ RunCodec runCodec = (RunCodec) codec;
+ int k = runCodec.getK();
+ int kb;
+ int kx;
+ if(k <= 256) {
+ kb = 0;
+ kx = k - 1;
+ } else if (k <= 4096) {
+ kb = 1;
+ kx = k/16 - 1;
+ } else if (k <= 65536) {
+ kb = 2;
+ kx = k/256 - 1;
+ } else {
+ kb = 3;
+ kx = k/4096 - 1;
+ }
+ Codec aCodec = runCodec.getACodec();
+ Codec bCodec = runCodec.getBCodec();
+ int abDef = 0;
+ if(aCodec.equals(defaultForBand)) {
+ abDef = 1;
+ } else if (bCodec.equals(defaultForBand)) {
+ abDef = 2;
+ }
+ int first = 117 + kb + (kx==3 ? 0 : 4) + (8 * abDef);
+ int[] aSpecifier = abDef == 1 ? new int[0] : getSpecifier(aCodec, defaultForBand);
+ int[] bSpecifier = abDef == 2 ? new int[0] : getSpecifier(bCodec, defaultForBand);
+ int[] specifier = new int[1 + (kx==3 ? 0 : 1) + aSpecifier.length + bSpecifier.length];
+ specifier[0] = first;
+ int index = 1;
+ if(kx != 3) {
+ specifier[1] = kx;
+ index++;
+ }
+ for (int i = 0; i < aSpecifier.length; i++) {
+ specifier[index] = aSpecifier[i];
+ index++;
+ }
+ for (int i = 0; i < bSpecifier.length; i++) {
+ specifier[index] = bSpecifier[i];
+ index++;
+ }
+ return specifier;
+ } else if (codec instanceof PopulationCodec) {
+ PopulationCodec populationCodec = (PopulationCodec) codec;
+ Codec tokenCodec = populationCodec.getTokenCodec();
+ Codec favouredCodec = populationCodec.getFavouredCodec();
+ Codec unfavouredCodec = populationCodec.getUnfavouredCodec();
+ int fDef = favouredCodec.equals(defaultForBand) ? 1 : 0;
+ int uDef = unfavouredCodec.equals(defaultForBand) ? 1 : 0;
+ int tDefL = 0;
+ long[] favoured = populationCodec.getFavoured();
+ if(favoured != null) {
+ int k = favoured.length;
+ if(tokenCodec == Codec.BYTE1) {
+ tDefL = 1;
+ } else if (tokenCodec instanceof BHSDCodec) {
+ BHSDCodec tokenBHSD = (BHSDCodec) tokenCodec;
+ if(tokenBHSD.getS() == 0) {
+ int[] possibleLValues = new int[] {4, 8, 16, 32, 64, 128, 192, 224, 240, 248, 252};
+ int l = 256-tokenBHSD.getH();
+ int index = Arrays.binarySearch(possibleLValues, l);
+ if(index != -1) {
+ // TODO: check range is ok for ks
+ tDefL = index++;
+ }
+ }
+ }
+ }
+ int first = 141 + fDef + (2 * uDef) + (4 * tDefL);
+ int[] favouredSpecifier = fDef == 1 ? new int[0] : getSpecifier(favouredCodec, defaultForBand);
+ int[] tokenSpecifier = tDefL != 0 ? new int[0] : getSpecifier(tokenCodec, defaultForBand);
+ int[] unfavouredSpecifier = uDef == 1 ? new int[0] : getSpecifier(unfavouredCodec, defaultForBand);
+ int[] specifier = new int[1 + favouredSpecifier.length + unfavouredSpecifier.length + tokenSpecifier.length];
+ specifier[0] = first;
+ int index = 1;
+ for (int i = 0; i < favouredSpecifier.length; i++) {
+ specifier[index] = favouredSpecifier[i];
+ index++;
+ }
+ for (int i = 0; i < tokenSpecifier.length; i++) {
+ specifier[index] = tokenSpecifier[i];
+ index++;
+ }
+ for (int i = 0; i < unfavouredSpecifier.length; i++) {
+ specifier[index] = unfavouredSpecifier[i];
+ index++;
+ }
+ return specifier;
}
return null;
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=765535&r1=765534&r2=765535&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 Apr 16 09:29:27 2009
@@ -134,7 +134,7 @@
return favouredCodec;
}
- public Codec getUnvafouredCodec() {
+ public Codec getUnfavouredCodec() {
return unvafouredCodec;
}
@@ -147,4 +147,12 @@
// TODO Auto-generated method stub
return null;
}
+
+ public Codec getTokenCodec() {
+ return tokenCodec;
+ }
+
+ public int getL() {
+ return l;
+ }
}
Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/RunCodec.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/RunCodec.java?rev=765535&r1=765534&r2=765535&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/RunCodec.java (original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/RunCodec.java Thu Apr 16 09:29:27 2009
@@ -105,7 +105,7 @@
for (int i = 0; i < band.length; i++) {
boolean favouredValue = Arrays.binarySearch(favoured, band[i]) > -1;
Codec theCodec = favouredValue ? popCodec.getFavouredCodec()
- : popCodec.getUnvafouredCodec();
+ : popCodec.getUnfavouredCodec();
if (theCodec instanceof BHSDCodec
&& ((BHSDCodec) theCodec).isDelta()) {
BHSDCodec bhsd = (BHSDCodec) theCodec;
@@ -135,4 +135,16 @@
// TODO Auto-generated method stub
return null;
}
+
+ public int getK() {
+ return k;
+ }
+
+ public Codec getACodec() {
+ return aCodec;
+ }
+
+ public Codec getBCodec() {
+ return bCodec;
+ }
}
Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodecEncodingTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodecEncodingTest.java?rev=765535&r1=765534&r2=765535&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodecEncodingTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodecEncodingTest.java Thu Apr 16 09:29:27 2009
@@ -28,6 +28,8 @@
import org.apache.harmony.pack200.Codec;
import org.apache.harmony.pack200.CodecEncoding;
import org.apache.harmony.pack200.Pack200Exception;
+import org.apache.harmony.pack200.PopulationCodec;
+import org.apache.harmony.pack200.RunCodec;
/**
*
@@ -176,12 +178,12 @@
public void testGetSpecifier() throws IOException, Pack200Exception {
// Test canonical codecs
for (int i = 1; i <= 115; i++) {
- assertEquals(i, CodecEncoding.getSpecifier(CodecEncoding.getCodec(i, null, null))[0]);
+ assertEquals(i, CodecEncoding.getSpecifier(CodecEncoding.getCodec(i, null, null), null)[0]);
}
// Test a range of non-canonical codecs
Codec c1 = new BHSDCodec(2, 125, 0, 1);
- int[] specifiers = CodecEncoding.getSpecifier(c1);
+ int[] specifiers = CodecEncoding.getSpecifier(c1, null);
assertEquals(3, specifiers.length);
assertEquals(116, specifiers[0]);
byte[] bytes = new byte[] {(byte) specifiers[1], (byte) specifiers[2]};
@@ -189,7 +191,7 @@
assertEquals(c1, CodecEncoding.getCodec(116, in, null));
c1 = new BHSDCodec(3, 125, 2, 1);
- specifiers = CodecEncoding.getSpecifier(c1);
+ specifiers = CodecEncoding.getSpecifier(c1, null);
assertEquals(3, specifiers.length);
assertEquals(116, specifiers[0]);
bytes = new byte[] {(byte) specifiers[1], (byte) specifiers[2]};
@@ -197,7 +199,7 @@
assertEquals(c1, CodecEncoding.getCodec(116, in, null));
c1 = new BHSDCodec(4, 125);
- specifiers = CodecEncoding.getSpecifier(c1);
+ specifiers = CodecEncoding.getSpecifier(c1, null);
assertEquals(3, specifiers.length);
assertEquals(116, specifiers[0]);
bytes = new byte[] {(byte) specifiers[1], (byte) specifiers[2]};
@@ -205,7 +207,7 @@
assertEquals(c1, CodecEncoding.getCodec(116, in, null));
c1 = new BHSDCodec(5, 125, 2, 0);
- specifiers = CodecEncoding.getSpecifier(c1);
+ specifiers = CodecEncoding.getSpecifier(c1, null);
assertEquals(3, specifiers.length);
assertEquals(116, specifiers[0]);
bytes = new byte[] {(byte) specifiers[1], (byte) specifiers[2]};
@@ -213,7 +215,7 @@
assertEquals(c1, CodecEncoding.getCodec(116, in, null));
c1 = new BHSDCodec(3, 5, 2, 1);
- specifiers = CodecEncoding.getSpecifier(c1);
+ specifiers = CodecEncoding.getSpecifier(c1, null);
assertEquals(3, specifiers.length);
assertEquals(116, specifiers[0]);
bytes = new byte[] {(byte) specifiers[1], (byte) specifiers[2]};
@@ -221,4 +223,89 @@
assertEquals(c1, CodecEncoding.getCodec(116, in, null));
}
+ public void testGetSpeciferForRunCodec() throws Pack200Exception, IOException {
+ RunCodec runCodec = new RunCodec(25, Codec.DELTA5, Codec.BYTE1);
+ int[] specifiers = CodecEncoding.getSpecifier(runCodec, null);
+ assertTrue(specifiers[0] > 116);
+ assertTrue(specifiers[0] < 141);
+ byte[] bytes = new byte[specifiers.length - 1];
+ for (int i = 0; i < bytes.length; i++) {
+ bytes[i] = (byte) specifiers[i+1];
+ }
+ InputStream in = new ByteArrayInputStream(bytes);
+ RunCodec runCodec2 = (RunCodec) CodecEncoding.getCodec(specifiers[0], in, null);
+ assertEquals(runCodec.getK(), runCodec2.getK());
+ assertEquals(runCodec.getACodec(), runCodec2.getACodec());
+ assertEquals(runCodec.getBCodec(), runCodec2.getBCodec());
+
+ // One codec is the same as the default
+ runCodec = new RunCodec(4096, Codec.DELTA5, Codec.BYTE1);
+ specifiers = CodecEncoding.getSpecifier(runCodec, Codec.DELTA5);
+ assertTrue(specifiers[0] > 116);
+ assertTrue(specifiers[0] < 141);
+ bytes = new byte[specifiers.length - 1];
+ for (int i = 0; i < bytes.length; i++) {
+ bytes[i] = (byte) specifiers[i+1];
+ }
+ in = new ByteArrayInputStream(bytes);
+ runCodec2 = (RunCodec) CodecEncoding.getCodec(specifiers[0], in, Codec.DELTA5);
+ assertEquals(runCodec.getK(), runCodec2.getK());
+ assertEquals(runCodec.getACodec(), runCodec2.getACodec());
+ assertEquals(runCodec.getBCodec(), runCodec2.getBCodec());
+
+ // Nested run codecs
+ runCodec = new RunCodec(64, Codec.SIGNED5, new RunCodec(25, Codec.UDELTA5, Codec.DELTA5));
+ specifiers = CodecEncoding.getSpecifier(runCodec, null);
+ assertTrue(specifiers[0] > 116);
+ assertTrue(specifiers[0] < 141);
+ bytes = new byte[specifiers.length - 1];
+ for (int i = 0; i < bytes.length; i++) {
+ bytes[i] = (byte) specifiers[i+1];
+ }
+ in = new ByteArrayInputStream(bytes);
+ runCodec2 = (RunCodec) CodecEncoding.getCodec(specifiers[0], in, null);
+ assertEquals(runCodec.getK(), runCodec2.getK());
+ assertEquals(runCodec.getACodec(), runCodec2.getACodec());
+ RunCodec bCodec = (RunCodec) runCodec.getBCodec();
+ RunCodec bCodec2 = (RunCodec) runCodec2.getBCodec();
+ assertEquals(bCodec.getK(), bCodec2.getK());
+ assertEquals(bCodec.getACodec(), bCodec2.getACodec());
+ assertEquals(bCodec.getBCodec(), bCodec2.getBCodec());
+
+ // Nested with one the same as the default
+ runCodec = new RunCodec(64, Codec.SIGNED5, new RunCodec(25, Codec.UDELTA5, Codec.DELTA5));
+ specifiers = CodecEncoding.getSpecifier(runCodec, Codec.UDELTA5);
+ assertTrue(specifiers[0] > 116);
+ assertTrue(specifiers[0] < 141);
+ bytes = new byte[specifiers.length - 1];
+ for (int i = 0; i < bytes.length; i++) {
+ bytes[i] = (byte) specifiers[i+1];
+ }
+ in = new ByteArrayInputStream(bytes);
+ runCodec2 = (RunCodec) CodecEncoding.getCodec(specifiers[0], in, Codec.UDELTA5);
+ assertEquals(runCodec.getK(), runCodec2.getK());
+ assertEquals(runCodec.getACodec(), runCodec2.getACodec());
+ bCodec = (RunCodec) runCodec.getBCodec();
+ bCodec2 = (RunCodec) runCodec2.getBCodec();
+ assertEquals(bCodec.getK(), bCodec2.getK());
+ assertEquals(bCodec.getACodec(), bCodec2.getACodec());
+ assertEquals(bCodec.getBCodec(), bCodec2.getBCodec());
+ }
+
+ public void testGetSpeciferForPopulationCodec() throws IOException, Pack200Exception {
+ PopulationCodec pCodec = new PopulationCodec(Codec.BYTE1, Codec.CHAR3, Codec.UNSIGNED5);
+ int[] specifiers = CodecEncoding.getSpecifier(pCodec, null);
+ assertTrue(specifiers[0] > 140);
+ assertTrue(specifiers[0] < 189);
+ byte[] bytes = new byte[specifiers.length - 1];
+ for (int i = 0; i < bytes.length; i++) {
+ bytes[i] = (byte) specifiers[i+1];
+ }
+ InputStream in = new ByteArrayInputStream(bytes);
+ PopulationCodec pCodec2 = (PopulationCodec) CodecEncoding.getCodec(specifiers[0], in, null);
+ assertEquals(pCodec.getFavouredCodec(), pCodec2.getFavouredCodec());
+ assertEquals(pCodec.getTokenCodec(), pCodec2.getTokenCodec());
+ assertEquals(pCodec.getUnfavouredCodec(), pCodec2.getUnfavouredCodec());
+ }
+
}