+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two. You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries. If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
package mir.util;
+import gnu.regexp.RE;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
-import java.io.IOException;
import java.io.FilenameFilter;
-
-import gnu.regexp.RE;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
public class FileFunctions {
protected static final int FILE_COPY_BUFFER_SIZE = 65536;
}
public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException {
- FileInputStream inputStream;
- FileOutputStream outputStream;
+ BufferedInputStream inputStream;
+ BufferedOutputStream outputStream;
int nrBytesRead;
byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];
- inputStream = new FileInputStream(aSourceFile);
+ inputStream = new BufferedInputStream(
+ new FileInputStream(aSourceFile));
try {
File directory = new File(aDestinationFile.getParent());
if (directory!=null && !directory.exists()){
directory.mkdirs();
}
- outputStream = new FileOutputStream(aDestinationFile);
+ outputStream = new BufferedOutputStream(
+ new FileOutputStream(aDestinationFile),8192);
try {
do {
nrBytesRead = inputStream.read(buffer);
}
}
+ /**
+ * Copy the contents of an {@link InputStream} to a {@link File}
+ */
+ public static void copy(InputStream aSource, File aDestination) throws IOException {
+ BufferedOutputStream outputStream =
+ new BufferedOutputStream(new FileOutputStream(aDestination), 8192);
+
+ int read;
+ byte[] buf = new byte[8 * 1024];
+
+ while ((read = aSource.read(buf)) != -1) {
+ outputStream.write(buf, 0, read);
+ }
+
+ aSource.close();
+ outputStream.close();
+ }
+
+ /**
+ * Moves a {@link File} to a new location
+ */
+ public static void move(File aSource, File aDestination) throws IOException {
+ aDestination.getParentFile().mkdirs();
+ if (!aSource.renameTo(aDestination)) {
+ byte[] buffer = new byte[16384];
+ FileInputStream inputStream = new FileInputStream(aSource);
+ FileOutputStream outputStream = new FileOutputStream(aDestination);
+ try {
+ int count=inputStream.read(buffer);
+ while (count>0) {
+ outputStream.write(buffer, 0, count);
+ count=inputStream.read(buffer);
+ }
+ }
+ finally {
+ outputStream.close();
+ inputStream.close();
+ }
+ aSource.delete();
+ }
+ }
+
+ /**
+ * Reads the content of a file into a string
+ */
+ public static String readFileIntoString(String fileName)
+ throws IOException
+ {
+ return new String(readFileIntoByteArray(fileName));
+ }
+ /**
+ * Reads the content of a file into an array of bytes
+ */
+ public static byte[] readFileIntoByteArray(String fileName)
+ throws IOException
+ {
+ FileInputStream input = new FileInputStream(fileName);
+ int size= input.available();
+ byte result[]= new byte[size];
+ input.read(result);
+ return result;
+ }
+
public static class RegExpFileFilter implements FilenameFilter {
private RE expression;
public boolean accept(File aDir, String aName) {
return new File(aDir, aName).isDirectory();
}
+ }
+
+ public static List getDirectoryContentsAsList(File aDirectory, FilenameFilter aFilter) {
+ Object[] contents = aDirectory.list(aFilter);
+ if (contents==null)
+ return Collections.EMPTY_LIST;
+ return Arrays.asList(contents);
+ }
+
+ public static String getExtension(String aPath) {
+ int position = aPath.lastIndexOf('.');
+ if (position>=0) {
+ return aPath.substring(position+1);
+ }
+ return "";
+ }
+ public static boolean isAbsolutePath(String aPath) {
+ return new File(aPath).isAbsolute();
}
-}
\ No newline at end of file
+ public static File getAbsoluteOrRelativeFile(File aParentIfRelative, String aPath) {
+ if (isAbsolutePath(aPath)) {
+ return new File(aPath);
+ }
+ return new File(aParentIfRelative, aPath);
+ }
+}