View Javadoc

1   /*
2    * $Id: WritableFile.java,v 1.4 2005/05/27 12:23:44 russel Exp $
3    *
4    * Copyright 2003 (C) John Wilson. All Rights Reserved.
5    *
6    * Redistribution and use of this software and associated documentation
7    * ("Software"), with or without modification, are permitted provided that the
8    * following conditions are met:
9    *  1. Redistributions of source code must retain copyright statements and
10   * notices. Redistributions must also contain a copy of this document.
11   *  2. Redistributions in binary form must reproduce the above copyright
12   * notice, this list of conditions and the following disclaimer in the
13   * documentation and/or other materials provided with the distribution.
14   *  3. The name "groovy" must not be used to endorse or promote products
15   * derived from this Software without prior written permission of The Codehaus.
16   * For written permission, please contact info@codehaus.org.
17   *  4. Products derived from this Software may not be called "groovy" nor may
18   * "groovy" appear in their names without prior written permission of The
19   * Codehaus. "groovy" is a registered trademark of The Codehaus.
20   *  5. Due credit should be given to The Codehaus - http://groovy.codehaus.org/
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY
23   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25   * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR
26   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32   * DAMAGE.
33   *
34   */
35  
36  package org.codehaus.groovy.runtime;
37  
38  import groovy.lang.Writable;
39  
40  import java.io.File;
41  import java.io.Writer;
42  import java.io.IOException;
43  import java.io.Reader;
44  import java.io.FilenameFilter;
45  import java.io.FileFilter;
46  import java.net.URI;
47  import java.net.URL;
48  import java.net.MalformedURLException;
49  
50  /***
51   * A Writable File.
52   *
53   * @author John Wilson
54   *
55   */
56  public class WritableFile extends File implements Writable {
57      private final File delegate;
58      private final String encoding;
59  
60      public WritableFile(File delegate) {
61          this(delegate, null);
62      }
63  
64      public WritableFile(File delegate, String encoding) {
65          super("");
66          this.delegate = delegate;
67          this.encoding = encoding;
68      }
69  
70      public Writer writeTo(Writer out) throws IOException {
71          final Reader reader =
72              (this.encoding == null)
73                  ? DefaultGroovyMethods.newReader(this.delegate)
74                  : DefaultGroovyMethods.newReader(this.delegate, this.encoding);
75  
76          try {
77              int c = reader.read();
78  
79              while (c != -1) {
80                  out.write(c);
81                  c = reader.read();
82              }
83          }
84          finally {
85              reader.close();
86          }
87          return out;
88      }
89  
90      public boolean canRead() {
91          return delegate.canRead();
92      }
93  
94      public boolean canWrite() {
95          return delegate.canWrite();
96      }
97  
98      public int compareTo(File arg0) {
99          return delegate.compareTo(arg0);
100     }
101 
102     public int compareTo(Object arg0) {
103         return delegate.compareTo(arg0);
104     }
105 
106     public boolean createNewFile() throws IOException {
107         return delegate.createNewFile();
108     }
109 
110     public boolean delete() {
111         return delegate.delete();
112     }
113 
114     public void deleteOnExit() {
115         delegate.deleteOnExit();
116     }
117 
118     public boolean equals(Object arg0) {
119         return delegate.equals(arg0);
120     }
121 
122     public boolean exists() {
123         return delegate.exists();
124     }
125 
126     public File getAbsoluteFile() {
127         return delegate.getAbsoluteFile();
128     }
129 
130     public String getAbsolutePath() {
131         return delegate.getAbsolutePath();
132     }
133 
134     public File getCanonicalFile() throws IOException {
135         return delegate.getCanonicalFile();
136     }
137 
138     public String getCanonicalPath() throws IOException {
139         return delegate.getCanonicalPath();
140     }
141 
142     public String getName() {
143         return delegate.getName();
144     }
145 
146     public String getParent() {
147         return delegate.getParent();
148     }
149 
150     public File getParentFile() {
151         return delegate.getParentFile();
152     }
153 
154     public String getPath() {
155         return delegate.getPath();
156     }
157 
158     public int hashCode() {
159         return delegate.hashCode();
160     }
161 
162     public boolean isAbsolute() {
163         return delegate.isAbsolute();
164     }
165 
166     public boolean isDirectory() {
167         return delegate.isDirectory();
168     }
169 
170     public boolean isFile() {
171         return delegate.isFile();
172     }
173 
174     /* (non-Javadoc)
175      * @see java.io.File#isHidden()
176      */
177     public boolean isHidden() {
178         return delegate.isHidden();
179     }
180 
181     /* (non-Javadoc)
182      * @see java.io.File#lastModified()
183      */
184     public long lastModified() {
185         return delegate.lastModified();
186     }
187 
188     /* (non-Javadoc)
189      * @see java.io.File#length()
190      */
191     public long length() {
192         return delegate.length();
193     }
194 
195     /* (non-Javadoc)
196      * @see java.io.File#list()
197      */
198     public String[] list() {
199         return delegate.list();
200     }
201 
202     /* (non-Javadoc)
203      * @see java.io.File#list(java.io.FilenameFilter)
204      */
205     public String[] list(FilenameFilter arg0) {
206         return delegate.list(arg0);
207     }
208 
209     /* (non-Javadoc)
210      * @see java.io.File#listFiles()
211      */
212     public File[] listFiles() {
213         return delegate.listFiles();
214     }
215 
216     /* (non-Javadoc)
217      * @see java.io.File#listFiles(java.io.FileFilter)
218      */
219     public File[] listFiles(FileFilter arg0) {
220         return delegate.listFiles(arg0);
221     }
222 
223     /* (non-Javadoc)
224      * @see java.io.File#listFiles(java.io.FilenameFilter)
225      */
226     public File[] listFiles(FilenameFilter arg0) {
227         return delegate.listFiles(arg0);
228     }
229 
230     /* (non-Javadoc)
231      * @see java.io.File#mkdir()
232      */
233     public boolean mkdir() {
234         return delegate.mkdir();
235     }
236 
237     /* (non-Javadoc)
238      * @see java.io.File#mkdirs()
239      */
240     public boolean mkdirs() {
241         return delegate.mkdirs();
242     }
243 
244     /* (non-Javadoc)
245      * @see java.io.File#renameTo(java.io.File)
246      */
247     public boolean renameTo(File arg0) {
248         return delegate.renameTo(arg0);
249     }
250 
251     /* (non-Javadoc)
252      * @see java.io.File#setLastModified(long)
253      */
254     public boolean setLastModified(long arg0) {
255         return delegate.setLastModified(arg0);
256     }
257 
258     /* (non-Javadoc)
259      * @see java.io.File#setReadOnly()
260      */
261     public boolean setReadOnly() {
262         return delegate.setReadOnly();
263     }
264 
265     /* (non-Javadoc)
266      * @see java.io.File#toString()
267      */
268     public String toString() {
269         return delegate.toString();
270     }
271 
272     /* (non-Javadoc)
273      * @see java.io.File#toURI()
274      */
275     public URI toURI() {
276         return delegate.toURI();
277     }
278 
279     /* (non-Javadoc)
280      * @see java.io.File#toURL()
281      */
282     public URL toURL() throws MalformedURLException {
283         return delegate.toURL();
284     }
285 
286 }