001package org.apache.maven.doxia.sink;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.io.FileOutputStream;
024import java.io.IOException;
025import java.io.OutputStream;
026
027import org.codehaus.plexus.util.WriterFactory;
028
029/**
030 * An abstract <code>SinkFactory</code> for binary output.
031 *
032 * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
033 * @version $Id$
034 * @since 1.1
035 */
036public abstract class AbstractBinarySinkFactory
037    implements SinkFactory
038{
039    /** {@inheritDoc} */
040    public Sink createSink( File outputDir, String outputName )
041        throws IOException
042    {
043        return createSink( outputDir, outputName, WriterFactory.UTF_8 );
044    }
045
046    /** {@inheritDoc} */
047    public Sink createSink( File outputDir, String outputName, String encoding )
048        throws IOException
049    {
050        if ( outputDir == null )
051        {
052            throw new IllegalArgumentException( "outputDir cannot be null." );
053        }
054
055        if ( !outputDir.exists() )
056        {
057            outputDir.mkdirs();
058        }
059        else
060        {
061            if ( !outputDir.isDirectory() )
062            {
063                throw new IllegalArgumentException( "The dir '" + outputDir + "' is not a directory." );
064            }
065        }
066
067        OutputStream out = new FileOutputStream( new File( outputDir, outputName ) );
068
069        return createSink( out, encoding );
070    }
071
072    /** {@inheritDoc} */
073    public Sink createSink( OutputStream out )
074        throws IOException
075    {
076        return createSink( out, WriterFactory.UTF_8 );
077    }
078}