
java.fedora.utilities.FileUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fcrepo-client Show documentation
Show all versions of fcrepo-client Show documentation
The Fedora Client is a Java Library that allows API access to a Fedora Repository. The client is typically one part of a full Fedora installation.
The newest version!
/*
* -----------------------------------------------------------------------------
*
* License and Copyright: The contents of this file are subject to the
* Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
* http://www.fedora-commons.org/licenses.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The entire file consists of original code.
* Copyright © 2008 Fedora Commons, Inc.
*
Copyright © 2002-2007 The Rector and Visitors of the University of
* Virginia and Cornell University
* All rights reserved.
*
* -----------------------------------------------------------------------------
*/
package fedora.utilities;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
public class FileUtils {
public static final int BUFFER = 2048;
/**
* Copy an InputStream to an OutputStream.
*
* @param source
* @param destination
* @return true
if the operation was successful;
* false
otherwise (which includes a null input).
*/
public static boolean copy(InputStream source, OutputStream destination) {
int count;
byte data[] = new byte[BUFFER];
BufferedOutputStream dest = new BufferedOutputStream(destination, BUFFER);
try {
while ((count = source.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
return true;
} catch (IOException e) {
return false;
}
}
/**
* Copy a file or directory.
*
* @param source
* @param destination
* @return true
if the operation was successful;
* false
otherwise (which includes a null input).
*/
public static boolean copy(File source, File destination) {
boolean result = true;
if (source.isDirectory()) {
if (destination.exists()) {
result = result && destination.isDirectory();
} else {
result = result && destination.mkdirs();
}
File[] children = source.listFiles();
for (int i = 0; i < children.length; i++) {
result = result && copy(new File(source, children[i].getName()),
new File(destination, children[i].getName()));
}
return result;
} else {
try {
InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(destination);
result = result && copy(in, out);
out.close();
in.close();
return result;
} catch (IOException e) {
return false;
}
}
}
/**
* Delete a File.
*
* @param file the File to delete.
*
* @return true
if the operation was successful;
* false
otherwise (which includes a null input).
*/
public static boolean delete(File file) {
boolean result = true;
if (file == null) {
return false;
}
if (file.exists()) {
if (file.isDirectory()) {
// 1. delete content of directory:
File[] children = file.listFiles();
for (int i = 0; i < children.length; i++) { //for each file:
File child = children[i];
result = result && delete(child);
}//next file
}
result = result && file.delete();
} //else: input directory does not exist or is not a directory
return result;
}
/**
* Delete the specified file or directory.
* @param file File or directory to delete
* @return true
if the operation was successful;
* false
otherwise (which includes a null input).
*/
public static boolean delete(String file) {
return delete(new File(file));
}
/**
* Move a file or directory.
* Initally attempts to move the File using java.io.File.renameTo().
* However, should this operation fail (e.g. when source and destination
* are across different filesystems), will attempt to copy and then delete
* the source.
*
* @param source
* @param destination
* @return true
if the operation was successful;
* false
otherwise (which includes a null input).
*/
public static boolean move(File source, File destination) {
if (source == null || destination == null) {
return false;
}
if (source.renameTo(destination)) {
return true;
} else {
return copy(source, destination) && delete(source);
}
}
/**
* Load properties from the given file.
*/
public static Properties loadProperties(File f) throws IOException {
Properties props = new Properties();
FileInputStream in = new FileInputStream(f);
try {
props.load(in);
return props;
} finally {
try { in.close(); } catch (IOException e) { }
}
}
public static FileFilter getPrefixFileFilter(String prefix) {
return new PrefixFileFilter(prefix);
}
public static FileFilter getSuffixFileFilter(String suffix) {
return new SuffixFileFilter(suffix);
}
private static class PrefixFileFilter implements FileFilter {
private final String filenamePrefix;
PrefixFileFilter(String filenamePrefix) {
this.filenamePrefix = filenamePrefix;
}
public boolean accept(File file) {
String filename = file.getName();
return filename.startsWith(this.filenamePrefix);
}
}
private static class SuffixFileFilter implements FileFilter {
private final String filenameSuffix;
SuffixFileFilter(String filenameSuffix) {
this.filenameSuffix = filenameSuffix;
}
public boolean accept(File file) {
String filename = file.getName();
return filename.endsWith(this.filenameSuffix);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy