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.FileOutputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022:
023: import org.apache.wicket.util.io.Streams;
024: import org.apache.wicket.util.string.Strings;
025:
026: /**
027: * File utility methods.
028: *
029: * @author Jonathan Locke
030: */
031: public class Files {
032: /**
033: * Strips off the given extension (probably returned from Files.extension())
034: * from the path, yielding a base pathname.
035: *
036: * @param path
037: * The path, possibly with an extension to strip
038: * @param extension
039: * The extension to strip, or null if no extension exists
040: * @return The path without any extension
041: */
042: public static String basePath(final String path,
043: final String extension) {
044: if (extension != null) {
045: return path.substring(0, path.length() - extension.length()
046: - 1);
047: }
048: return path;
049: }
050:
051: /**
052: * Gets extension from path
053: *
054: * @param path
055: * The path
056: * @return The extension, like "bmp" or "html", or null if none can be found
057: */
058: public static String extension(final String path) {
059: if (path.indexOf('.') != -1) {
060: return Strings.lastPathComponent(path, '.');
061: }
062: return null;
063: }
064:
065: /**
066: * Gets filename from path
067: *
068: * @param path
069: * The path
070: * @return The filename
071: */
072: public static String filename(final String path) {
073: return Strings
074: .lastPathComponent(path.replace('/',
075: java.io.File.separatorChar),
076: java.io.File.separatorChar);
077: }
078:
079: /**
080: * Deletes a file, dealing with a particularly nasty bug on Windows.
081: *
082: * @param file
083: * File to delete
084: * @return True if file was deleted
085: */
086: public static boolean remove(final java.io.File file) {
087: // Delete current file
088: if (!file.delete()) {
089: // NOTE: fix for java/win bug. see:
090: // http://forum.java.sun.com/thread.jsp?forum=4&thread=158689&tstart=0&trange=15
091: System.gc();
092: try {
093: Thread.sleep(100);
094: } catch (InterruptedException e) {
095: }
096:
097: // Try one more time to delete the file
098: return file.delete();
099: }
100: return true;
101: }
102:
103: /**
104: * Writes the given input stream to the given file
105: *
106: * @param file
107: * The file to write to
108: * @param input
109: * The input
110: * @return Number of bytes written
111: * @throws IOException
112: */
113: public static final int writeTo(final java.io.File file,
114: final InputStream input) throws IOException {
115: final FileOutputStream out = new FileOutputStream(file);
116: try {
117: return Streams.copy(input, out);
118: } finally {
119: out.close();
120: }
121: }
122:
123: /**
124: * Private constructor to prevent instantiation.
125: */
126: private Files() {
127: }
128: }
|