01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.harmony.pack200;
18:
19: import java.io.BufferedInputStream;
20: import java.io.File;
21: import java.io.FileInputStream;
22: import java.io.IOException;
23: import java.io.InputStream;
24: import java.util.jar.JarOutputStream;
25: import java.util.jar.Pack200.Unpacker;
26:
27: import org.apache.harmony.pack200.Pack200Exception;
28: import org.apache.harmony.pack200.Segment;
29:
30: /**
31: * This class provides the binding between the standard Pack200 interface and
32: * the internal interface for (un)packing. As this uses generics for the
33: * SortedMap, this class must be compiled and run on a Java 1.5 system. However,
34: * Java 1.5 is not necessary to use the internal libraries for unpacking.
35: */
36: public class Pack200UnpackerAdapter extends Pack200Adapter implements
37: Unpacker {
38: /*
39: * (non-Javadoc)
40: *
41: * @see java.util.jar.Pack200.Unpacker#unpack(java.io.InputStream,
42: * java.util.jar.JarOutputStream)
43: */
44: public void unpack(InputStream in, JarOutputStream out)
45: throws IOException {
46: if (in == null || out == null)
47: throw new IllegalArgumentException(
48: "Must specify both input and output streams");
49: completed(0);
50: try {
51: while (in.available() > 0) {
52: Segment s = Segment.parse(in);
53: s.writeJar(out);
54: out.flush();
55: }
56: } catch (Pack200Exception e) {
57: throw new IOException("Failed to unpack Jar:"
58: + String.valueOf(e));
59: }
60: completed(1);
61: in.close();
62: }
63:
64: /*
65: * (non-Javadoc)
66: *
67: * @see java.util.jar.Pack200.Unpacker#unpack(java.io.File,
68: * java.util.jar.JarOutputStream)
69: */
70: public void unpack(File file, JarOutputStream out)
71: throws IOException {
72: if (file == null || out == null)
73: throw new IllegalArgumentException(
74: "Must specify both input and output streams");
75: int size = (int) file.length();
76: int bufferSize = (size > 0 && size < DEFAULT_BUFFER_SIZE ? size
77: : DEFAULT_BUFFER_SIZE);
78: InputStream in = new BufferedInputStream(new FileInputStream(
79: file), bufferSize);
80: unpack(in, out);
81: }
82: }
|