Index: src/main/java/org/apache/harmony/pack200/FileBands.java =================================================================== --- src/main/java/org/apache/harmony/pack200/FileBands.java (revision 608803) +++ src/main/java/org/apache/harmony/pack200/FileBands.java (working copy) @@ -40,6 +40,8 @@ private long[] fileSize; private String[] cpUTF8; + + private InputStream in; /** * @param header @@ -73,10 +75,11 @@ } else { fileOptions = new long[numberOfFiles]; } + this.in = in; // store for use by processFileBits(), which is called later } - - public void processFileBits(InputStream in) throws IOException, + // TODO: stream the file bits directly somehow + public void processFileBits() throws IOException, Pack200Exception { // now read in the bytes int numberOfFiles = header.getNumberOfFiles(); @@ -86,8 +89,9 @@ // TODO This breaks if file_size > 2^32. Probably an array is // not the right choice, and we should just serialize it here? fileBits[i] = new byte[size]; - for (int j = 0; j < size; j++) { - fileBits[i][j] = (byte) Codec.BYTE1.decode(in); + int read = in.read(fileBits[i]); + if(read < size) { + throw new Pack200Exception("Expected to read " + size +" bytes but read " + read); } } } Index: src/main/java/org/apache/harmony/pack200/Segment.java =================================================================== --- src/main/java/org/apache/harmony/pack200/Segment.java (revision 596606) +++ src/main/java/org/apache/harmony/pack200/Segment.java (working copy) @@ -97,7 +97,7 @@ } else { in.reset(); } - segment.parseSegment(in); + segment.parseSegment(in); return segment; } @@ -268,9 +268,9 @@ * @throws Pack200Exception * if an error occurs whilst unpacking data */ - public void writeJar(JarOutputStream out, InputStream in) + public void writeJar(JarOutputStream out) throws IOException, Pack200Exception { - fileBands.processFileBits(in); + fileBands.processFileBits(); DataOutputStream dos = new DataOutputStream(out); String[] fileName = fileBands.getFileName(); long[] fileModtime = fileBands.getFileModtime(); Index: src/main/java/org/apache/harmony/pack200/SegmentHeader.java =================================================================== --- src/main/java/org/apache/harmony/pack200/SegmentHeader.java (revision 575301) +++ src/main/java/org/apache/harmony/pack200/SegmentHeader.java (working copy) @@ -329,7 +329,7 @@ private long[] decodeScalar(String name, InputStream in, BHSDCodec codec, int n) throws IOException, Pack200Exception { // TODO Remove debugging code -// debug("Parsed #" + name + " (" + n + ")"); + debug("Parsed #" + name + " (" + n + ")"); return codec.decode(n, in); } Index: src/main/java5/org/apache/harmony/pack200/Pack200UnpackerAdapter.java =================================================================== --- src/main/java5/org/apache/harmony/pack200/Pack200UnpackerAdapter.java (revision 565676) +++ src/main/java5/org/apache/harmony/pack200/Pack200UnpackerAdapter.java (working copy) @@ -48,7 +48,7 @@ try { while (in.available() > 0) { Segment s = Segment.parse(in); - s.writeJar(out, in); + s.writeJar(out); out.flush(); } } catch (Pack200Exception e) { Index: src/test/java/Unpack.java =================================================================== --- src/test/java/Unpack.java (revision 572385) +++ src/test/java/Unpack.java (working copy) @@ -33,6 +33,6 @@ JarOutputStream out = new JarOutputStream( args.length > 1 ? (OutputStream) new FileOutputStream(args[1]) : (OutputStream) new BufferedOutputStream(System.out)); - Segment.parse(in).writeJar(out, in); + Segment.parse(in).writeJar(out); } } Index: src/test/java/org/apache/harmony/pack200/tests/SegmentTest.java =================================================================== --- src/test/java/org/apache/harmony/pack200/tests/SegmentTest.java (revision 608798) +++ src/test/java/org/apache/harmony/pack200/tests/SegmentTest.java (working copy) @@ -29,6 +29,9 @@ * Tests for org.apache.harmony.pack200.Segment, which is the main class for pack200. */ public class SegmentTest extends TestCase { + + InputStream in; + JarOutputStream out; boolean handlingInnerClasses = false; @@ -33,26 +36,31 @@ boolean handlingInnerClasses = false; public void testHelloWorld() throws Exception { - InputStream in = Segment.class - .getResourceAsStream("/org/apache/harmony/pack200/tests/HelloWorld.pack"); + in = Segment.class + .getResourceAsStream("/org/apache/harmony/pack200/tests/HelloWorld.pack"); Segment segment = Segment.parse(in); assertNotNull(segment); - segment.writeJar(new JarOutputStream(new FileOutputStream(File.createTempFile("Hello", "World.jar"))), in); - } + out = new JarOutputStream(new FileOutputStream(File.createTempFile("hello", "world.jar"))); + segment.writeJar(out); + } public void testJustResources() throws Exception { - InputStream in = Segment.class - .getResourceAsStream("/org/apache/harmony/pack200/tests/JustResources.pack"); + in = Segment.class + .getResourceAsStream("/org/apache/harmony/pack200/tests/JustResources.pack"); Segment segment = Segment.parse(in); - assertNotNull(segment); - segment.writeJar(new JarOutputStream(new FileOutputStream(File.createTempFile("just", "Resources.jar"))), in); + assertNotNull(segment); + out = new JarOutputStream(new FileOutputStream(File.createTempFile("just", "resources.jar"))); + segment.writeJar(out); } public void testJustResourcesGZip() throws Exception { - assertNotNull(Segment - .parse(Segment.class - .getResourceAsStream("/org/apache/harmony/pack200/tests/JustResources.pack.gz"))); - } + in = Segment.class + .getResourceAsStream("/org/apache/harmony/pack200/tests/JustResources.pack.gz"); + Segment segment = Segment.parse(in); + assertNotNull(segment); + out = new JarOutputStream(new FileOutputStream(File.createTempFile("Just", "ResourcesGz.jar"))); + segment.writeJar(out); + } // Test with an archive containing Harmony's SQL module, packed with -E1 public void testWithSqlE1() throws Exception { @@ -59,10 +67,12 @@ // This test will not pass until we handle inner classes // correctly. if(!handlingInnerClasses) return; - assertNotNull(Segment - .parse(Segment.class - .getResourceAsStream("/org/apache/harmony/pack200/tests/sql-e1.pack.gz"))); - + in = Segment.class + .getResourceAsStream("/org/apache/harmony/pack200/tests/sql-e1.pack.gz"); + Segment segment = Segment.parse(in); + assertNotNull(segment); + out = new JarOutputStream(new FileOutputStream(File.createTempFile("s", "ql-e1.jar"))); + segment.writeJar(out); } // Test with an archive containing Harmony's SQL module @@ -70,10 +80,12 @@ // This test will not pass until we handle inner classes // correctly. if(!handlingInnerClasses) return; - assertNotNull(Segment - .parse(Segment.class - .getResourceAsStream("/org/apache/harmony/pack200/tests/sql.pack.gz"))); - + in = Segment.class + .getResourceAsStream("/org/apache/harmony/pack200/tests/sql.pack.gz"); + Segment segment = Segment.parse(in); + assertNotNull(segment); + out = new JarOutputStream(new FileOutputStream(File.createTempFile("s", "ql.jar"))); + segment.writeJar(out); } // Test with an archive containing Harmony's Pack200 module, packed with -E1 @@ -78,10 +90,12 @@ // Test with an archive containing Harmony's Pack200 module, packed with -E1 public void testWithPack200E1() throws Exception { - assertNotNull(Segment - .parse(Segment.class - .getResourceAsStream("/org/apache/harmony/pack200/tests/pack200-e1.pack.gz"))); - + in = Segment.class + .getResourceAsStream("/org/apache/harmony/pack200/tests/pack200-e1.pack.gz"); + Segment segment = Segment.parse(in); + assertNotNull(segment); + out = new JarOutputStream(new FileOutputStream(File.createTempFile("pack", "200-e1.jar"))); + segment.writeJar(out); } // Test with an archive containing Harmony's Pack200 module @@ -86,10 +100,12 @@ // Test with an archive containing Harmony's Pack200 module public void testWithPack200() throws Exception { - assertNotNull(Segment - .parse(Segment.class - .getResourceAsStream("/org/apache/harmony/pack200/tests/pack200.pack.gz"))); - + in = Segment.class + .getResourceAsStream("/org/apache/harmony/pack200/tests/pack200.pack.gz"); + Segment segment = Segment.parse(in); + assertNotNull(segment); + out = new JarOutputStream(new FileOutputStream(File.createTempFile("pack", "200.jar"))); + segment.writeJar(out); } // Test with an archive containing Harmony's JNDI module @@ -95,10 +111,12 @@ // Test with an archive containing Harmony's JNDI module public void testWithJNDIE1() throws Exception { if(!handlingInnerClasses) return; - assertNotNull(Segment - .parse(Segment.class - .getResourceAsStream("/org/apache/harmony/pack200/tests/jndi-e1.pack.gz"))); - + in = Segment.class + .getResourceAsStream("/org/apache/harmony/pack200/tests/jndi-e1.pack.gz"); + Segment segment = Segment.parse(in); + assertNotNull(segment); + out = new JarOutputStream(new FileOutputStream(File.createTempFile("jn", "di-e1.jar"))); + segment.writeJar(out); } // Test with an archive containing Annotations @@ -103,9 +121,13 @@ // Test with an archive containing Annotations public void testWithAnnotations() throws Exception { - assertNotNull(Segment - .parse(Segment.class - .getResourceAsStream("/org/apache/harmony/pack200/tests/annotations.pack.gz"))); + + in = Segment.class + .getResourceAsStream("/org/apache/harmony/pack200/tests/annotations.pack.gz"); + Segment segment = Segment.parse(in); + assertNotNull(segment); + out = new JarOutputStream(new FileOutputStream(File.createTempFile("ann", "otations.jar"))); + segment.writeJar(out); } @@ -110,9 +132,25 @@ } public void testInterfaceOnly() throws Exception { - assertNotNull(Segment - .parse(Segment.class - .getResourceAsStream("/org/apache/harmony/pack200/tests/InterfaceOnly.pack"))); + in = Segment.class + .getResourceAsStream("/org/apache/harmony/pack200/tests/InterfaceOnly.pack"); + Segment segment = Segment.parse(in); + assertNotNull(segment); + out = new JarOutputStream(new FileOutputStream(File.createTempFile("Interface", "Only.jar"))); + segment.writeJar(out); + } + + protected void tearDown() throws Exception { + super.tearDown(); + try { + if (in != null) { + in.close(); + } + } finally { + if (out != null) { + out.close(); + } + } } } \ No newline at end of file