View Javadoc

1   package org.apache.maven.wagon;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileDescriptor;
24  import java.io.FileNotFoundException;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.nio.channels.FileChannel;
29  
30  
31  /**
32   * Variant of FileOutputStream which creates the file only when first portion
33   * of data is written.
34   *
35   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
36   * @version $Id: LazyFileOutputStream.java 668181 2008-06-16 15:05:52Z brett $
37   */
38  public class LazyFileOutputStream
39      extends OutputStream
40  {
41  
42      private File file;
43  
44      private FileOutputStream delegee;
45  
46  
47      public LazyFileOutputStream( String filename )
48      {
49          this.file = new File( filename );
50      }
51  
52      public LazyFileOutputStream( File file )
53      {
54          this.file = file;
55      }
56  
57  
58      public void close()
59          throws IOException
60      {
61          if ( delegee != null )
62          {
63              delegee.close();
64          }
65      }
66  
67  
68      public boolean equals( Object obj )
69      {
70          return delegee.equals( obj );
71      }
72  
73  
74      public void flush()
75          throws IOException
76      {
77          if ( delegee != null )
78          {
79              delegee.flush();
80          }
81      }
82  
83  
84      public FileChannel getChannel()
85      {
86          return delegee.getChannel();
87      }
88  
89  
90      public FileDescriptor getFD()
91          throws IOException
92      {
93          return delegee.getFD();
94      }
95  
96      public int hashCode()
97      {
98          return delegee.hashCode();
99      }
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 }