|
|
|
@ -32,6 +32,7 @@ import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
@ -66,8 +67,8 @@ import net.yacy.kelondro.index.Row;
|
|
|
|
|
import net.yacy.kelondro.index.RowSet;
|
|
|
|
|
import net.yacy.kelondro.logging.Log;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public final class FileUtils {
|
|
|
|
|
public final class FileUtils
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private static final int DEFAULT_BUFFER_SIZE = 1024; // this is also the maximum chunk size
|
|
|
|
|
|
|
|
|
@ -83,13 +84,13 @@ public final class FileUtils {
|
|
|
|
|
* @param count the total amount of bytes to copy (-1 for all, else must be greater than zero)
|
|
|
|
|
* @return Total number of bytes copied.
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*
|
|
|
|
|
* @see #copy(InputStream source, File dest)
|
|
|
|
|
* @see #copyRange(File source, OutputStream dest, int start)
|
|
|
|
|
* @see #copy(File source, OutputStream dest)
|
|
|
|
|
* @see #copy(File source, File dest)
|
|
|
|
|
*/
|
|
|
|
|
public static long copy(final InputStream source, final OutputStream dest, final long count) throws IOException {
|
|
|
|
|
public static long copy(final InputStream source, final OutputStream dest, final long count)
|
|
|
|
|
throws IOException {
|
|
|
|
|
assert count < 0 || count > 0 : "precondition violated: count == " + count + " (nothing to copy)";
|
|
|
|
|
if ( count == 0 ) {
|
|
|
|
|
// no bytes to copy
|
|
|
|
@ -99,7 +100,8 @@ public final class FileUtils {
|
|
|
|
|
final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
|
|
|
|
int chunkSize = (int) ((count > 0) ? Math.min(count, DEFAULT_BUFFER_SIZE) : DEFAULT_BUFFER_SIZE);
|
|
|
|
|
|
|
|
|
|
int c; long total = 0;
|
|
|
|
|
int c;
|
|
|
|
|
long total = 0;
|
|
|
|
|
while ( (c = source.read(buffer, 0, chunkSize)) > 0 ) {
|
|
|
|
|
dest.write(buffer, 0, c);
|
|
|
|
|
dest.flush();
|
|
|
|
@ -107,7 +109,9 @@ public final class FileUtils {
|
|
|
|
|
|
|
|
|
|
if ( count > 0 ) {
|
|
|
|
|
chunkSize = (int) Math.min(count - total, DEFAULT_BUFFER_SIZE);
|
|
|
|
|
if (chunkSize == 0) break;
|
|
|
|
|
if ( chunkSize == 0 ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -116,13 +120,19 @@ public final class FileUtils {
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int copy(final File source, final Charset inputCharset, final Writer dest) throws IOException {
|
|
|
|
|
public static int copy(final File source, final Charset inputCharset, final Writer dest)
|
|
|
|
|
throws IOException {
|
|
|
|
|
InputStream fis = null;
|
|
|
|
|
try {
|
|
|
|
|
fis = new FileInputStream(source);
|
|
|
|
|
return copy(fis, dest, inputCharset);
|
|
|
|
|
} finally {
|
|
|
|
|
if (fis != null) try { fis.close(); } catch (final Exception e) {}
|
|
|
|
|
if ( fis != null ) {
|
|
|
|
|
try {
|
|
|
|
|
fis.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -131,7 +141,8 @@ public final class FileUtils {
|
|
|
|
|
return copy(reader, dest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int copy(final InputStream source, final Writer dest, final Charset inputCharset) throws IOException {
|
|
|
|
|
public static int copy(final InputStream source, final Writer dest, final Charset inputCharset)
|
|
|
|
|
throws IOException {
|
|
|
|
|
final InputStreamReader reader = new InputStreamReader(source, inputCharset);
|
|
|
|
|
return copy(reader, dest);
|
|
|
|
|
}
|
|
|
|
@ -145,8 +156,12 @@ public final class FileUtils {
|
|
|
|
|
public static int copy(final Reader source, final Writer dest) throws IOException {
|
|
|
|
|
assert source != null;
|
|
|
|
|
assert dest != null;
|
|
|
|
|
if (source == null) throw new IOException("source is null");
|
|
|
|
|
if (dest == null) throw new IOException("dest is null");
|
|
|
|
|
if ( source == null ) {
|
|
|
|
|
throw new IOException("source is null");
|
|
|
|
|
}
|
|
|
|
|
if ( dest == null ) {
|
|
|
|
|
throw new IOException("dest is null");
|
|
|
|
|
}
|
|
|
|
|
final char[] buffer = new char[DEFAULT_BUFFER_SIZE];
|
|
|
|
|
int count = 0;
|
|
|
|
|
int n = 0;
|
|
|
|
@ -160,7 +175,9 @@ public final class FileUtils {
|
|
|
|
|
assert e != null;
|
|
|
|
|
// an "sun.io.MalformedInputException: Missing byte-order mark" - exception may occur here
|
|
|
|
|
//Log.logException(e);
|
|
|
|
|
throw new IOException(e == null ? "null" : e.getMessage() == null ? e.toString() : e.getMessage(), e);
|
|
|
|
|
throw new IOException(
|
|
|
|
|
e == null ? "null" : e.getMessage() == null ? e.toString() : e.getMessage(),
|
|
|
|
|
e);
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
@ -183,18 +200,29 @@ public final class FileUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static void copy(final InputStream source, final File dest, final long count) throws IOException {
|
|
|
|
|
final String path = dest.getParent();
|
|
|
|
|
if (path != null && path.length() > 0) new File(path).mkdirs();
|
|
|
|
|
if ( path != null && path.length() > 0 ) {
|
|
|
|
|
new File(path).mkdirs();
|
|
|
|
|
}
|
|
|
|
|
FileOutputStream fos = null;
|
|
|
|
|
try {
|
|
|
|
|
fos = new FileOutputStream(dest);
|
|
|
|
|
copy(source, fos, count);
|
|
|
|
|
} finally {
|
|
|
|
|
if (fos != null) try {fos.close();} catch (final Exception e) { Log.logWarning("FileUtils", "cannot close FileOutputStream for "+ dest +"! "+ e.getMessage()); }
|
|
|
|
|
if ( fos != null ) {
|
|
|
|
|
try {
|
|
|
|
|
fos.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
Log.logWarning(
|
|
|
|
|
"FileUtils",
|
|
|
|
|
"cannot close FileOutputStream for " + dest + "! " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copies a part of a File to an OutputStream.
|
|
|
|
|
*
|
|
|
|
|
* @param source File
|
|
|
|
|
* @param dest OutputStream
|
|
|
|
|
* @param start Number of bytes to skip from the beginning of the File
|
|
|
|
@ -204,20 +232,33 @@ public final class FileUtils {
|
|
|
|
|
* @see #copy(File source, OutputStream dest)
|
|
|
|
|
* @see #copy(File source, File dest)
|
|
|
|
|
*/
|
|
|
|
|
public static void copyRange(final File source, final OutputStream dest, final int start) throws IOException {
|
|
|
|
|
public static void copyRange(final File source, final OutputStream dest, final int start)
|
|
|
|
|
throws IOException {
|
|
|
|
|
InputStream fis = null;
|
|
|
|
|
try {
|
|
|
|
|
fis = new FileInputStream(source);
|
|
|
|
|
final long skipped = fis.skip(start);
|
|
|
|
|
if (skipped != start) throw new IllegalStateException("Unable to skip '" + start + "' bytes. Only '" + skipped + "' bytes skipped.");
|
|
|
|
|
if ( skipped != start ) {
|
|
|
|
|
throw new IllegalStateException("Unable to skip '"
|
|
|
|
|
+ start
|
|
|
|
|
+ "' bytes. Only '"
|
|
|
|
|
+ skipped
|
|
|
|
|
+ "' bytes skipped.");
|
|
|
|
|
}
|
|
|
|
|
copy(fis, dest, -1);
|
|
|
|
|
} finally {
|
|
|
|
|
if (fis != null) try { fis.close(); } catch (final Exception e) {}
|
|
|
|
|
if ( fis != null ) {
|
|
|
|
|
try {
|
|
|
|
|
fis.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copies a File to an OutputStream.
|
|
|
|
|
*
|
|
|
|
|
* @param source File
|
|
|
|
|
* @param dest OutputStream
|
|
|
|
|
* @throws IOException
|
|
|
|
@ -232,12 +273,18 @@ public final class FileUtils {
|
|
|
|
|
fis = new FileInputStream(source);
|
|
|
|
|
copy(fis, dest, -1);
|
|
|
|
|
} finally {
|
|
|
|
|
if (fis != null) try { fis.close(); } catch (final Exception e) {}
|
|
|
|
|
if ( fis != null ) {
|
|
|
|
|
try {
|
|
|
|
|
fis.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copies a File to a File.
|
|
|
|
|
*
|
|
|
|
|
* @param source File
|
|
|
|
|
* @param dest File
|
|
|
|
|
* @param count the amount of bytes to copy
|
|
|
|
@ -255,8 +302,18 @@ public final class FileUtils {
|
|
|
|
|
fos = new FileOutputStream(dest);
|
|
|
|
|
copy(fis, fos, -1);
|
|
|
|
|
} finally {
|
|
|
|
|
if (fis != null) try {fis.close();} catch (final Exception e) {}
|
|
|
|
|
if (fos != null) try {fos.close();} catch (final Exception e) {}
|
|
|
|
|
if ( fis != null ) {
|
|
|
|
|
try {
|
|
|
|
|
fis.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( fos != null ) {
|
|
|
|
|
try {
|
|
|
|
|
fos.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -297,9 +354,16 @@ public final class FileUtils {
|
|
|
|
|
try {
|
|
|
|
|
fis = new FileInputStream(source);
|
|
|
|
|
int p = 0, c;
|
|
|
|
|
while ((c = fis.read(buffer, p, buffer.length - p)) > 0) p += c;
|
|
|
|
|
while ( (c = fis.read(buffer, p, buffer.length - p)) > 0 ) {
|
|
|
|
|
p += c;
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (fis != null) try { fis.close(); } catch (final Exception e) {}
|
|
|
|
|
if ( fis != null ) {
|
|
|
|
|
try {
|
|
|
|
|
fis.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fis = null;
|
|
|
|
|
}
|
|
|
|
|
return buffer;
|
|
|
|
@ -315,8 +379,18 @@ public final class FileUtils {
|
|
|
|
|
zipOut.close();
|
|
|
|
|
return byteOut.toByteArray();
|
|
|
|
|
} finally {
|
|
|
|
|
if (zipOut != null) try { zipOut.close(); } catch (final Exception e) {}
|
|
|
|
|
if (byteOut != null) try { byteOut.close(); } catch (final Exception e) {}
|
|
|
|
|
if ( zipOut != null ) {
|
|
|
|
|
try {
|
|
|
|
|
zipOut.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( byteOut != null ) {
|
|
|
|
|
try {
|
|
|
|
|
byteOut.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -326,7 +400,12 @@ public final class FileUtils {
|
|
|
|
|
fos = new FileOutputStream(dest);
|
|
|
|
|
writeAndGZip(source, fos);
|
|
|
|
|
} finally {
|
|
|
|
|
if (fos != null) try {fos.close();} catch (final Exception e) {}
|
|
|
|
|
if ( fos != null ) {
|
|
|
|
|
try {
|
|
|
|
|
fos.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -337,18 +416,26 @@ public final class FileUtils {
|
|
|
|
|
copy(source, zipOut);
|
|
|
|
|
zipOut.close();
|
|
|
|
|
} finally {
|
|
|
|
|
if (zipOut != null) try { zipOut.close(); } catch (final Exception e) {}
|
|
|
|
|
if ( zipOut != null ) {
|
|
|
|
|
try {
|
|
|
|
|
zipOut.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This function determines if a byte array is gzip compressed and uncompress it
|
|
|
|
|
*
|
|
|
|
|
* @param source properly gzip compressed byte array
|
|
|
|
|
* @return uncompressed byte array
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static byte[] uncompressGZipArray(byte[] source) throws IOException {
|
|
|
|
|
if (source == null) return null;
|
|
|
|
|
if ( source == null ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// support of gzipped data (requested by roland)
|
|
|
|
|
/* "Bitwise OR of signed byte value
|
|
|
|
@ -394,12 +481,19 @@ public final class FileUtils {
|
|
|
|
|
String line;
|
|
|
|
|
while ( (line = br.readLine()) != null ) {
|
|
|
|
|
line = line.trim();
|
|
|
|
|
if (line.length() > 0 && line.charAt(0) != '#') set.add(line.trim().toLowerCase());
|
|
|
|
|
if ( line.length() > 0 && line.charAt(0) != '#' ) {
|
|
|
|
|
set.add(line.trim().toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
br.close();
|
|
|
|
|
} catch ( final IOException e ) {
|
|
|
|
|
} finally {
|
|
|
|
|
if (br != null) try { br.close(); } catch (final Exception e) {}
|
|
|
|
|
if ( br != null ) {
|
|
|
|
|
try {
|
|
|
|
|
br.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
@ -415,16 +509,18 @@ public final class FileUtils {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void saveMap(final File file, final Map<String, String> props, final String comment) throws IOException {
|
|
|
|
|
public static void saveMap(final File file, final Map<String, String> props, final String comment) {
|
|
|
|
|
PrintWriter pw = null;
|
|
|
|
|
final File tf = new File(file.toString() + "." + (System.currentTimeMillis() % 1000));
|
|
|
|
|
try {
|
|
|
|
|
pw = new PrintWriter(tf, "UTF-8");
|
|
|
|
|
pw.println("# " + comment);
|
|
|
|
|
String key, value;
|
|
|
|
|
for ( final Map.Entry<String, String> entry : props.entrySet() ) {
|
|
|
|
|
key = entry.getKey();
|
|
|
|
|
if (key != null)
|
|
|
|
|
if ( key != null ) {
|
|
|
|
|
key = key.replace("\\", "\\\\").replace("\n", "\\n").replace("=", "\\=");
|
|
|
|
|
}
|
|
|
|
|
if ( entry.getValue() == null ) {
|
|
|
|
|
value = "";
|
|
|
|
|
} else {
|
|
|
|
@ -433,12 +529,27 @@ public final class FileUtils {
|
|
|
|
|
pw.println(key + "=" + value);
|
|
|
|
|
}
|
|
|
|
|
pw.println("# EOF");
|
|
|
|
|
} catch ( FileNotFoundException e ) {
|
|
|
|
|
Log.logWarning("FileUtils", e.getMessage(), e);
|
|
|
|
|
} catch ( UnsupportedEncodingException e ) {
|
|
|
|
|
Log.logWarning("FileUtils", e.getMessage(), e);
|
|
|
|
|
} finally {
|
|
|
|
|
if ( pw != null ) {
|
|
|
|
|
pw.close();
|
|
|
|
|
}
|
|
|
|
|
pw = null;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
forceMove(tf, file);
|
|
|
|
|
} catch ( IOException e ) {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Set<String> loadSet(final File file, final int chunksize, final boolean tree) throws IOException {
|
|
|
|
|
final Set<String> set = (tree) ? (Set<String>) new TreeSet<String>() : (Set<String>) new HashSet<String>();
|
|
|
|
|
public static Set<String> loadSet(final File file, final int chunksize, final boolean tree)
|
|
|
|
|
throws IOException {
|
|
|
|
|
final Set<String> set =
|
|
|
|
|
(tree) ? (Set<String>) new TreeSet<String>() : (Set<String>) new HashSet<String>();
|
|
|
|
|
final byte[] b = read(file);
|
|
|
|
|
for ( int i = 0; (i + chunksize) <= b.length; i++ ) {
|
|
|
|
|
set.add(UTF8.String(b, i, chunksize));
|
|
|
|
@ -446,8 +557,10 @@ public final class FileUtils {
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Set<String> loadSet(final File file, final String sep, final boolean tree) throws IOException {
|
|
|
|
|
final Set<String> set = (tree) ? (Set<String>) new TreeSet<String>() : (Set<String>) new HashSet<String>();
|
|
|
|
|
public static Set<String> loadSet(final File file, final String sep, final boolean tree)
|
|
|
|
|
throws IOException {
|
|
|
|
|
final Set<String> set =
|
|
|
|
|
(tree) ? (Set<String>) new TreeSet<String>() : (Set<String>) new HashSet<String>();
|
|
|
|
|
final byte[] b = read(file);
|
|
|
|
|
final StringTokenizer st = new StringTokenizer(UTF8.String(b), sep);
|
|
|
|
|
while ( st.hasMoreTokens() ) {
|
|
|
|
@ -456,7 +569,8 @@ public final class FileUtils {
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void saveSet(final File file, final String format, final Set<byte[]> set, final String sep) throws IOException {
|
|
|
|
|
public static void saveSet(final File file, final String format, final Set<byte[]> set, final String sep)
|
|
|
|
|
throws IOException {
|
|
|
|
|
final File tf = new File(file.toString() + ".prt" + (System.currentTimeMillis() % 1000));
|
|
|
|
|
OutputStream os = null;
|
|
|
|
|
if ( (format == null) || (format.equals("plain")) ) {
|
|
|
|
@ -466,21 +580,26 @@ public final class FileUtils {
|
|
|
|
|
} else if ( format.equals("zip") ) {
|
|
|
|
|
final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
|
|
|
|
|
String name = file.getName();
|
|
|
|
|
if (name.endsWith(".zip")) name = name.substring(0, name.length() - 4);
|
|
|
|
|
if ( name.endsWith(".zip") ) {
|
|
|
|
|
name = name.substring(0, name.length() - 4);
|
|
|
|
|
}
|
|
|
|
|
zos.putNextEntry(new ZipEntry(name + ".txt"));
|
|
|
|
|
os = zos;
|
|
|
|
|
}
|
|
|
|
|
if ( os != null ) {
|
|
|
|
|
for ( final byte[] b : set ) {
|
|
|
|
|
os.write(b);
|
|
|
|
|
if (sep != null) os.write(UTF8.getBytes(sep));
|
|
|
|
|
if ( sep != null ) {
|
|
|
|
|
os.write(UTF8.getBytes(sep));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
os.close();
|
|
|
|
|
}
|
|
|
|
|
forceMove(tf, file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void saveSet(final File file, final String format, final RowSet set, final String sep) throws IOException {
|
|
|
|
|
public static void saveSet(final File file, final String format, final RowSet set, final String sep)
|
|
|
|
|
throws IOException {
|
|
|
|
|
final File tf = new File(file.toString() + ".prt" + (System.currentTimeMillis() % 1000));
|
|
|
|
|
OutputStream os = null;
|
|
|
|
|
if ( (format == null) || (format.equals("plain")) ) {
|
|
|
|
@ -490,7 +609,9 @@ public final class FileUtils {
|
|
|
|
|
} else if ( format.equals("zip") ) {
|
|
|
|
|
final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
|
|
|
|
|
String name = file.getName();
|
|
|
|
|
if (name.endsWith(".zip")) name = name.substring(0, name.length() - 4);
|
|
|
|
|
if ( name.endsWith(".zip") ) {
|
|
|
|
|
name = name.substring(0, name.length() - 4);
|
|
|
|
|
}
|
|
|
|
|
zos.putNextEntry(new ZipEntry(name + ".txt"));
|
|
|
|
|
os = zos;
|
|
|
|
|
}
|
|
|
|
@ -500,7 +621,9 @@ public final class FileUtils {
|
|
|
|
|
os.write(i.next().getPrimaryKeyBytes());
|
|
|
|
|
}
|
|
|
|
|
while ( i.hasNext() ) {
|
|
|
|
|
if (sep != null) os.write(UTF8.getBytes(sep));
|
|
|
|
|
if ( sep != null ) {
|
|
|
|
|
os.write(UTF8.getBytes(sep));
|
|
|
|
|
}
|
|
|
|
|
os.write(i.next().getPrimaryKeyBytes());
|
|
|
|
|
}
|
|
|
|
|
os.close();
|
|
|
|
@ -516,6 +639,7 @@ public final class FileUtils {
|
|
|
|
|
private final static Pattern escaped_equal = Pattern.compile("\\=", Pattern.LITERAL);
|
|
|
|
|
private final static Pattern escaped_newline = Pattern.compile("\\n", Pattern.LITERAL);
|
|
|
|
|
private final static Pattern escaped_backslash = Pattern.compile("\\", Pattern.LITERAL);
|
|
|
|
|
|
|
|
|
|
//private final static Pattern escaped_backslashbackslash = Pattern.compile("\\\\", Pattern.LITERAL);
|
|
|
|
|
|
|
|
|
|
public static ConcurrentHashMap<String, String> table(final Iterator<String> li) {
|
|
|
|
@ -524,7 +648,9 @@ public final class FileUtils {
|
|
|
|
|
while ( li.hasNext() ) {
|
|
|
|
|
int pos = 0;
|
|
|
|
|
line = li.next().trim();
|
|
|
|
|
if (line.length() > 0 && line.charAt(0) == '#') continue; // exclude comments
|
|
|
|
|
if ( line.length() > 0 && line.charAt(0) == '#' ) {
|
|
|
|
|
continue; // exclude comments
|
|
|
|
|
}
|
|
|
|
|
do {
|
|
|
|
|
// search for unescaped =
|
|
|
|
|
pos = line.indexOf('=', pos + 1);
|
|
|
|
@ -547,15 +673,18 @@ public final class FileUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Iterator<String> strings(final byte[] a) {
|
|
|
|
|
if (a == null) return new ArrayList<String>().iterator();
|
|
|
|
|
if ( a == null ) {
|
|
|
|
|
return new ArrayList<String>().iterator();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
return new StringsIterator(new BufferedReader(new InputStreamReader(new ByteArrayInputStream(a), "UTF-8")));
|
|
|
|
|
return new StringsIterator(new BufferedReader(new InputStreamReader(
|
|
|
|
|
new ByteArrayInputStream(a),
|
|
|
|
|
"UTF-8")));
|
|
|
|
|
} catch ( final UnsupportedEncodingException e ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read lines of a file into an ArrayList.
|
|
|
|
|
*
|
|
|
|
@ -576,13 +705,16 @@ public final class FileUtils {
|
|
|
|
|
} catch ( final IOException e ) {
|
|
|
|
|
// list is empty
|
|
|
|
|
} finally {
|
|
|
|
|
if (br!=null) try { br.close(); } catch (final Exception e) {}
|
|
|
|
|
if ( br != null ) {
|
|
|
|
|
try {
|
|
|
|
|
br.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write a String to a file (used for string representation of lists).
|
|
|
|
|
*
|
|
|
|
@ -600,7 +732,12 @@ public final class FileUtils {
|
|
|
|
|
} catch ( final IOException e ) {
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
if (bw!=null) try { bw.close(); } catch (final Exception e) {}
|
|
|
|
|
if ( bw != null ) {
|
|
|
|
|
try {
|
|
|
|
|
bw.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -625,7 +762,9 @@ public final class FileUtils {
|
|
|
|
|
// Read the List
|
|
|
|
|
String line = "";
|
|
|
|
|
while ( (line = br.readLine()) != null ) {
|
|
|
|
|
if (line.length() == 0) continue;
|
|
|
|
|
if ( line.length() == 0 ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ( line.charAt(0) != '#' || withcomments ) {
|
|
|
|
|
//temp += line + serverCore.CRLF_STRING;
|
|
|
|
|
temp.append(line).append(CR).append(LF);
|
|
|
|
@ -634,7 +773,12 @@ public final class FileUtils {
|
|
|
|
|
br.close();
|
|
|
|
|
} catch ( final IOException e ) {
|
|
|
|
|
} finally {
|
|
|
|
|
if (br!=null) try { br.close(); } catch (final Exception e) {}
|
|
|
|
|
if ( br != null ) {
|
|
|
|
|
try {
|
|
|
|
|
br.close();
|
|
|
|
|
} catch ( final Exception e ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new String(temp);
|
|
|
|
@ -642,8 +786,8 @@ public final class FileUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read content of a directory into a String array of file names.
|
|
|
|
|
* @param dirname The directory to get the file listing from. If it doesn't exist yet,
|
|
|
|
|
* it will be created.
|
|
|
|
|
*
|
|
|
|
|
* @param dirname The directory to get the file listing from. If it doesn't exist yet, it will be created.
|
|
|
|
|
* @return array of file names
|
|
|
|
|
*/
|
|
|
|
|
public static List<String> getDirListing(final String dirname) {
|
|
|
|
@ -652,11 +796,10 @@ public final class FileUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read content of a directory into a String array of file names.
|
|
|
|
|
* @param dirname The directory to get the file listing from. If it doesn't exist yet,
|
|
|
|
|
* it will be created.
|
|
|
|
|
* @param filter String which contains a regular expression which has to be matched by
|
|
|
|
|
* file names in order to appear in returned array. All file names will be returned if
|
|
|
|
|
* filter is null.
|
|
|
|
|
*
|
|
|
|
|
* @param dirname The directory to get the file listing from. If it doesn't exist yet, it will be created.
|
|
|
|
|
* @param filter String which contains a regular expression which has to be matched by file names in order
|
|
|
|
|
* to appear in returned array. All file names will be returned if filter is null.
|
|
|
|
|
* @return array of file names
|
|
|
|
|
*/
|
|
|
|
|
public static List<String> getDirListing(final String dirname, final String filter) {
|
|
|
|
@ -666,8 +809,7 @@ public final class FileUtils {
|
|
|
|
|
/**
|
|
|
|
|
* Read content of a directory into a String array of file names.
|
|
|
|
|
*
|
|
|
|
|
* @param dir The directory to get the file listing from. If it doesn't exist yet,
|
|
|
|
|
* it will be created.
|
|
|
|
|
* @param dir The directory to get the file listing from. If it doesn't exist yet, it will be created.
|
|
|
|
|
* @return array of file names
|
|
|
|
|
*/
|
|
|
|
|
public static List<String> getDirListing(final File dir) {
|
|
|
|
@ -676,11 +818,10 @@ public final class FileUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read content of a directory into a String array of file names.
|
|
|
|
|
* @param dir The directory to get the file listing from. If it doesn't exist yet,
|
|
|
|
|
* it will be created.
|
|
|
|
|
* @param filter String which contains a regular expression which has to be matched by
|
|
|
|
|
* file names in order to appear in returned array. All file names will be returned if
|
|
|
|
|
* filter is null.
|
|
|
|
|
*
|
|
|
|
|
* @param dir The directory to get the file listing from. If it doesn't exist yet, it will be created.
|
|
|
|
|
* @param filter String which contains a regular expression which has to be matched by file names in order
|
|
|
|
|
* to appear in returned array. All file names will be returned if filter is null.
|
|
|
|
|
* @return array of file names
|
|
|
|
|
*/
|
|
|
|
|
public static List<String> getDirListing(final File dir, final String filter) {
|
|
|
|
@ -707,17 +848,20 @@ public final class FileUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a List of all dirs and subdirs as File Objects
|
|
|
|
|
*
|
|
|
|
|
* Warning: untested
|
|
|
|
|
* Returns a List of all dirs and subdirs as File Objects Warning: untested
|
|
|
|
|
*/
|
|
|
|
|
public static ArrayList<File> getDirsRecursive(final File dir, final String notdir, final boolean excludeDotfiles){
|
|
|
|
|
public static ArrayList<File> getDirsRecursive(
|
|
|
|
|
final File dir,
|
|
|
|
|
final String notdir,
|
|
|
|
|
final boolean excludeDotfiles) {
|
|
|
|
|
final File[] dirList = dir.listFiles();
|
|
|
|
|
final ArrayList<File> resultList = new ArrayList<File>();
|
|
|
|
|
ArrayList<File> recursive;
|
|
|
|
|
Iterator<File> iter;
|
|
|
|
|
for ( int i = 0; i < dirList.length; i++ ) {
|
|
|
|
|
if (dirList[i].isDirectory() && (!excludeDotfiles || !dirList[i].getName().startsWith(".")) && !dirList[i].getName().equals(notdir)) {
|
|
|
|
|
if ( dirList[i].isDirectory()
|
|
|
|
|
&& (!excludeDotfiles || !dirList[i].getName().startsWith("."))
|
|
|
|
|
&& !dirList[i].getName().equals(notdir) ) {
|
|
|
|
|
resultList.add(dirList[i]);
|
|
|
|
|
recursive = getDirsRecursive(dirList[i], notdir, excludeDotfiles);
|
|
|
|
|
iter = recursive.iterator();
|
|
|
|
@ -729,8 +873,6 @@ public final class FileUtils {
|
|
|
|
|
return resultList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write elements of an Array of Strings to a file (one element per line).
|
|
|
|
|
*
|
|
|
|
@ -746,24 +888,31 @@ public final class FileUtils {
|
|
|
|
|
return FileUtils.writeList(listFile, new String(out)); //(File, String)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class StringsIterator implements Iterator<String> {
|
|
|
|
|
public static class StringsIterator implements Iterator<String>
|
|
|
|
|
{
|
|
|
|
|
private final BufferedReader reader;
|
|
|
|
|
private String nextLine;
|
|
|
|
|
|
|
|
|
|
public StringsIterator(final BufferedReader reader) {
|
|
|
|
|
this.reader = reader;
|
|
|
|
|
this.nextLine = null;
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
|
return this.nextLine != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String next() {
|
|
|
|
|
final String line = this.nextLine;
|
|
|
|
|
try {
|
|
|
|
|
while ( (this.nextLine = this.reader.readLine()) != null ) {
|
|
|
|
|
this.nextLine = this.nextLine.trim();
|
|
|
|
|
if (this.nextLine.length() > 0) break;
|
|
|
|
|
if ( this.nextLine.length() > 0 ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch ( final IOException e ) {
|
|
|
|
|
this.nextLine = null;
|
|
|
|
@ -774,6 +923,7 @@ public final class FileUtils {
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void remove() {
|
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
|
}
|
|
|
|
@ -795,27 +945,39 @@ public final class FileUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Moves all files from a directory to another.
|
|
|
|
|
*
|
|
|
|
|
* @param from_dir Directory which contents will be moved.
|
|
|
|
|
* @param to_dir Directory to move into. It must exist already.
|
|
|
|
|
*/
|
|
|
|
|
public static void moveAll(final File from_dir, final File to_dir) {
|
|
|
|
|
if (!(from_dir.isDirectory())) return;
|
|
|
|
|
if (!(to_dir.isDirectory())) return;
|
|
|
|
|
if ( !(from_dir.isDirectory()) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( !(to_dir.isDirectory()) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
final String[] list = from_dir.list();
|
|
|
|
|
for ( int i = 0; i < list.length; i++ ) {
|
|
|
|
|
if(!new File(from_dir, list[i]).renameTo(new File(to_dir, list[i])))
|
|
|
|
|
Log.logWarning("serverFileUtils", "moveAll(): could not move from "+ from_dir + list[i] +" to "+ to_dir + list[i]);
|
|
|
|
|
if ( !new File(from_dir, list[i]).renameTo(new File(to_dir, list[i])) ) {
|
|
|
|
|
Log.logWarning("serverFileUtils", "moveAll(): could not move from "
|
|
|
|
|
+ from_dir
|
|
|
|
|
+ list[i]
|
|
|
|
|
+ " to "
|
|
|
|
|
+ to_dir
|
|
|
|
|
+ list[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class dirlistComparator implements Comparator<File>, Serializable {
|
|
|
|
|
public static class dirlistComparator implements Comparator<File>, Serializable
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generated serial
|
|
|
|
|
*/
|
|
|
|
|
private static final long serialVersionUID = -5196490300039230135L;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int compare(final File file1, final File file2) {
|
|
|
|
|
if ( file1.isDirectory() && !file2.isDirectory() ) {
|
|
|
|
|
return -1;
|
|
|
|
@ -842,18 +1004,23 @@ public final class FileUtils {
|
|
|
|
|
final String fileExt = (idx > -1) ? fileName.substring(idx + 1) : "";
|
|
|
|
|
|
|
|
|
|
// create the temp file
|
|
|
|
|
final File tempFile = File.createTempFile(parserClassName + "_" + ((idx>-1)?fileName.substring(0,idx):fileName), (fileExt.length()>0)?"."+fileExt:fileExt);
|
|
|
|
|
final File tempFile =
|
|
|
|
|
File.createTempFile(
|
|
|
|
|
parserClassName + "_" + ((idx > -1) ? fileName.substring(0, idx) : fileName),
|
|
|
|
|
(fileExt.length() > 0) ? "." + fileExt : fileExt);
|
|
|
|
|
return tempFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* copies the input stream to one output stream (byte per byte)
|
|
|
|
|
*
|
|
|
|
|
* @param in
|
|
|
|
|
* @param out
|
|
|
|
|
* @return number of copies bytes
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static int copyToStream(final BufferedInputStream in, final BufferedOutputStream out) throws IOException {
|
|
|
|
|
public static int copyToStream(final BufferedInputStream in, final BufferedOutputStream out)
|
|
|
|
|
throws IOException {
|
|
|
|
|
int count = 0;
|
|
|
|
|
// copy bytes
|
|
|
|
|
int b;
|
|
|
|
@ -867,13 +1034,17 @@ public final class FileUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* copies the input stream to both output streams (byte per byte)
|
|
|
|
|
*
|
|
|
|
|
* @param in
|
|
|
|
|
* @param out0
|
|
|
|
|
* @param out1
|
|
|
|
|
* @return number of copies bytes
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static int copyToStreams(final BufferedInputStream in, final BufferedOutputStream out0, final BufferedOutputStream out1) throws IOException {
|
|
|
|
|
public static int copyToStreams(
|
|
|
|
|
final BufferedInputStream in,
|
|
|
|
|
final BufferedOutputStream out0,
|
|
|
|
|
final BufferedOutputStream out1) throws IOException {
|
|
|
|
|
assert out0 != null;
|
|
|
|
|
assert out1 != null;
|
|
|
|
|
|
|
|
|
@ -892,13 +1063,17 @@ public final class FileUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* copies the input stream to all writers (byte per byte)
|
|
|
|
|
*
|
|
|
|
|
* @param data
|
|
|
|
|
* @param writer
|
|
|
|
|
* @param charSet
|
|
|
|
|
* @return
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
public static int copyToWriter(final BufferedInputStream data, final BufferedWriter writer, final Charset charSet) throws IOException {
|
|
|
|
|
public static int copyToWriter(
|
|
|
|
|
final BufferedInputStream data,
|
|
|
|
|
final BufferedWriter writer,
|
|
|
|
|
final Charset charSet) throws IOException {
|
|
|
|
|
// the docs say: "For top efficiency, consider wrapping an InputStreamReader within a BufferedReader."
|
|
|
|
|
final Reader sourceReader = new InputStreamReader(data, charSet);
|
|
|
|
|
|
|
|
|
@ -913,7 +1088,11 @@ public final class FileUtils {
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int copyToWriters(final BufferedInputStream data, final BufferedWriter writer0, final BufferedWriter writer1, final Charset charSet) throws IOException {
|
|
|
|
|
public static int copyToWriters(
|
|
|
|
|
final BufferedInputStream data,
|
|
|
|
|
final BufferedWriter writer0,
|
|
|
|
|
final BufferedWriter writer1,
|
|
|
|
|
final Charset charSet) throws IOException {
|
|
|
|
|
// the docs say: "For top efficiency, consider wrapping an InputStreamReader within a BufferedReader."
|
|
|
|
|
assert writer0 != null;
|
|
|
|
|
assert writer1 != null;
|
|
|
|
@ -933,30 +1112,42 @@ public final class FileUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* delete files and directories
|
|
|
|
|
* if a directory is not empty, delete also everything inside
|
|
|
|
|
* because deletion sometimes fails on windows, there is also a windows exec included
|
|
|
|
|
* delete files and directories if a directory is not empty, delete also everything inside because
|
|
|
|
|
* deletion sometimes fails on windows, there is also a windows exec included
|
|
|
|
|
*
|
|
|
|
|
* @param path
|
|
|
|
|
*/
|
|
|
|
|
public static void deletedelete(final File path) {
|
|
|
|
|
if (path == null || !path.exists()) return;
|
|
|
|
|
if ( path == null || !path.exists() ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// empty the directory first
|
|
|
|
|
if ( path.isDirectory() ) {
|
|
|
|
|
final String[] list = path.list();
|
|
|
|
|
if ( list != null ) {
|
|
|
|
|
for (final String s: list) deletedelete(new File(path, s));
|
|
|
|
|
for ( final String s : list ) {
|
|
|
|
|
deletedelete(new File(path, s));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int c = 0;
|
|
|
|
|
while ( c++ < 20 ) {
|
|
|
|
|
if (!path.exists()) break;
|
|
|
|
|
if (path.delete()) break;
|
|
|
|
|
if ( !path.exists() ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if ( path.delete() ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// some OS may be slow when giving up file pointer
|
|
|
|
|
//System.runFinalization();
|
|
|
|
|
//System.gc();
|
|
|
|
|
try { Thread.sleep(200); } catch (final InterruptedException e) { break; }
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(200);
|
|
|
|
|
} catch ( final InterruptedException e ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( path.exists() ) {
|
|
|
|
|
path.deleteOnExit();
|
|
|
|
@ -981,7 +1172,9 @@ public final class FileUtils {
|
|
|
|
|
Log.logException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (path.exists()) Log.logSevere("FileUtils", "cannot delete file " + p);
|
|
|
|
|
if ( path.exists() ) {
|
|
|
|
|
Log.logSevere("FileUtils", "cannot delete file " + p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|