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      private static Bzip2FileSystem cast(final CompressedFileFileSystem fs) {
36          if (fs instanceof Bzip2FileSystem) {
37              return (Bzip2FileSystem) fs;
38          }
39          throw new IllegalArgumentException("Bzip2FileObject requires a Bzip2FileSystem implementation");
40      }
41  
42      /**
43       * Wraps an input stream in a compressor input stream.
44       *
45       * @param name Unused.
46       * @param inputStream The input stream to wrap.
47       * @return a new compressor input stream.
48       * @throws IOException if the stream content is malformed or an I/O error occurs.
49       */
50      public static InputStream wrapInputStream(final String name, final InputStream inputStream) throws IOException {
51          return new BZip2CompressorInputStream(inputStream);
52      }
53  
54      /**
55       * Constructs a new instance.
56       *
57       * @param fileName the file name.
58       * @param container the container.
59       * @param fileSystem the file system.
60       */
61      protected Bzip2FileObject(final AbstractFileName fileName, final FileObject container, final Bzip2FileSystem fileSystem) {
62          super(fileName, container, fileSystem);
63      }
64  
65      /**
66       * Deprecated since 2.1.
67       *
68       * @param name Abstract file name.
69       * @param container My container.
70       * @param fs My file system.
71       * @deprecated Use {@link #Bzip2FileObject(AbstractFileName, FileObject, Bzip2FileSystem)} instead.
72       */
73      @Deprecated
74      protected Bzip2FileObject(final AbstractFileName name, final FileObject container,
75              final CompressedFileFileSystem fs) {
76          super(name, container, cast(fs));
77      }
78  
79      @Override
80      protected InputStream doGetInputStream(final int bufferSize) throws Exception {
81          // check file
82          return wrapInputStream(getName().getURI(), getContainer().getContent().getInputStream(bufferSize));
83      }
84  
85      @Override
86      protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception {
87          return new BZip2CompressorOutputStream(getContainer().getContent().getOutputStream(false));
88      }
89  }