001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.provider.bzip2; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.OutputStream; 022 023import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; 024import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; 025import org.apache.commons.vfs2.FileObject; 026import org.apache.commons.vfs2.provider.AbstractFileName; 027import org.apache.commons.vfs2.provider.compressed.CompressedFileFileObject; 028import org.apache.commons.vfs2.provider.compressed.CompressedFileFileSystem; 029 030/** 031 * the bzip2 file. 032 */ 033public class Bzip2FileObject extends CompressedFileFileObject<Bzip2FileSystem> { 034 035 private static Bzip2FileSystem cast(final CompressedFileFileSystem fs) { 036 if (fs instanceof Bzip2FileSystem) { 037 return (Bzip2FileSystem) fs; 038 } 039 throw new IllegalArgumentException("Bzip2FileObject requires a Bzip2FileSystem implementation"); 040 } 041 042 /** 043 * Wraps an input stream in a compressor input stream. 044 * 045 * @param name Unused. 046 * @param inputStream The input stream to wrap. 047 * @return a new compressor input stream. 048 * @throws IOException if the stream content is malformed or an I/O error occurs. 049 */ 050 public static InputStream wrapInputStream(final String name, final InputStream inputStream) throws IOException { 051 return new BZip2CompressorInputStream(inputStream); 052 } 053 054 /** 055 * Constructs a new instance. 056 * 057 * @param fileName the file name. 058 * @param container the container. 059 * @param fileSystem the file system. 060 */ 061 protected Bzip2FileObject(final AbstractFileName fileName, final FileObject container, final Bzip2FileSystem fileSystem) { 062 super(fileName, container, fileSystem); 063 } 064 065 /** 066 * Deprecated since 2.1. 067 * 068 * @param name Abstract file name. 069 * @param container My container. 070 * @param fs My file system. 071 * @deprecated Use {@link #Bzip2FileObject(AbstractFileName, FileObject, Bzip2FileSystem)} instead. 072 */ 073 @Deprecated 074 protected Bzip2FileObject(final AbstractFileName name, final FileObject container, 075 final CompressedFileFileSystem fs) { 076 super(name, container, cast(fs)); 077 } 078 079 @Override 080 protected InputStream doGetInputStream(final int bufferSize) throws Exception { 081 // check file 082 return wrapInputStream(getName().getURI(), getContainer().getContent().getInputStream(bufferSize)); 083 } 084 085 @Override 086 protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception { 087 return new BZip2CompressorOutputStream(getContainer().getContent().getOutputStream(false)); 088 } 089}