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       * @param name Abstract file name.
39       * @param container My container.
40       * @param fs My file system.
41       *
42       * @deprecated Use {@link #Bzip2FileObject(AbstractFileName, FileObject, Bzip2FileSystem)} instead.
43       */
44      @Deprecated
45      protected Bzip2FileObject(final AbstractFileName name, final FileObject container,
46              final CompressedFileFileSystem fs) {
47          super(name, container, cast(fs));
48      }
49  
50      protected Bzip2FileObject(final AbstractFileName name, final FileObject container, final Bzip2FileSystem fs) {
51          super(name, container, fs);
52      }
53  
54      @Override
55      protected InputStream doGetInputStream(final int bufferSize) throws Exception {
56          // check file
57          return wrapInputStream(getName().getURI(), getContainer().getContent().getInputStream(bufferSize));
58      }
59  
60      public static InputStream wrapInputStream(final String name, final InputStream inputStream) throws IOException {
61          return new BZip2CompressorInputStream(inputStream);
62      }
63  
64      @Override
65      protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception {
66          return new BZip2CompressorOutputStream(getContainer().getContent().getOutputStream(false));
67      }
68  
69      private static Bzip2FileSystem cast(final CompressedFileFileSystem fs) {
70          if (fs instanceof Bzip2FileSystem) {
71              return (Bzip2FileSystem) fs;
72          }
73          throw new IllegalArgumentException("Bzip2FileObject requires a Bzip2FileSystem implementation");
74      }
75  }