/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.commons.compress.archivers;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
import org.apache.commons.compress.archivers.ar.ArArchiveOutputStream;
import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
import org.apache.commons.compress.archivers.cpio.CpioArchiveOutputStream;
import org.apache.commons.compress.archivers.jar.JarArchiveInputStream;
import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;

/**
 * Factory to create Archive[In|Out]putStreams from names or the first bytes of
 * the InputStream. In order add other implementations you should extend
 * ArchiveStreamFactory and override the appropriate methods (and call their
 * implementation from super of course)
 * 
 * TODO add example here
 * 
 */
public class ArchiveStreamFactory {

    /**
     * Create an archive input stream from an archiver name and an input stream.
     * 
     * @param archiverName the archive name, i.e. "ar", "zip", "tar", "jar" or "cpio"
     * @param in the input stream
     * @return the archive input stream
     * @throws ArchiveException
     * @throws IllegalArgumentException if the archiver name is null or not known
     */
    public ArchiveInputStream createArchiveInputStream(
            final String archiverName, final InputStream in)
            throws ArchiveException {
        if (archiverName == null || in == null) {
            throw new IllegalArgumentException("Archivername must not be null.");
        }

        if ("ar".equalsIgnoreCase(archiverName)) {
            return new ArArchiveInputStream(in);
        } else if ("zip".equalsIgnoreCase(archiverName)) {
            return new ZipArchiveInputStream(in);
        } else if ("tar".equalsIgnoreCase(archiverName)) {
            return new TarArchiveInputStream(in);
        } else if ("jar".equalsIgnoreCase(archiverName)) {
            return new JarArchiveInputStream(in);
        } else if ("cpio".equalsIgnoreCase(archiverName)) {
            return new CpioArchiveInputStream(in);
        }
        throw new ArchiveException("Archiver: " + archiverName + " not found.");
    }

    /**
     * Create an archive output stream from an archiver name and an input stream.
     * 
     * @param archiverName the archive name, i.e. "ar", "zip", "tar", "jar" or "cpio"
     * @param out the output stream
     * @return the archive output stream
     * @throws ArchiveException
     * @throws IllegalArgumentException if the archiver name is null or not known
     */
    public ArchiveOutputStream createArchiveOutputStream(
            final String archiverName, final OutputStream out)
            throws ArchiveException {
        if (archiverName == null || out == null) {
            throw new IllegalArgumentException(
                    "Archivername and stream must not be null.");
        }

        if ("ar".equalsIgnoreCase(archiverName)) {
            return new ArArchiveOutputStream(out);
        } else if ("zip".equalsIgnoreCase(archiverName)) {
            return new ZipArchiveOutputStream(out);
        } else if ("tar".equalsIgnoreCase(archiverName)) {
            return new TarArchiveOutputStream(out);
        } else if ("jar".equalsIgnoreCase(archiverName)) {
            return new JarArchiveOutputStream(out);
        } else if ("cpio".equalsIgnoreCase(archiverName)) {
            return new CpioArchiveOutputStream(out);
        }
        throw new ArchiveException("Archiver: " + archiverName + " not found.");
    }

    /**
     * Create an archive input stream from an input stream, autodetecting
     * the archive type from the first few bytes of the stream.
     * 
     * @param in the input stream
     * @return the archive input stream
     * @throws ArchiveException
     * @throws IllegalArgumentException if the archiver name is null or not known
     */
    public ArchiveInputStream createArchiveInputStream(final InputStream in)
            throws ArchiveException {
        if (in == null) {
            throw new IllegalArgumentException("Stream must not be null.");
        }

        if (!in.markSupported()) {
            throw new IllegalArgumentException("Mark is not supported.");
        }

        final byte[] signature = new byte[12];
        in.mark(signature.length);
        try {
            int signatureLength = in.read(signature);
            in.reset();
            if (ZipArchiveInputStream.matches(signature, signatureLength)) {
                return new ZipArchiveInputStream(in);
            } else if (JarArchiveInputStream
                    .matches(signature, signatureLength)) {
                return new JarArchiveInputStream(in);
            } else if (TarArchiveInputStream
                    .matches(signature, signatureLength)) {
                return new TarArchiveInputStream(in);
            } else if (ArArchiveInputStream.matches(signature, signatureLength)) {
                return new ArArchiveInputStream(in);
            } else if (CpioArchiveInputStream.matches(signature,
                    signatureLength)) {
                return new CpioArchiveInputStream(in);
            }
        } catch (IOException e) {
            throw new ArchiveException(
                    "Could not use reset and mark operations.", e);
        }

        throw new ArchiveException(
                "No Archiver found for the stream signature");
    }
}

@@ -2287,32 +2287,66 @@
 ArchiveException
+ if the archiver name is not known
 %0A     * @throws 
@@ -2391,36 +2391,33 @@
 er name 
-is null or not known
+or stream is null
 %0A     */
@@ -3635,32 +3635,66 @@
 ArchiveException
+ if the archiver name is not known
 %0A     * @throws 
@@ -3739,36 +3739,33 @@
 er name 
-is null or not known
+or stream is null
 %0A     */
@@ -5005,16 +5005,50 @@
 xception
+ if the archiver name is not known
 %0A     * 
@@ -5083,37 +5083,30 @@
 tion if the 
-archiver n
+stre
 am
-e
  is null or 
@@ -5105,25 +5105,37 @@
 null or 
-not known
+does not support mark
 %0A     */

Fix Javadoc