001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.file;
018:
019: import java.io.BufferedInputStream;
020: import java.io.FileInputStream;
021: import java.io.FileWriter;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.net.URI;
025:
026: import org.apache.wicket.util.io.Streams;
027: import org.apache.wicket.util.time.Time;
028: import org.apache.wicket.util.watch.IModifiable;
029:
030: /**
031: * Simple extension of File that adds an implementation of IModifiable for
032: * files. This allows the ModificationWatcher class to watch files for
033: * modification. The IModifiable.lastModifiedTime() method also returns a Time
034: * object with a more convenient API than either Date or a value in
035: * milliseconds.
036: *
037: * @author Jonathan Locke
038: */
039: public class File extends java.io.File implements IModifiable {
040: private static final long serialVersionUID = 1L;
041:
042: /**
043: * Constructor.
044: *
045: * @param parent
046: * parent
047: * @param child
048: * child
049: */
050: public File(final File parent, final String child) {
051: super (parent, child);
052: }
053:
054: /**
055: * Construct.
056: * @param parent
057: * @param child
058: */
059: public File(final java.io.File parent, final String child) {
060: super (parent, child);
061: }
062:
063: /**
064: * Construct.
065: *
066: * @param file
067: * File from java.io package
068: */
069: public File(final java.io.File file) {
070: super (file.getAbsolutePath());
071: }
072:
073: /**
074: * Constructor.
075: *
076: * @param pathname
077: * path name
078: */
079: public File(final String pathname) {
080: super (pathname);
081: }
082:
083: /**
084: * Constructor.
085: *
086: * @param parent
087: * parent
088: * @param child
089: * child
090: */
091: public File(final String parent, final String child) {
092: super (parent, child);
093: }
094:
095: /**
096: * Constructor.
097: *
098: * @param uri
099: * file uri
100: */
101: public File(final URI uri) {
102: super (uri);
103: }
104:
105: /**
106: * @return File extension (whatever is after the last '.' in the file name)
107: */
108: public String getExtension() {
109: final int lastDot = getName().lastIndexOf('.');
110: if (lastDot >= 0) {
111: return getName().substring(lastDot + 1);
112: }
113: return null;
114: }
115:
116: /**
117: * @return Parent folder
118: */
119: public Folder getParentFolder() {
120: return new Folder(getParent());
121: }
122:
123: /**
124: * Returns a Time object representing the most recent time this file was
125: * modified.
126: *
127: * @return This file's lastModified() value as a Time object
128: */
129: public final Time lastModifiedTime() {
130: return Time.milliseconds(lastModified());
131: }
132:
133: /**
134: * @return String read from this file
135: * @throws IOException
136: */
137: public final String readString() throws IOException {
138: final InputStream in = new FileInputStream(this );
139: try {
140: return Streams.readString(in);
141: } finally {
142: in.close();
143: }
144: }
145:
146: /**
147: * @return True if the file was removed
148: * @see java.io.File#delete()
149: */
150: public boolean remove() {
151: return Files.remove(this );
152: }
153:
154: /**
155: * Force contents of file to physical storage
156: * @throws IOException
157: */
158: public void sync() throws IOException {
159: final FileInputStream in = new FileInputStream(this );
160: try {
161: in.getFD().sync();
162: } finally {
163: in.close();
164: }
165: }
166:
167: /**
168: * Writes the given file to this one
169: *
170: * @param file
171: * The file to copy
172: * @throws IOException
173: */
174: public final void write(final File file) throws IOException {
175: final InputStream in = new BufferedInputStream(
176: new FileInputStream(file));
177: try {
178: write(in);
179: } finally {
180: in.close();
181: }
182: }
183:
184: /**
185: * Writes the given input stream to this file
186: *
187: * @param input
188: * The input
189: * @return Number of bytes written
190: * @throws IOException
191: */
192: public final int write(final InputStream input) throws IOException {
193: return Files.writeTo(this , input);
194: }
195:
196: /**
197: * Write the given string to this file
198: *
199: * @param string
200: * The string to write
201: * @throws IOException
202: */
203: public final void write(final String string) throws IOException {
204: final FileWriter out = new FileWriter(this);
205: try {
206: out.write(string);
207: } finally {
208: out.close();
209: }
210: }
211: }
|