001package org.apache.maven.wagon;
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.FileDescriptor;
024import java.io.FileNotFoundException;
025import java.io.FileOutputStream;
026import java.io.IOException;
027import java.io.OutputStream;
028import java.nio.channels.FileChannel;
029
030
031/**
032 * Variant of FileOutputStream which creates the file only when first portion
033 * of data is written.
034 *
035 * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
036 *
037 */
038public class LazyFileOutputStream
039    extends OutputStream
040{
041
042    private File file;
043
044    private FileOutputStream delegee;
045
046
047    public LazyFileOutputStream( String filename )
048    {
049        this.file = new File( filename );
050    }
051
052    public LazyFileOutputStream( File file )
053    {
054        this.file = file;
055    }
056
057
058    public void close()
059        throws IOException
060    {
061        if ( delegee != null )
062        {
063            delegee.close();
064        }
065    }
066
067
068    public boolean equals( Object obj )
069    {
070        return delegee.equals( obj );
071    }
072
073
074    public void flush()
075        throws IOException
076    {
077        if ( delegee != null )
078        {
079            delegee.flush();
080        }
081    }
082
083
084    public FileChannel getChannel()
085    {
086        return delegee.getChannel();
087    }
088
089
090    public FileDescriptor getFD()
091        throws IOException
092    {
093        return delegee.getFD();
094    }
095
096    public int hashCode()
097    {
098        return delegee.hashCode();
099    }
100
101
102    public String toString()
103    {
104        return delegee.toString();
105    }
106
107    public void write( byte[] b )
108        throws IOException
109    {
110        if ( delegee == null )
111        {
112            initialize();
113        }
114
115        delegee.write( b );
116    }
117
118    /**
119     * @see java.io.OutputStream#write(byte[], int, int)
120     */
121    public void write( byte[] b, int off, int len )
122        throws IOException
123    {
124        if ( delegee == null )
125        {
126            initialize();
127        }
128
129        delegee.write( b, off, len );
130    }
131
132    /**
133     * @param b
134     * @throws java.io.IOException
135     */
136    public void write( int b )
137        throws IOException
138    {
139        if ( delegee == null )
140        {
141            initialize();
142        }
143
144        delegee.write( b );
145    }
146
147
148    /**
149     * 
150     */
151    private void initialize()
152        throws FileNotFoundException
153    {
154        delegee = new FileOutputStream( file );
155    }
156}