A collection of file-processing util methods
| Method from org.apache.hadoop.fs.FileUtil Detail: |
public static int chmod(String filename,
String perm) throws IOException, InterruptedException {
String cmd = "chmod " + perm + " " + filename;
Process p = Runtime.getRuntime().exec(cmd, null);
return p.waitFor();
}
Change the permissions on a filename. |
public static void closeSocket(Socket sock) {
// avoids try { close() } dance
if ( sock != null ) {
try {
sock.close();
} catch ( IOException ignored ) {
}
}
}
|
public static void closeStream(Closeable closeable) {
// avoids try { close() } dance
if ( closeable != null ) {
try {
closeable.close();
} catch ( IOException ignored ) {
}
}
}
|
public static boolean copy(File src,
FileSystem dstFS,
Path dst,
boolean deleteSource,
Configuration conf) throws IOException {
dst = checkDest(src.getName(), dstFS, dst);
if (src.isDirectory()) {
if (!dstFS.mkdirs(dst)) {
return false;
}
File contents[] = src.listFiles();
for (int i = 0; i < contents.length; i++) {
copy(contents[i], dstFS, new Path(dst, contents[i].getName()),
deleteSource, conf);
}
} else if (src.isFile()) {
InputStream in = new FileInputStream(src);
try {
copyContent(in, dstFS.create(dst), conf);
} finally {
in.close();
}
}
if (deleteSource) {
return FileUtil.fullyDelete(src);
} else {
return true;
}
}
Copy local files to a FileSystem. |
public static boolean copy(FileSystem srcFS,
Path src,
File dst,
boolean deleteSource,
Configuration conf) throws IOException {
if (srcFS.isDirectory(src)) {
if (!dst.mkdirs()) {
return false;
}
Path contents[] = srcFS.listPaths(src);
for (int i = 0; i < contents.length; i++) {
copy(srcFS, contents[i], new File(dst, contents[i].getName()),
deleteSource, conf);
}
} else if (srcFS.isFile(src)) {
InputStream in = srcFS.open(src);
try {
copyContent(in, new FileOutputStream(dst), conf);
} finally {
in.close();
}
}
if (deleteSource) {
return srcFS.delete(src);
} else {
return true;
}
}
Copy FileSystem files to local files. |
public static boolean copy(FileSystem srcFS,
Path src,
FileSystem dstFS,
Path dst,
boolean deleteSource,
Configuration conf) throws IOException {
dst = checkDest(src.getName(), dstFS, dst);
if (srcFS.isDirectory(src)) {
checkDependencies(srcFS, src, dstFS, dst);
if (!dstFS.mkdirs(dst)) {
return false;
}
Path contents[] = srcFS.listPaths(src);
for (int i = 0; i < contents.length; i++) {
copy(srcFS, contents[i], dstFS, new Path(dst, contents[i].getName()),
deleteSource, conf);
}
} else if (srcFS.isFile(src)) {
InputStream in = srcFS.open(src);
try {
OutputStream out = dstFS.create(dst);
copyContent(in, out, conf);
} finally {
in.close();
}
} else {
throw new IOException(src.toString() + ": No such file or directory");
}
if (deleteSource) {
return srcFS.delete(src);
} else {
return true;
}
}
Copy files between FileSystems. |
public static boolean copyMerge(FileSystem srcFS,
Path srcDir,
FileSystem dstFS,
Path dstFile,
boolean deleteSource,
Configuration conf,
String addString) throws IOException {
dstFile = checkDest(srcDir.getName(), dstFS, dstFile);
if (!srcFS.isDirectory(srcDir))
return false;
OutputStream out = dstFS.create(dstFile);
try {
Path contents[] = srcFS.listPaths(srcDir);
for (int i = 0; i < contents.length; i++) {
if (srcFS.isFile(contents[i])) {
InputStream in = srcFS.open(contents[i]);
try {
copyContent(in, out, conf, false);
if (addString!=null)
out.write(addString.getBytes("UTF-8"));
} finally {
in.close();
}
}
}
} finally {
out.close();
}
if (deleteSource) {
return srcFS.delete(srcDir);
} else {
return true;
}
}
Copy all files in a directory to one output file (merge). |
public static final File createLocalTempFile(File basefile,
String prefix,
boolean isDeleteOnExit) throws IOException {
File tmp = File.createTempFile(prefix + basefile.getName(),
"", basefile.getParentFile());
if (isDeleteOnExit) {
tmp.deleteOnExit();
}
return tmp;
}
Create a tmp file for a base file. |
public static boolean fullyDelete(File dir) throws IOException {
File contents[] = dir.listFiles();
if (contents != null) {
for (int i = 0; i < contents.length; i++) {
if (contents[i].isFile()) {
if (!contents[i].delete()) {
return false;
}
} else {
//try deleting the directory
// this might be a symlink
boolean b = false;
b = contents[i].delete();
if (b){
//this was indeed a symlink or an empty directory
continue;
}
// if not an empty directory or symlink let
// fullydelete handle it.
if (!fullyDelete(contents[i])) {
return false;
}
}
}
}
return dir.delete();
}
Delete a directory and all its contents. If
we return false, the directory may be partially-deleted. |
public static void fullyDelete(FileSystem fs,
Path dir) throws IOException {
Path[] paths = fs.listPaths(dir);
if (paths != null) {
for (Path p : paths) {
if (fs.isFile(p)) {
fs.delete(p);
} else {
fullyDelete(fs, p);
}
}
}
fs.delete(dir);
}
Recursively delete a directory. |
public static long getDU(File dir) {
long size = 0;
if (!dir.exists())
return 0;
if (!dir.isDirectory()) {
return dir.length();
} else {
size = dir.length();
File[] allFiles = dir.listFiles();
for (int i = 0; i < allFiles.length; i++) {
size = size + getDU(allFiles[i]);
}
return size;
}
}
Takes an input dir and returns the du on that local directory. Very basic
implementation. |
public static String makeShellPath(String filename) throws IOException {
if (Path.WINDOWS) {
return new CygPathCommand(filename).getResult();
} else {
return filename;
}
}
Convert a os-native filename to a path that works for the shell. |
public static String makeShellPath(File file) throws IOException {
return makeShellPath(file.toString());
}
Convert a os-native filename to a path that works for the shell. |
public static void readFully(InputStream in,
byte[] buf,
int off,
int len) throws IOException {
int toRead = len;
while ( toRead > 0 ) {
int ret = in.read( buf, off, toRead );
if ( ret < 0 ) {
throw new IOException( "Premeture EOF from inputStream");
}
toRead -= ret;
off += ret;
}
}
|
public static void skipFully(InputStream in,
long len) throws IOException {
long toSkip = len;
while ( toSkip > 0 ) {
long ret = in.skip( toSkip );
if ( ret < 0 ) {
throw new IOException( "Premeture EOF from inputStream");
}
toSkip -= ret;
}
}
|
public static int symLink(String target,
String linkname) throws IOException {
String cmd = "ln -s " + target + " " + linkname;
Process p = Runtime.getRuntime().exec(cmd, null);
int returnVal = -1;
try{
returnVal = p.waitFor();
} catch(InterruptedException e){
//do nothing as of yet
}
return returnVal;
}
Create a soft link between a src and destination
only on a local disk. HDFS does not support this |
public static void unZip(File inFile,
File unzipDir) throws IOException {
Enumeration entries;
ZipFile zipFile = new ZipFile(inFile);
try {
entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
if (!entry.isDirectory()) {
InputStream in = zipFile.getInputStream(entry);
try {
File file = new File(unzipDir, entry.getName());
if (!file.getParentFile().mkdirs()) {
if (!file.getParentFile().isDirectory()) {
throw new IOException("Mkdirs failed to create " +
file.getParentFile().toString());
}
}
OutputStream out = new FileOutputStream(file);
try {
byte[] buffer = new byte[8192];
int i;
while ((i = in.read(buffer)) != -1) {
out.write(buffer, 0, i);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
}
} finally {
zipFile.close();
}
}
Given a File input it will unzip the file in a the unzip directory
passed as the second parameter |