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 2008/01/30 12:34:20 UTC
svn commit: r616726 - in
/harmony/enhanced/classlib/trunk/modules/pack200/src:
main/java/org/apache/harmony/pack200/
test/java/org/apache/harmony/pack200/tests/
Author: sjanuary
Date: Wed Jan 30 03:34:18 2008
New Revision: 616726
URL: http://svn.apache.org/viewvc?rev=616726&view=rev
Log:
Added a new class to pack200 to act as the main point of entry to the unpack function
Added:
harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Archive.java
harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/ArchiveTest.java
Modified:
harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java
harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/SegmentTest.java
Added: 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=616726&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Archive.java (added)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Archive.java Wed Jan 30 03:34:18 2008
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.pack200;
+
+import java.io.BufferedInputStream;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.jar.JarOutputStream;
+import java.util.zip.GZIPInputStream;
+
+/**
+ * The Archive class is the main entry point to unpack200. An archive is
+ * constructed with either two file names, a pack file and an output file name
+ * or two input streams corresponding to the input and the output streams. Then
+ * <code>unpack()</code> is called, to unpack the pack200 archive.
+ */
+public class Archive {
+
+ private static final int LOG_LEVEL_VERBOSE = 2;
+
+ private static final int LOG_LEVEL_STANDARD = 1;
+
+ private static final int LOG_LEVEL_QUIET = 0;
+
+ private InputStream inputStream;
+
+ private JarOutputStream outputStream;
+
+ private boolean removePackFile;
+
+ private int logLevel = LOG_LEVEL_STANDARD;
+
+ private FileOutputStream logFile;
+
+ private boolean overrideDeflateHint;
+
+ private boolean deflateHint;
+
+ public Archive(String inputFile, String outputFile)
+ throws FileNotFoundException, IOException {
+ inputStream = new FileInputStream(inputFile);
+ outputStream = new JarOutputStream(new FileOutputStream(outputFile));
+ }
+
+ public Archive(InputStream inputStream, JarOutputStream outputStream)
+ throws FileNotFoundException, IOException {
+ this.inputStream = inputStream;
+ this.outputStream = outputStream;
+ }
+
+ public void unpack() throws Pack200Exception, IOException {
+ outputStream.setComment("PACK200");
+ try {
+ if (!inputStream.markSupported()) {
+ inputStream = new BufferedInputStream(inputStream);
+ if (!inputStream.markSupported())
+ throw new IllegalStateException();
+ }
+ inputStream.mark(2);
+ if (((inputStream.read() & 0xFF) | (inputStream.read() & 0xFF) << 8) == GZIPInputStream.GZIP_MAGIC) {
+ inputStream.reset();
+ inputStream = new BufferedInputStream(new GZIPInputStream(
+ inputStream));
+ } else {
+ inputStream.reset();
+ }
+ inputStream.mark(4);
+ int[] jarMagic = { 0xCA, 0xFE, 0xBA, 0xBE }; // Magic word for a Jar file
+ byte word[] = new byte[4];
+ inputStream.read(word);
+ boolean compressedWithE0 = true;
+ for (int m = 0; m < jarMagic.length; m++) {
+ if (word[m] != jarMagic[m]) {
+ compressedWithE0 = false;
+ }
+ }
+ inputStream.reset();
+ if(compressedWithE0) { // The original Jar was not packed, so just copy it across
+ byte[] bytes = new byte[16384];
+ int bytesRead = 0;
+ while(bytesRead != -1) {
+ bytesRead = inputStream.read(bytes);
+ outputStream.write(bytes, 0, bytesRead);
+ }
+ } else {
+ while (inputStream.available() > 0) {
+ Segment segment = new Segment();
+ segment.setLogLevel(logLevel);
+ segment.setLogStream(logFile != null ? (OutputStream) logFile
+ : (OutputStream) System.out);
+ if (overrideDeflateHint) {
+ segment.overrideDeflateHint(deflateHint);
+ }
+ segment.unpack(inputStream, outputStream);
+ }
+ }
+ } catch (IOException e) {
+ try {
+ inputStream.close();
+ } finally {
+ outputStream.close();
+ }
+ }
+ if (removePackFile) {
+
+ }
+ }
+
+ public void setRemovePackFile(boolean removePackFile) {
+ this.removePackFile = removePackFile;
+ }
+
+ public void setVerbose(boolean verbose) {
+ if (verbose) {
+ logLevel = LOG_LEVEL_VERBOSE;
+ } else if (logLevel == LOG_LEVEL_VERBOSE) {
+ logLevel = LOG_LEVEL_STANDARD;
+ }
+ }
+
+ public void setQuiet(boolean quiet) {
+ if (quiet) {
+ logLevel = LOG_LEVEL_QUIET;
+ } else if (logLevel == LOG_LEVEL_QUIET) {
+ logLevel = LOG_LEVEL_QUIET;
+ }
+ ;
+ }
+
+ public void setLogFile(String logFileName) throws FileNotFoundException {
+ this.logFile = new FileOutputStream(logFileName);
+ }
+
+ public void setDeflateHint(boolean deflateHint) {
+ overrideDeflateHint = true;
+ this.deflateHint = deflateHint;
+ }
+
+}
\ No newline at end of file
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=616726&r1=616725&r2=616726&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 Jan 30 03:34:18 2008
@@ -20,6 +20,7 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.GZIPInputStream;
@@ -67,15 +68,14 @@
/**
+ * TODO: Do we need this method now we have Archive as the main entry point?
+ *
* Decode a segment from the given input stream. This does not attempt to
* re-assemble or export any class files, but it contains enough information
* to be able to re-assemble class files by external callers.
*
* @param in
- * the input stream to read from TODO At this point, this must be
- * a non-GZipped input stream, but this decoding could be done in
- * this method in the future (but perhaps more likely on an
- * archive as a whole)
+ * the input stream to read from
* @return a segment parsed from the input stream
* @throws IOException
* if a problem occurs during reading from the underlying stream
@@ -86,19 +86,6 @@
public static Segment parse(InputStream in) throws IOException,
Pack200Exception {
Segment segment = new Segment();
- // See if file is GZip compressed
- if (!in.markSupported()) {
- in = new BufferedInputStream(in);
- if (!in.markSupported())
- throw new IllegalStateException();
- }
- in.mark(2);
- if (((in.read() & 0xFF) | (in.read() & 0xFF) << 8) == GZIPInputStream.GZIP_MAGIC) {
- in.reset();
- in = new BufferedInputStream(new GZIPInputStream(in));
- } else {
- in.reset();
- }
segment.parseSegment(in);
return segment;
}
@@ -118,6 +105,10 @@
private FileBands fileBands;
+ private boolean overrideDeflateHint;
+
+ private boolean deflateHint;
+
private ClassFile buildClassFile(int classNum) throws Pack200Exception {
ClassFile classFile = new ClassFile();
classFile.major = header.getDefaultClassMajorVersion(); // TODO If
@@ -276,10 +267,9 @@
public void unpack(InputStream in, JarOutputStream out) throws IOException,
Pack200Exception {
if (!in.markSupported())
- in = new BufferedInputStream(in);
- // TODO Can handle multiple concatenated streams, so should deal with
- // that possibility
- parse(in).unpack(in, out);
+ in = new BufferedInputStream(in);
+ parseSegment(in);
+ writeJar(out);
}
/**
@@ -336,6 +326,9 @@
long modtime = archiveModtime + fileModtime[i];
boolean deflate = (fileOptions[i] & 1) == 1
|| options.shouldDeflate();
+ if (overrideDeflateHint) { // Overridden by a command line argument
+ deflate = deflateHint;
+ }
boolean isClass = (fileOptions[i] & 2) == 2 || name == null
|| name.equals("");
if (isClass) {
@@ -403,6 +396,28 @@
protected IcBands getIcBands() {
return icBands;
+ }
+
+
+ public void setLogLevel(int logLevel) {
+
+ }
+
+ public void setLogStream(OutputStream stream) {
+
+ }
+
+ public void log(int logLevel, String message) {
+
+ }
+
+ /**
+ * Override the archive's deflate hint with the given boolean
+ * @param deflateHint - the deflate hint to use
+ */
+ public void overrideDeflateHint(boolean deflateHint) {
+ this.overrideDeflateHint = true;
+ this.deflateHint = deflateHint;
}
}
Added: harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/ArchiveTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/ArchiveTest.java?rev=616726&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/ArchiveTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/ArchiveTest.java Wed Jan 30 03:34:18 2008
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.pack200.tests;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.jar.JarOutputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.pack200.Archive;
+
+/**
+ * Tests for org.apache.harmony.pack200.Archive, which is the main class for unpack200.
+ */
+public class ArchiveTest extends TestCase {
+
+ InputStream in;
+ JarOutputStream out;
+
+ public void testJustResourcesGZip() throws Exception {
+ in = Archive.class
+ .getResourceAsStream("/org/apache/harmony/pack200/tests/JustResources.pack.gz");
+ out = new JarOutputStream(new FileOutputStream(File.createTempFile("Just", "ResourcesGz.jar")));
+ Archive archive = new Archive(in, out);
+ archive.unpack();
+ }
+
+ // Test with an archive containing Harmony's SQL module, packed with -E1
+ public void testWithSqlE1() throws Exception {
+ in = Archive.class
+ .getResourceAsStream("/org/apache/harmony/pack200/tests/sql-e1.pack.gz");
+ out = new JarOutputStream(new FileOutputStream(File.createTempFile("sql-e1", ".jar")));
+ Archive archive = new Archive(in, out);
+ archive.unpack();
+ }
+
+ // Test with an archive containing Harmony's SQL module
+ public void testWithSql() throws Exception {
+ in = Archive.class
+ .getResourceAsStream("/org/apache/harmony/pack200/tests/sql.pack.gz");
+ out = new JarOutputStream(new FileOutputStream(File.createTempFile("sql", ".jar")));
+ Archive archive = new Archive(in, out);
+ archive.unpack();
+ }
+
+ // Test with an archive containing Harmony's Pack200 module, packed with -E1
+ public void testWithPack200E1() throws Exception {
+ in = Archive.class
+ .getResourceAsStream("/org/apache/harmony/pack200/tests/pack200-e1.pack.gz");
+ out = new JarOutputStream(new FileOutputStream(File.createTempFile("pack", "200-e1.jar")));
+ Archive archive = new Archive(in, out);
+ archive.unpack();
+ }
+
+ // Test with an archive containing Harmony's Pack200 module
+ public void testWithPack200() throws Exception {
+ in = Archive.class
+ .getResourceAsStream("/org/apache/harmony/pack200/tests/pack200.pack.gz");
+ out = new JarOutputStream(new FileOutputStream(File.createTempFile("pack", "200.jar")));
+ Archive archive = new Archive(in, out);
+ archive.unpack();
+ }
+
+ // Test with an archive containing Harmony's JNDI module
+ public void testWithJNDIE1() throws Exception {
+ in = Archive.class
+ .getResourceAsStream("/org/apache/harmony/pack200/tests/jndi-e1.pack.gz");
+ out = new JarOutputStream(new FileOutputStream(File.createTempFile("jndi", "-e1.jar")));
+ Archive archive = new Archive(in, out);
+ archive.unpack();
+ }
+
+ // Test with an archive containing Annotations
+ public void testWithAnnotations() throws Exception {
+ in = Archive.class
+ .getResourceAsStream("/org/apache/harmony/pack200/tests/annotations.pack.gz");
+ out = new JarOutputStream(new FileOutputStream(File.createTempFile("ann", "otations.jar")));
+ Archive archive = new Archive(in, out);
+ archive.unpack();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ try {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ } finally {
+ try {
+ if (out != null) {
+ out.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+
+
+
+
+}
Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/SegmentTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/SegmentTest.java?rev=616726&r1=616725&r2=616726&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/SegmentTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/SegmentTest.java Wed Jan 30 03:34:18 2008
@@ -16,9 +16,13 @@
*/
package org.apache.harmony.pack200.tests;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import junit.framework.TestCase;
@@ -26,103 +30,42 @@
import org.apache.harmony.pack200.Segment;
/**
- * Tests for org.apache.harmony.pack200.Segment, which is the main class for pack200.
+ * Tests for org.apache.harmony.pack200.Segment.
*/
public class SegmentTest extends TestCase {
InputStream in;
JarOutputStream out;
- public void testHelloWorld() throws Exception {
+ public void testHelloWorld() throws Exception {
in = Segment.class
.getResourceAsStream("/org/apache/harmony/pack200/tests/HelloWorld.pack");
Segment segment = Segment.parse(in);
assertNotNull(segment);
- out = new JarOutputStream(new FileOutputStream(File.createTempFile("hello", "world.jar")));
+ File file = File.createTempFile("hello", "world.jar");
+ out = new JarOutputStream(new FileOutputStream(file));
segment.writeJar(out);
+ out.close();
+ out = null;
+ JarFile jarFile = new JarFile(file);
+ JarEntry entry = jarFile.getJarEntry("org/apache/harmony/archive/tests/internal/pack200/HelloWorld.class");
+ assertNotNull(entry);
+ Process process = Runtime.getRuntime().exec("java -cp " + file.getName() + " org.apache.harmony.archive.tests.internal.pack200.HelloWorld", new String[] {}, file.getParentFile());
+ BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+ String line = reader.readLine();
+ assertEquals(line, "Hello world");
}
- public void testJustResources() throws Exception {
+ public void testJustResources() throws Exception {
in = Segment.class
.getResourceAsStream("/org/apache/harmony/pack200/tests/JustResources.pack");
Segment segment = Segment.parse(in);
assertNotNull(segment);
out = new JarOutputStream(new FileOutputStream(File.createTempFile("just", "resources.jar")));
segment.writeJar(out);
- }
-
- public void testJustResourcesGZip() throws Exception {
- 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 {
- 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("sql", "-e1.jar")));
- segment.writeJar(out);
- }
-
- // Test with an archive containing Harmony's SQL module
- public void testWithSql() throws Exception {
- 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("sql", ".jar")));
- segment.writeJar(out);
- }
-
- // Test with an archive containing Harmony's Pack200 module, packed with -E1
- public void testWithPack200E1() throws Exception {
- 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
- public void testWithPack200() throws Exception {
- 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
- public void testWithJNDIE1() throws Exception {
- 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("jndi", "-e1.jar")));
- segment.writeJar(out);
- }
-
- // Test with an archive containing Annotations
- public void testWithAnnotations() throws Exception {
-
- 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);
-
}
- public void testInterfaceOnly() throws Exception {
+ public void testInterfaceOnly() throws Exception {
in = Segment.class
.getResourceAsStream("/org/apache/harmony/pack200/tests/InterfaceOnly.pack");
Segment segment = Segment.parse(in);