package org.jboss.util.file;
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.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import org.jboss.logging.Logger;
import org.jboss.util.stream.Streams;
public final class Files
{
private static final Logger log = Logger.getLogger(Files.class);
private static final char[] hexDigits = new char[]
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
public static final int DEFAULT_BUFFER_SIZE = 8192;
public static boolean delete(final File dir)
{
boolean success = true;
File files[] = dir.listFiles();
if (files != null)
{
for (int i = 0; i < files.length; i++)
{
File f = files[i];
if( f.isDirectory() == true )
{
if( delete(f) == false )
{
success = false;
log.debug("Failed to delete dir: "+f.getAbsolutePath());
}
}
else if( f.delete() == false )
{
success = false;
log.debug("Failed to delete file: "+f.getAbsolutePath());
}
}
}
if( dir.delete() == false )
{
success = false;
log.debug("Failed to delete dir: "+dir.getAbsolutePath());
}
return success;
}
public static boolean delete(final String dirname)
{
return delete(new File(dirname));
}
public static boolean deleteContaining(final String filename)
{
File file = new File(filename);
File containingDir = file.getParentFile();
return delete(containingDir);
}
public static void copy(final File source,
final File target,
final byte buff[])
throws IOException
{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target));
int read;
try
{
while ((read = in.read(buff)) != -1)
{
out.write(buff, 0, read);
}
}
finally
{
Streams.flush(out);
Streams.close(in);
Streams.close(out);
}
}
public static void copy(final File source,
final File target,
final int size)
throws IOException
{
copy(source, target, new byte[size]);
}
public static void copy(final File source, final File target)
throws IOException
{
copy(source, target, DEFAULT_BUFFER_SIZE);
}
public static void copy(URL src, File dest) throws IOException
{
log.debug("Copying " + src + " -> " + dest);
File dir = dest.getParentFile();
if (!dir.exists())
{
if (!dir.mkdirs())
{
throw new IOException("mkdirs failed for: " + dir.getAbsolutePath());
}
}
if (dest.exists())
{
if (!Files.delete(dest))
{
throw new IOException("delete of previous content failed for: " + dest.getAbsolutePath());
}
}
InputStream in = new BufferedInputStream(src.openStream());
OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
Streams.copy(in, out);
out.flush();
out.close();
in.close();
}
public static String encodeFileName(String name)
{
StringBuffer rc = new StringBuffer();
for (int i = 0; i < name.length(); i++ )
{
switch (name.charAt(i))
{
case 'a': case 'A': case 'b': case 'B': case 'c': case 'C':
case 'd': case 'D': case 'e': case 'E': case 'f': case 'F':
case 'g': case 'G': case 'h': case 'H': case 'i': case 'I':
case 'j': case 'J': case 'k': case 'K': case 'l': case 'L':
case 'm': case 'M': case 'n': case 'N': case 'o': case 'O':
case 'p': case 'P': case 'q': case 'Q': case 'r': case 'R':
case 's': case 'S': case 't': case 'T': case 'u': case 'U':
case 'v': case 'V': case 'w': case 'W': case 'x': case 'X':
case 'y': case 'Y': case 'z': case 'Z':
case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': case '0':
case '-': case '_': case '.':
rc.append(name.charAt(i));
break;
default:
try
{
byte data[] = ("" + name.charAt(i)).getBytes("UTF8");
for (int j = 0; j < data.length; j++)
{
rc.append('%');
rc.append(hexDigits[ (data[j] >> 4) & 0xF ]); rc.append(hexDigits[ (data[j] ) & 0xF ]); }
}
catch (UnsupportedEncodingException wonthappen)
{
}
}
}
return rc.toString();
}
public static String decodeFileName(String name)
{
if (name == null)
{
return null;
}
StringBuffer sbuf = new StringBuffer(name.length());
for (int i = 0; i < name.length(); i++)
{
char c = name.charAt(i);
if (c == '%')
{
char h1 = name.charAt(++i);
char h2 = name.charAt(++i);
int d1 = (h1 >= 'a') ? (10 + h1 - 'a')
: ((h1 >= 'A') ? (10 + h1 - 'A')
: (h1 - '0'));
int d2 = (h2 >= 'a') ? (10 + h2 - 'a')
: ((h2 >= 'A') ? (10 + h2 - 'A')
: (h2 - '0'));
byte[] bytes = new byte[] { (byte)(d1 * 16 + d2) };
try
{
String s = new String(bytes, "UTF8");
sbuf.append(s);
}
catch (UnsupportedEncodingException wonthappen)
{
}
}
else
{
sbuf.append(c);
}
}
return sbuf.toString();
}
public static String findRelativePath(String base, String path)
throws IOException
{
String a = new File(base).getCanonicalFile().toURI().getPath();
String b = new File(path).getCanonicalFile().toURI().getPath();
String[] basePaths = a.split("/");
String[] otherPaths = b.split("/");
int n = 0;
for(; n < basePaths.length && n < otherPaths.length; n ++)
{
if( basePaths[n].equals(otherPaths[n]) == false )
break;
}
System.out.println("Common length: "+n);
StringBuffer tmp = new StringBuffer("../");
for(int m = n; m < basePaths.length - 1; m ++)
tmp.append("../");
for(int m = n; m < otherPaths.length; m ++)
{
tmp.append(otherPaths[m]);
tmp.append("/");
}
return tmp.toString();
}
}