001 /* 002 * $Id: WritableFile.java,v 1.4 2005/05/27 12:23:44 russel Exp $ 003 * 004 * Copyright 2003 (C) John Wilson. All Rights Reserved. 005 * 006 * Redistribution and use of this software and associated documentation 007 * ("Software"), with or without modification, are permitted provided that the 008 * following conditions are met: 009 * 1. Redistributions of source code must retain copyright statements and 010 * notices. Redistributions must also contain a copy of this document. 011 * 2. Redistributions in binary form must reproduce the above copyright 012 * notice, this list of conditions and the following disclaimer in the 013 * documentation and/or other materials provided with the distribution. 014 * 3. The name "groovy" must not be used to endorse or promote products 015 * derived from this Software without prior written permission of The Codehaus. 016 * For written permission, please contact info@codehaus.org. 017 * 4. Products derived from this Software may not be called "groovy" nor may 018 * "groovy" appear in their names without prior written permission of The 019 * Codehaus. "groovy" is a registered trademark of The Codehaus. 020 * 5. Due credit should be given to The Codehaus - http://groovy.codehaus.org/ 021 * 022 * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY 023 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 024 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 025 * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR 026 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 027 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 028 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 031 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 032 * DAMAGE. 033 * 034 */ 035 036 package org.codehaus.groovy.runtime; 037 038 import groovy.lang.Writable; 039 040 import java.io.File; 041 import java.io.Writer; 042 import java.io.IOException; 043 import java.io.Reader; 044 import java.io.FilenameFilter; 045 import java.io.FileFilter; 046 import java.net.URI; 047 import java.net.URL; 048 import java.net.MalformedURLException; 049 050 /** 051 * A Writable File. 052 * 053 * @author John Wilson 054 * 055 */ 056 public class WritableFile extends File implements Writable { 057 private final File delegate; 058 private final String encoding; 059 060 public WritableFile(File delegate) { 061 this(delegate, null); 062 } 063 064 public WritableFile(File delegate, String encoding) { 065 super(""); 066 this.delegate = delegate; 067 this.encoding = encoding; 068 } 069 070 public Writer writeTo(Writer out) throws IOException { 071 final Reader reader = 072 (this.encoding == null) 073 ? DefaultGroovyMethods.newReader(this.delegate) 074 : DefaultGroovyMethods.newReader(this.delegate, this.encoding); 075 076 try { 077 int c = reader.read(); 078 079 while (c != -1) { 080 out.write(c); 081 c = reader.read(); 082 } 083 } 084 finally { 085 reader.close(); 086 } 087 return out; 088 } 089 090 public boolean canRead() { 091 return delegate.canRead(); 092 } 093 094 public boolean canWrite() { 095 return delegate.canWrite(); 096 } 097 098 public int compareTo(File arg0) { 099 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 }