net.ovres.util.UncompressedInputStream.java Source code

Java tutorial

Introduction

Here is the source code for net.ovres.util.UncompressedInputStream.java

Source

/*
 * TuxCourser
 * Copyright (C) 2004 Adam Elliott
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package net.ovres.util;

import java.io.BufferedInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorStreamFactory;

public class UncompressedInputStream extends FilterInputStream {
    protected InputStream in;

    public UncompressedInputStream(InputStream input) throws IOException {
        super(new BufferedInputStream(convertStream(input)));
    }

    /**
     * Returns a decompressed version of the given input stream.
     * This is done for any set 
     *
     * @param  raw  the raw input stream
     * @return  the decompressed version of <tt>raw</tt>
     */
    public static InputStream convertStream(InputStream raw) throws IOException {
        if (!raw.markSupported()) {
            raw = new BufferedInputStream(raw);
        }

        try {
            return new CompressorStreamFactory().createCompressorInputStream(raw);
        } catch (CompressorException e) {
            // Assume this is because of an unknown compression type.
            // That's ok, it might not be compressed.
            return raw;
        }
    }

}