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 * Deprecated since 2.1. 036 * 037 * @deprecated Use {@link #Bzip2FileObject(AbstractFileName, FileObject, Bzip2FileSystem)} instead. 038 */ 039 @Deprecated 040 protected Bzip2FileObject(final AbstractFileName name, final FileObject container, 041 final CompressedFileFileSystem fs) { 042 super(name, container, cast(fs)); 043 } 044 045 protected Bzip2FileObject(final AbstractFileName name, final FileObject container, final Bzip2FileSystem fs) { 046 super(name, container, fs); 047 } 048 049 @Override 050 protected InputStream doGetInputStream() throws Exception { 051 // check file 052 final InputStream is = getContainer().getContent().getInputStream(); 053 return wrapInputStream(getName().getURI(), is); 054 } 055 056 public static InputStream wrapInputStream(final String name, final InputStream is) throws IOException { 057 return new BZip2CompressorInputStream(is); 058 } 059 060 @Override 061 protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception { 062 final OutputStream os = getContainer().getContent().getOutputStream(false); 063 return new BZip2CompressorOutputStream(os); 064 } 065 066 private static Bzip2FileSystem cast(final CompressedFileFileSystem fs) { 067 if (fs instanceof Bzip2FileSystem) { 068 return (Bzip2FileSystem) fs; 069 } 070 throw new IllegalArgumentException("Bzip2FileObject requires a Bzip2FileSystem implementation"); 071 } 072}