View Javadoc
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  package org.apache.commons.vfs2.provider.bzip2;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  
23  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
24  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
25  import org.apache.commons.vfs2.FileObject;
26  import org.apache.commons.vfs2.provider.AbstractFileName;
27  import org.apache.commons.vfs2.provider.compressed.CompressedFileFileObject;
28  import org.apache.commons.vfs2.provider.compressed.CompressedFileFileSystem;
29  
30  /**
31   * the bzip2 file.
32   */
33  public class Bzip2FileObject extends CompressedFileFileObject<Bzip2FileSystem>
34  {
35      /**
36       * Deprecated since 2.1. 
37       * 
38       * @deprecated Use {@link #Bzip2FileObject(AbstractFileName, FileObject, Bzip2FileSystem)} instead.
39       */
40      @Deprecated
41      protected Bzip2FileObject(final AbstractFileName name, final FileObject container, final CompressedFileFileSystem fs)
42      {
43          super(name, container, cast(fs));
44      }
45  
46      protected Bzip2FileObject(final AbstractFileName name, final FileObject container, final Bzip2FileSystem fs)
47      {
48          super(name, container, fs);
49      }
50  
51      @Override
52      protected InputStream doGetInputStream() throws Exception
53      {
54          // check file
55          final InputStream is = getContainer().getContent().getInputStream();
56          return wrapInputStream(getName().getURI(), is);
57      }
58  
59      public static InputStream wrapInputStream(final String name, final InputStream is) throws IOException
60      {
61          return new BZip2CompressorInputStream(is);
62      }
63  
64      @Override
65      protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception
66      {
67          final OutputStream os = getContainer().getContent().getOutputStream(false);
68          return new BZip2CompressorOutputStream(os);
69      }
70  
71      private static Bzip2FileSystem cast(CompressedFileFileSystem fs) {
72          if (fs instanceof Bzip2FileSystem) {
73              return (Bzip2FileSystem) fs;
74          }
75          throw new IllegalArgumentException("Bzip2FileObject requires a Bzip2FileSystem implementation");
76      }
77  }