|
|
|
@ -181,7 +181,7 @@ public final class FileUtils {
|
|
|
|
|
* @see #copy(File source, File dest)
|
|
|
|
|
*/
|
|
|
|
|
public static void copy(final InputStream source, final File dest, final long count) throws IOException {
|
|
|
|
|
String path = dest.getParent();
|
|
|
|
|
final String path = dest.getParent();
|
|
|
|
|
if (path != null && path.length() > 0) new File(path).mkdirs();
|
|
|
|
|
FileOutputStream fos = null;
|
|
|
|
|
try {
|
|
|
|
@ -274,11 +274,11 @@ public final class FileUtils {
|
|
|
|
|
|
|
|
|
|
public static byte[] read(final InputStream source, final int count) throws IOException {
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
byte[] b = new byte[count];
|
|
|
|
|
int c = source.read(b, 0, count);
|
|
|
|
|
final byte[] b = new byte[count];
|
|
|
|
|
final int c = source.read(b, 0, count);
|
|
|
|
|
assert c == count: "count = " + count + ", c = " + c;
|
|
|
|
|
if (c != count) {
|
|
|
|
|
byte[] bb = new byte[c];
|
|
|
|
|
final byte[] bb = new byte[c];
|
|
|
|
|
System.arraycopy(b, 0, bb, 0, c);
|
|
|
|
|
return bb;
|
|
|
|
|
}
|
|
|
|
@ -470,8 +470,8 @@ public final class FileUtils {
|
|
|
|
|
os = zos;
|
|
|
|
|
}
|
|
|
|
|
if(os != null) {
|
|
|
|
|
for (final Iterator<byte[]> i = set.iterator(); i.hasNext(); ) {
|
|
|
|
|
os.write(i.next());
|
|
|
|
|
for (final byte[] b : set) {
|
|
|
|
|
os.write(b);
|
|
|
|
|
if (sep != null) os.write(UTF8.getBytes(sep));
|
|
|
|
|
}
|
|
|
|
|
os.close();
|
|
|
|
@ -495,23 +495,20 @@ public final class FileUtils {
|
|
|
|
|
}
|
|
|
|
|
if (os != null) {
|
|
|
|
|
final Iterator<Row.Entry> i = set.iterator();
|
|
|
|
|
String key;
|
|
|
|
|
if (i.hasNext()) {
|
|
|
|
|
key = UTF8.String(i.next().getPrimaryKeyBytes());
|
|
|
|
|
os.write(UTF8.getBytes(key));
|
|
|
|
|
os.write(i.next().getPrimaryKeyBytes());
|
|
|
|
|
}
|
|
|
|
|
while (i.hasNext()) {
|
|
|
|
|
key = UTF8.String(i.next().getPrimaryKeyBytes());
|
|
|
|
|
if (sep != null) os.write(UTF8.getBytes(sep));
|
|
|
|
|
os.write(UTF8.getBytes(key));
|
|
|
|
|
os.write(i.next().getPrimaryKeyBytes());
|
|
|
|
|
}
|
|
|
|
|
os.close();
|
|
|
|
|
}
|
|
|
|
|
forceMove(tf, file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ConcurrentHashMap<String, String> table(Reader r) {
|
|
|
|
|
BufferedReader br = new BufferedReader(r);
|
|
|
|
|
public static ConcurrentHashMap<String, String> table(final Reader r) {
|
|
|
|
|
final BufferedReader br = new BufferedReader(r);
|
|
|
|
|
return table(new StringsIterator(br));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -519,7 +516,7 @@ public final class FileUtils {
|
|
|
|
|
//private final static Pattern escaped_equal = Pattern.compile("\\=");
|
|
|
|
|
//private final static Pattern escaped_newline = Pattern.compile("\\n");
|
|
|
|
|
//private final static Pattern escaped_backslash = Pattern.compile("\\");
|
|
|
|
|
public static ConcurrentHashMap<String, String> table(Iterator<String> li) {
|
|
|
|
|
public static ConcurrentHashMap<String, String> table(final Iterator<String> li) {
|
|
|
|
|
String line;
|
|
|
|
|
final ConcurrentHashMap<String, String> props = new ConcurrentHashMap<String, String>();
|
|
|
|
|
while (li.hasNext()) {
|
|
|
|
@ -532,8 +529,8 @@ public final class FileUtils {
|
|
|
|
|
} while ( pos > 0 && line.charAt(pos-1) == '\\');
|
|
|
|
|
if (pos > 0) {
|
|
|
|
|
//String key = escaped_equal.matcher(line.substring(0, pos).trim()).replaceAll("=");
|
|
|
|
|
String key = line.substring(0, pos).trim().replace("\\=", "=").replace("\\n", "\n").replace("\\", "\\");
|
|
|
|
|
String value = line.substring(pos + 1).trim().replace("\\n", "\n").replace("\\\\", "\\");
|
|
|
|
|
final String key = line.substring(0, pos).trim().replace("\\=", "=").replace("\\n", "\n").replace("\\", "\\");
|
|
|
|
|
final String value = line.substring(pos + 1).trim().replace("\\n", "\n").replace("\\\\", "\\");
|
|
|
|
|
props.put(key, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -544,11 +541,11 @@ public final class FileUtils {
|
|
|
|
|
return table(strings(a));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Iterator<String> strings(byte[] a) {
|
|
|
|
|
public static Iterator<String> strings(final byte[] a) {
|
|
|
|
|
if (a == null) return new ArrayList<String>().iterator();
|
|
|
|
|
try {
|
|
|
|
|
return new StringsIterator(new BufferedReader(new InputStreamReader(new ByteArrayInputStream(a), "UTF-8")));
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
} catch (final UnsupportedEncodingException e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -682,7 +679,7 @@ public final class FileUtils {
|
|
|
|
|
* @return array of file names
|
|
|
|
|
*/
|
|
|
|
|
public static List<String> getDirListing(final File dir, final String filter){
|
|
|
|
|
List<String> ret = new LinkedList<String>();
|
|
|
|
|
final List<String> ret = new LinkedList<String>();
|
|
|
|
|
File[] fileList;
|
|
|
|
|
if (dir != null ) {
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
@ -738,8 +735,8 @@ public final class FileUtils {
|
|
|
|
|
*/
|
|
|
|
|
public static boolean writeList(final File listFile, final String[] list){
|
|
|
|
|
final StringBuilder out = new StringBuilder(list.length * 40 + 1);
|
|
|
|
|
for(int i=0;i < list.length; i++){
|
|
|
|
|
out.append(list[i]).append(CR).append(LF);
|
|
|
|
|
for (final String element : list) {
|
|
|
|
|
out.append(element).append(CR).append(LF);
|
|
|
|
|
}
|
|
|
|
|
return FileUtils.writeList(listFile, new String(out)); //(File, String)
|
|
|
|
|
}
|
|
|
|
@ -753,21 +750,21 @@ public final class FileUtils {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
|
return nextLine != null;
|
|
|
|
|
return this.nextLine != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String next() {
|
|
|
|
|
String line = nextLine;
|
|
|
|
|
final String line = this.nextLine;
|
|
|
|
|
try {
|
|
|
|
|
while ((nextLine = reader.readLine()) != null) {
|
|
|
|
|
nextLine = nextLine.trim();
|
|
|
|
|
if (nextLine.length() > 0) break;
|
|
|
|
|
while ((this.nextLine = this.reader.readLine()) != null) {
|
|
|
|
|
this.nextLine = this.nextLine.trim();
|
|
|
|
|
if (this.nextLine.length() > 0) break;
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
nextLine = null;
|
|
|
|
|
} catch (OutOfMemoryError e) {
|
|
|
|
|
} catch (final IOException e) {
|
|
|
|
|
this.nextLine = null;
|
|
|
|
|
} catch (final OutOfMemoryError e) {
|
|
|
|
|
Log.logException(e);
|
|
|
|
|
nextLine = null;
|
|
|
|
|
this.nextLine = null;
|
|
|
|
|
}
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
@ -825,7 +822,7 @@ public final class FileUtils {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static final File createTempFile(Class<?> classObj, final String name) throws IOException {
|
|
|
|
|
public static final File createTempFile(final Class<?> classObj, final String name) throws IOException {
|
|
|
|
|
String parserClassName = classObj.getName();
|
|
|
|
|
int idx = parserClassName.lastIndexOf('.');
|
|
|
|
|
if (idx != -1) {
|
|
|
|
@ -943,7 +940,7 @@ public final class FileUtils {
|
|
|
|
|
if (path.isDirectory()) {
|
|
|
|
|
final String[] list = path.list();
|
|
|
|
|
if (list != null) {
|
|
|
|
|
for (String s: list) deletedelete(new File(path, s));
|
|
|
|
|
for (final String s: list) deletedelete(new File(path, s));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -954,28 +951,28 @@ public final class FileUtils {
|
|
|
|
|
// some OS may be slow when giving up file pointer
|
|
|
|
|
//System.runFinalization();
|
|
|
|
|
//System.gc();
|
|
|
|
|
try { Thread.sleep(200); } catch (InterruptedException e) { break; }
|
|
|
|
|
try { Thread.sleep(200); } catch (final InterruptedException e) { break; }
|
|
|
|
|
}
|
|
|
|
|
if (path.exists()) {
|
|
|
|
|
path.deleteOnExit();
|
|
|
|
|
String p = "";
|
|
|
|
|
try {
|
|
|
|
|
p = path.getCanonicalPath();
|
|
|
|
|
} catch (IOException e1) {
|
|
|
|
|
} catch (final IOException e1) {
|
|
|
|
|
Log.logException(e1);
|
|
|
|
|
}
|
|
|
|
|
if (System.getProperties().getProperty("os.name","").toLowerCase().startsWith("windows")) {
|
|
|
|
|
// deleting files on windows sometimes does not work with java
|
|
|
|
|
try {
|
|
|
|
|
String command = "cmd /C del /F /Q \"" + p + "\"";
|
|
|
|
|
Process r = Runtime.getRuntime().exec(command);
|
|
|
|
|
final String command = "cmd /C del /F /Q \"" + p + "\"";
|
|
|
|
|
final Process r = Runtime.getRuntime().exec(command);
|
|
|
|
|
if (r == null) {
|
|
|
|
|
Log.logSevere("FileUtils", "cannot execute command: " + command);
|
|
|
|
|
} else {
|
|
|
|
|
byte[] response = read(r.getInputStream());
|
|
|
|
|
final byte[] response = read(r.getInputStream());
|
|
|
|
|
Log.logInfo("FileUtils", "deletedelete: " + UTF8.String(response));
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
} catch (final IOException e) {
|
|
|
|
|
Log.logException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|