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: */
18: package org.apache.tools.ant.types.resources;
19:
20: import java.io.InputStream;
21: import java.io.IOException;
22: import java.io.OutputStream;
23:
24: import org.apache.tools.bzip2.CBZip2InputStream;
25: import org.apache.tools.bzip2.CBZip2OutputStream;
26:
27: /**
28: * A Bzip2 compressed resource.
29: *
30: * <p>Wraps around another resource, delegates all quries to that
31: * other resource but uncompresses/compresses streams on the fly.</p>
32: *
33: * @since Ant 1.7
34: */
35: public class BZip2Resource extends CompressedResource {
36: private static final char[] MAGIC = new char[] { 'B', 'Z' };
37:
38: /** A no-arg constructor */
39: public BZip2Resource() {
40: }
41:
42: /**
43: * Constructor with another resource to wrap.
44: * @param other the resource to wrap.
45: */
46: public BZip2Resource(
47: org.apache.tools.ant.types.ResourceCollection other) {
48: super (other);
49: }
50:
51: /**
52: * Decompress on the fly using {@link CBZip2InputStream}.
53: * @param in the stream to wrap.
54: * @return the wrapped stream.
55: * @throws IOException if there is a problem.
56: */
57: protected InputStream wrapStream(InputStream in) throws IOException {
58: for (int i = 0; i < MAGIC.length; i++) {
59: if (in.read() != MAGIC[i]) {
60: throw new IOException("Invalid bz2 stream.");
61: }
62: }
63: return new CBZip2InputStream(in);
64: }
65:
66: /**
67: * Compress on the fly using {@link CBZip2OutputStream}.
68: * @param out the stream to wrap.
69: * @return the wrapped stream.
70: * @throws IOException if there is a problem.
71: */
72: protected OutputStream wrapStream(OutputStream out)
73: throws IOException {
74: for (int i = 0; i < MAGIC.length; i++) {
75: out.write(MAGIC[i]);
76: }
77: return new CBZip2OutputStream(out);
78: }
79:
80: /**
81: * Get the name of the compression method.
82: * @return the string "Bzip2".
83: */
84: protected String getCompressionName() {
85: return "Bzip2";
86: }
87: }
|