KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > BZip2Resource


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
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(org.apache.tools.ant.types.ResourceCollection other) {
47         super(other);
48     }
49
50     /**
51      * Decompress on the fly using {@link CBZip2InputStream}.
52      * @param in the stream to wrap.
53      * @return the wrapped stream.
54      * @throws IOException if there is a problem.
55      */

56     protected InputStream JavaDoc wrapStream(InputStream JavaDoc in) throws IOException JavaDoc {
57         for (int i = 0; i < MAGIC.length; i++) {
58             if (in.read() != MAGIC[i]) {
59                 throw new IOException JavaDoc("Invalid bz2 stream.");
60             }
61         }
62         return new CBZip2InputStream(in);
63     }
64
65     /**
66      * Compress on the fly using {@link CBZip2OutputStream}.
67      * @param out the stream to wrap.
68      * @return the wrapped stream.
69      * @throws IOException if there is a problem.
70      */

71     protected OutputStream JavaDoc wrapStream(OutputStream JavaDoc out) throws IOException JavaDoc {
72         for (int i = 0; i < MAGIC.length; i++) {
73             out.write(MAGIC[i]);
74         }
75         return new CBZip2OutputStream(out);
76     }
77
78     /**
79      * Get the name of the compression method.
80      * @return the string "Bzip2".
81      */

82     protected String JavaDoc getCompressionName() {
83         return "Bzip2";
84     }
85 }
86
Popular Tags