*/
package mir.util;
-import gnu.regexp.RE;
-
-import java.io.*;
+import org.apache.oro.text.regex.MalformedPatternException;
+import org.apache.oro.text.regex.Pattern;
+import org.apache.oro.text.regex.Perl5Compiler;
+import org.apache.oro.text.regex.Perl5Matcher;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
inputStream = new BufferedInputStream(new FileInputStream(aSourceFile));
try {
- File directory = new File(aDestinationFile.getParent());
- if (directory != null && !directory.exists()) {
- directory.mkdirs();
- }
+ createParentDirectories(aDestinationFile);
+
outputStream = new BufferedOutputStream(new FileOutputStream(aDestinationFile), 8192);
try {
do {
nrBytesRead = inputStream.read(buffer);
- if (nrBytesRead > 0)
+ if (nrBytesRead > 0) {
outputStream.write(buffer, 0, nrBytesRead);
+ }
}
while (nrBytesRead >= 0);
}
}
public static class RegExpFileFilter implements FilenameFilter {
- private RE expression;
+ private Pattern expression;
- public RegExpFileFilter(String anExpression) {
- try {
- expression = new RE(anExpression);
- }
- catch (Throwable t) {
- throw new RuntimeException(t.getMessage());
- }
+ public RegExpFileFilter(String anExpression) throws MalformedPatternException {
+ Perl5Compiler compiler = new Perl5Compiler();
+
+ expression = compiler.compile(anExpression, Perl5Compiler.READ_ONLY_MASK);
}
public boolean accept(File aDir, String aName) {
- return expression.isMatch(aName) && !new File(aDir, aName).isDirectory();
+ Perl5Matcher matcher = new Perl5Matcher();
+
+ return !new File(aDir, aName).isDirectory() && matcher.matches(aName, expression);
}
}
}
/**
- * Return the extension of a path. (e.g. <code>getExtension("example.txt")</code> will
+ * Returns the extension of a path. (e.g. <code>getExtension("example.txt")</code> will
* return <code>"txt"</code>
*/
public static String getExtension(String aPath) {
int position = aPath.lastIndexOf('.');
+
if (position >= 0) {
return aPath.substring(position + 1);
}
+
return "";
}
+ /**
+ * Returns <code>true</code> if the given path is absolute
+ */
public static boolean isAbsolutePath(String aPath) {
return new File(aPath).isAbsolute();
}
return result.toByteArray();
}
- /**
- * Reads the content of a file into an array of bytes
- */
- public static void writeByteArrayIntoFile(byte[] anArray, File aFile) throws IOException {
- OutputStream output = new FileOutputStream(aFile);
-
- try {
- ByteArrayInputStream input = new ByteArrayInputStream(anArray);
- try {
- IORoutines.copyStream(input, output);
- }
- finally {
- input.close();
- }
- }
- finally {
- output.close();
- }
- }
-
-
+ /**
+ * Reads the content of a file into an array of bytes
+ */
+ public static void writeByteArrayIntoFile(byte[] anArray, File aFile) throws IOException {
+ OutputStream output = new FileOutputStream(aFile);
+ try {
+ ByteArrayInputStream input = new ByteArrayInputStream(anArray);
+ try {
+ IORoutines.copyStream(input, output);
+ }
+ finally {
+ input.close();
+ }
+ }
+ finally {
+ output.close();
+ }
+ }
/**
* Creates all parent directories of a file if they do not exist