|
|
@ -42,20 +42,10 @@ public class FileUtils {
|
|
|
|
* @since IO 2.1
|
|
|
|
* @since IO 2.1
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException {
|
|
|
|
public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException {
|
|
|
|
OutputStream out = null;
|
|
|
|
try (OutputStream out = openOutputStream(file, append)) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
out = openOutputStream(file, append);
|
|
|
|
|
|
|
|
out.write(data);
|
|
|
|
out.write(data);
|
|
|
|
out.close(); // don't swallow close Exception if copy completes normally
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (out != null) {
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
|
|
|
|
// ignore
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -111,10 +101,8 @@ public class FileUtils {
|
|
|
|
* @param file the file to save the history
|
|
|
|
* @param file the file to save the history
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void saveCommandHistory(List<int[]> history, File file) {
|
|
|
|
public static void saveCommandHistory(List<int[]> history, File file) {
|
|
|
|
OutputStream out = null;
|
|
|
|
try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) {
|
|
|
|
try {
|
|
|
|
for (int[] command : history) {
|
|
|
|
out = new BufferedOutputStream(openOutputStream(file, false));
|
|
|
|
|
|
|
|
for (int[] command: history) {
|
|
|
|
|
|
|
|
String commandStr = Helper.fromCodePoints(command);
|
|
|
|
String commandStr = Helper.fromCodePoints(command);
|
|
|
|
if (isAuthCommand(commandStr)) {
|
|
|
|
if (isAuthCommand(commandStr)) {
|
|
|
|
command = AUTH_CODEPOINTS;
|
|
|
|
command = AUTH_CODEPOINTS;
|
|
|
@ -127,20 +115,13 @@ public class FileUtils {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
// ignore
|
|
|
|
// ignore
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (out != null) {
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
|
|
|
|
// ignore
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static List<int[]> loadCommandHistory(File file) {
|
|
|
|
public static List<int[]> loadCommandHistory(File file) {
|
|
|
|
BufferedReader br = null;
|
|
|
|
BufferedReader br = null;
|
|
|
|
List<int[]> history = new ArrayList<int[]>();
|
|
|
|
List<int[]> history = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
|
|
|
|
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
|
|
|
|
String line;
|
|
|
|
String line;
|
|
|
@ -167,10 +148,8 @@ public class FileUtils {
|
|
|
|
* @param file the file to save the history
|
|
|
|
* @param file the file to save the history
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void saveCommandHistoryString(List<String> history, File file) {
|
|
|
|
public static void saveCommandHistoryString(List<String> history, File file) {
|
|
|
|
OutputStream out = null;
|
|
|
|
try (OutputStream out = new BufferedOutputStream(openOutputStream(file, false))) {
|
|
|
|
try {
|
|
|
|
for (String command : history) {
|
|
|
|
out = new BufferedOutputStream(openOutputStream(file, false));
|
|
|
|
|
|
|
|
for (String command: history) {
|
|
|
|
|
|
|
|
if (!StringUtils.isBlank(command)) {
|
|
|
|
if (!StringUtils.isBlank(command)) {
|
|
|
|
if (isAuthCommand(command)) {
|
|
|
|
if (isAuthCommand(command)) {
|
|
|
|
command = ArthasConstants.AUTH;
|
|
|
|
command = ArthasConstants.AUTH;
|
|
|
@ -181,20 +160,13 @@ public class FileUtils {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
// ignore
|
|
|
|
// ignore
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (out != null) {
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
|
|
|
|
// ignore
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static List<String> loadCommandHistoryString(File file) {
|
|
|
|
public static List<String> loadCommandHistoryString(File file) {
|
|
|
|
BufferedReader br = null;
|
|
|
|
BufferedReader br = null;
|
|
|
|
List<String> history = new ArrayList<String>();
|
|
|
|
List<String> history = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
|
|
|
|
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
|
|
|
|
String line;
|
|
|
|
String line;
|
|
|
@ -218,8 +190,7 @@ public class FileUtils {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String readFileToString(File file, Charset encoding) throws IOException {
|
|
|
|
public static String readFileToString(File file, Charset encoding) throws IOException {
|
|
|
|
FileInputStream stream = new FileInputStream(file);
|
|
|
|
try (FileInputStream stream = new FileInputStream(file)) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
Reader reader = new BufferedReader(new InputStreamReader(stream, encoding));
|
|
|
|
Reader reader = new BufferedReader(new InputStreamReader(stream, encoding));
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
char[] buffer = new char[8192];
|
|
|
|
char[] buffer = new char[8192];
|
|
|
@ -228,8 +199,6 @@ public class FileUtils {
|
|
|
|
builder.append(buffer, 0, read);
|
|
|
|
builder.append(buffer, 0, read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
return builder.toString();
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
stream.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|