001: /*
002: * $Id: Files.java 460514 2006-05-04 18:43:27Z jonl $ $Revision: 460514 $
003: * $Date: 2006-05-04 20:43:27 +0200 (Thu, 04 May 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.util.file;
019:
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import wicket.util.io.Streams;
025: import wicket.util.string.Strings;
026:
027: /**
028: * File utility methods.
029: *
030: * @author Jonathan Locke
031: */
032: public class Files {
033: /**
034: * Strips off the given extension (probably returned from Files.extension())
035: * from the path, yielding a base pathname.
036: *
037: * @param path
038: * The path, possibly with an extension to strip
039: * @param extension
040: * The extension to strip, or null if no extension exists
041: * @return The path without any extension
042: */
043: public static String basePath(final String path,
044: final String extension) {
045: if (extension != null) {
046: return path.substring(0, path.length() - extension.length()
047: - 1);
048: }
049: return path;
050: }
051:
052: /**
053: * Gets extension from path
054: *
055: * @param path
056: * The path
057: * @return The extension, like "bmp" or "html", or null if none can be found
058: */
059: public static String extension(final String path) {
060: if (path.indexOf('.') != -1) {
061: return Strings.lastPathComponent(path, '.');
062: }
063: return null;
064: }
065:
066: /**
067: * Gets filename from path
068: *
069: * @param path
070: * The path
071: * @return The filename
072: */
073: public static String filename(final String path) {
074: return Strings
075: .lastPathComponent(path.replace('/',
076: java.io.File.separatorChar),
077: java.io.File.separatorChar);
078: }
079:
080: /**
081: * Deletes a file, dealing with a particularly nasty bug on Windows.
082: *
083: * @param file
084: * File to delete
085: * @return True if file was deleted
086: */
087: public static boolean remove(final java.io.File file) {
088: // Delete current file
089: if (!file.delete()) {
090: // NOTE: fix for java/win bug. see:
091: // http://forum.java.sun.com/thread.jsp?forum=4&thread=158689&tstart=0&trange=15
092: System.gc();
093: try {
094: Thread.sleep(100);
095: } catch (InterruptedException e) {
096: }
097:
098: // Try one more time to delete the file
099: return file.delete();
100: }
101: return true;
102: }
103:
104: /**
105: * Writes the given input stream to the given file
106: *
107: * @param file
108: * The file to write to
109: * @param input
110: * The input
111: * @return Number of bytes written
112: * @throws IOException
113: */
114: public static final int writeTo(final java.io.File file,
115: final InputStream input) throws IOException {
116: final FileOutputStream out = new FileOutputStream(file);
117: try {
118: return Streams.copy(input, out);
119: } finally {
120: out.close();
121: }
122: }
123:
124: /**
125: * Private constructor to prevent instantiation.
126: */
127: private Files() {
128: }
129: }
|