KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > bzip2 > Bzip2FileObject


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.bzip2;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 import org.apache.commons.vfs.FileName;
23 import org.apache.commons.vfs.FileObject;
24 import org.apache.commons.vfs.FileSystemException;
25 import org.apache.commons.vfs.provider.compressed.CompressedFileFileObject;
26 import org.apache.commons.vfs.provider.compressed.CompressedFileFileSystem;
27 import org.apache.tools.bzip2.CBZip2InputStream;
28 import org.apache.tools.bzip2.CBZip2OutputStream;
29
30 /**
31  * the bzip2 file
32  *
33  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
34  */

35 public class Bzip2FileObject extends CompressedFileFileObject
36 {
37     protected Bzip2FileObject(FileName name, FileObject container, CompressedFileFileSystem fs)
38     {
39         super(name, container, fs);
40     }
41
42     protected InputStream JavaDoc doGetInputStream() throws Exception JavaDoc
43     {
44         // check file
45
InputStream JavaDoc is = getContainer().getContent().getInputStream();
46         return wrapInputStream(getName().getURI(), is);
47     }
48
49     public static InputStream JavaDoc wrapInputStream(final String JavaDoc name, final InputStream JavaDoc is) throws IOException JavaDoc
50     {
51         final int b1 = is.read();
52         final int b2 = is.read();
53         if (b1 != 'B' || b2 != 'Z')
54         {
55             throw new FileSystemException("vfs.provider.compressedFile/not-a-compressedFile-file.error", name);
56         }
57         return new CBZip2InputStream(is);
58     }
59
60     protected OutputStream JavaDoc doGetOutputStream(boolean bAppend) throws Exception JavaDoc
61     {
62         OutputStream JavaDoc os = getContainer().getContent().getOutputStream(false);
63         os.write('B');
64         os.write('Z');
65
66         return new CBZip2OutputStream(os);
67     }
68 }
69
Popular Tags