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.gzip;
018
019import java.io.InputStream;
020import java.io.OutputStream;
021import java.util.zip.GZIPInputStream;
022import java.util.zip.GZIPOutputStream;
023
024import org.apache.commons.vfs2.FileObject;
025import org.apache.commons.vfs2.provider.AbstractFileName;
026import org.apache.commons.vfs2.provider.compressed.CompressedFileFileObject;
027import org.apache.commons.vfs2.provider.compressed.CompressedFileFileSystem;
028
029/**
030 * A Gzip file.
031 */
032public class GzipFileObject extends CompressedFileFileObject<GzipFileSystem> {
033
034    private static GzipFileSystem cast(final CompressedFileFileSystem fs) {
035        if (fs instanceof GzipFileSystem) {
036            return (GzipFileSystem) fs;
037        }
038        throw new IllegalArgumentException("GzipFileObject expects an instance of GzipFileSystem");
039    }
040
041    /**
042     * Deprecated since 2.1.
043     *
044     * @param name Abstract file name.
045     * @param container My container.
046     * @param fs My file system.
047     * @deprecated Use {@link #GzipFileObject(AbstractFileName, FileObject, GzipFileSystem)} instead.
048     */
049    @Deprecated
050    protected GzipFileObject(final AbstractFileName name, final FileObject container,
051            final CompressedFileFileSystem fs) {
052        super(name, container, cast(fs));
053    }
054
055    /**
056     * Constructs a new instance.
057     *
058     * @param fileName the file name.
059     * @param container the container.
060     * @param fileSystem the file system.
061     */
062    protected GzipFileObject(final AbstractFileName fileName, final FileObject container, final GzipFileSystem fileSystem) {
063        super(fileName, container, fileSystem);
064    }
065
066    @Override
067    protected InputStream doGetInputStream(final int bufferSize) throws Exception {
068        return new GZIPInputStream(getContainer().getContent().getInputStream(), bufferSize);
069    }
070
071    @Override
072    protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception {
073        return new GZIPOutputStream(getContainer().getContent().getOutputStream(false));
074    }
075}