mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
Changed!: Rename FileUtils readStringFromFile() to readTextFromFile() and writeStringToFile() to writeTextToFile()
This commit is contained in:
@@ -69,7 +69,7 @@ public class CrashUtils {
|
|||||||
StringBuilder reportStringBuilder = new StringBuilder();
|
StringBuilder reportStringBuilder = new StringBuilder();
|
||||||
|
|
||||||
// Read report string from crash log file
|
// Read report string from crash log file
|
||||||
error = FileUtils.readStringFromFile("crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, Charset.defaultCharset(), reportStringBuilder, false);
|
error = FileUtils.readTextFromFile("crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, Charset.defaultCharset(), reportStringBuilder, false);
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
Logger.logErrorExtended(logTag, error.toString());
|
Logger.logErrorExtended(logTag, error.toString());
|
||||||
return;
|
return;
|
||||||
|
@@ -70,7 +70,7 @@ public class CrashHandler implements Thread.UncaughtExceptionHandler {
|
|||||||
Logger.logError(reportString.toString());
|
Logger.logError(reportString.toString());
|
||||||
|
|
||||||
// Write report string to crash log file
|
// Write report string to crash log file
|
||||||
Error error = FileUtils.writeStringToFile("crash log", crashHandlerClient.getCrashLogFilePath(context),
|
Error error = FileUtils.writeTextToFile("crash log", crashHandlerClient.getCrashLogFilePath(context),
|
||||||
Charset.defaultCharset(), reportString.toString(), false);
|
Charset.defaultCharset(), reportString.toString(), false);
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
Logger.logErrorExtended(LOG_TAG, error.toString());
|
Logger.logErrorExtended(LOG_TAG, error.toString());
|
||||||
|
@@ -1375,22 +1375,22 @@ public class FileUtils {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a {@link String} from file at path with a specific {@link Charset} into {@code dataString}.
|
* Read a text {@link String} from file at path with a specific {@link Charset} into {@code dataString}.
|
||||||
*
|
*
|
||||||
* @param label The optional label for file to read. This can optionally be {@code null}.
|
* @param label The optional label for file to read. This can optionally be {@code null}.
|
||||||
* @param filePath The {@code path} for file to read.
|
* @param filePath The {@code path} for file to read.
|
||||||
* @param charset The {@link Charset} of the file. If this is {@code null},
|
* @param charset The {@link Charset} of the file. If this is {@code null},
|
||||||
* * then default {@link Charset} will be used.
|
* then default {@link Charset} will be used.
|
||||||
* @param dataStringBuilder The {@code StringBuilder} to read data into.
|
* @param dataStringBuilder The {@code StringBuilder} to read data into.
|
||||||
* @param ignoreNonExistentFile The {@code boolean} that decides if it should be considered an
|
* @param ignoreNonExistentFile The {@code boolean} that decides if it should be considered an
|
||||||
* error if file to read doesn't exist.
|
* error if file to read doesn't exist.
|
||||||
* @return Returns the {@code error} if reading was not successful, otherwise {@code null}.
|
* @return Returns the {@code error} if reading was not successful, otherwise {@code null}.
|
||||||
*/
|
*/
|
||||||
public static Error readStringFromFile(String label, final String filePath, Charset charset, @NonNull final StringBuilder dataStringBuilder, final boolean ignoreNonExistentFile) {
|
public static Error readTextFromFile(String label, final String filePath, Charset charset, @NonNull final StringBuilder dataStringBuilder, final boolean ignoreNonExistentFile) {
|
||||||
label = (label == null ? "" : label + " ");
|
label = (label == null ? "" : label + " ");
|
||||||
if (filePath == null || filePath.isEmpty()) return FunctionErrno.ERRNO_NULL_OR_EMPTY_PARAMETER.getError(label + "file path", "readStringFromFile");
|
if (filePath == null || filePath.isEmpty()) return FunctionErrno.ERRNO_NULL_OR_EMPTY_PARAMETER.getError(label + "file path", "readStringFromFile");
|
||||||
|
|
||||||
Logger.logVerbose(LOG_TAG, "Reading string from " + label + "file at path \"" + filePath + "\"");
|
Logger.logVerbose(LOG_TAG, "Reading text from " + label + "file at path \"" + filePath + "\"");
|
||||||
|
|
||||||
Error error;
|
Error error;
|
||||||
|
|
||||||
@@ -1423,7 +1423,7 @@ public class FileUtils {
|
|||||||
FileInputStream fileInputStream = null;
|
FileInputStream fileInputStream = null;
|
||||||
BufferedReader bufferedReader = null;
|
BufferedReader bufferedReader = null;
|
||||||
try {
|
try {
|
||||||
// Read string from file
|
// Read text from file
|
||||||
fileInputStream = new FileInputStream(filePath);
|
fileInputStream = new FileInputStream(filePath);
|
||||||
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, charset));
|
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, charset));
|
||||||
|
|
||||||
@@ -1437,7 +1437,7 @@ public class FileUtils {
|
|||||||
|
|
||||||
Logger.logVerbose(LOG_TAG, Logger.getMultiLineLogStringEntry("String", DataUtils.getTruncatedCommandOutput(dataStringBuilder.toString(), Logger.LOGGER_ENTRY_MAX_SAFE_PAYLOAD, true, false, true), "-"));
|
Logger.logVerbose(LOG_TAG, Logger.getMultiLineLogStringEntry("String", DataUtils.getTruncatedCommandOutput(dataStringBuilder.toString(), Logger.LOGGER_ENTRY_MAX_SAFE_PAYLOAD, true, false, true), "-"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return FileUtilsErrno.ERRNO_READING_STRING_FROM_FILE_FAILED_WITH_EXCEPTION.getError(e, label + "file", filePath, e.getMessage());
|
return FileUtilsErrno.ERRNO_READING_TEXT_FROM_FILE_FAILED_WITH_EXCEPTION.getError(e, label + "file", filePath, e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
closeCloseable(fileInputStream);
|
closeCloseable(fileInputStream);
|
||||||
closeCloseable(bufferedReader);
|
closeCloseable(bufferedReader);
|
||||||
@@ -1497,7 +1497,7 @@ public class FileUtils {
|
|||||||
FileInputStream fileInputStream = null;
|
FileInputStream fileInputStream = null;
|
||||||
ObjectInputStream objectInputStream = null;
|
ObjectInputStream objectInputStream = null;
|
||||||
try {
|
try {
|
||||||
// Read string from file
|
// Read serializable object from file
|
||||||
fileInputStream = new FileInputStream(filePath);
|
fileInputStream = new FileInputStream(filePath);
|
||||||
objectInputStream = new ObjectInputStream(fileInputStream);
|
objectInputStream = new ObjectInputStream(fileInputStream);
|
||||||
//serializableObject = (T) objectInputStream.readObject();
|
//serializableObject = (T) objectInputStream.readObject();
|
||||||
@@ -1515,7 +1515,7 @@ public class FileUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the {@link String} {@code dataString} with a specific {@link Charset} to file at path.
|
* Write text {@code dataString} with a specific {@link Charset} to file at path.
|
||||||
*
|
*
|
||||||
* @param label The optional label for file to write. This can optionally be {@code null}.
|
* @param label The optional label for file to write. This can optionally be {@code null}.
|
||||||
* @param filePath The {@code path} for file to write.
|
* @param filePath The {@code path} for file to write.
|
||||||
@@ -1525,11 +1525,11 @@ public class FileUtils {
|
|||||||
* @param append The {@code boolean} that decides if file should be appended to or not.
|
* @param append The {@code boolean} that decides if file should be appended to or not.
|
||||||
* @return Returns the {@code error} if writing was not successful, otherwise {@code null}.
|
* @return Returns the {@code error} if writing was not successful, otherwise {@code null}.
|
||||||
*/
|
*/
|
||||||
public static Error writeStringToFile(String label, final String filePath, Charset charset, final String dataString, final boolean append) {
|
public static Error writeTextToFile(String label, final String filePath, Charset charset, final String dataString, final boolean append) {
|
||||||
label = (label == null ? "" : label + " ");
|
label = (label == null ? "" : label + " ");
|
||||||
if (filePath == null || filePath.isEmpty()) return FunctionErrno.ERRNO_NULL_OR_EMPTY_PARAMETER.getError(label + "file path", "writeStringToFile");
|
if (filePath == null || filePath.isEmpty()) return FunctionErrno.ERRNO_NULL_OR_EMPTY_PARAMETER.getError(label + "file path", "writeStringToFile");
|
||||||
|
|
||||||
Logger.logVerbose(LOG_TAG, Logger.getMultiLineLogStringEntry("Writing string to " + label + "file at path \"" + filePath + "\"", DataUtils.getTruncatedCommandOutput(dataString, Logger.LOGGER_ENTRY_MAX_SAFE_PAYLOAD, true, false, true), "-"));
|
Logger.logVerbose(LOG_TAG, Logger.getMultiLineLogStringEntry("Writing text to " + label + "file at path \"" + filePath + "\"", DataUtils.getTruncatedCommandOutput(dataString, Logger.LOGGER_ENTRY_MAX_SAFE_PAYLOAD, true, false, true), "-"));
|
||||||
|
|
||||||
Error error;
|
Error error;
|
||||||
|
|
||||||
@@ -1547,14 +1547,14 @@ public class FileUtils {
|
|||||||
FileOutputStream fileOutputStream = null;
|
FileOutputStream fileOutputStream = null;
|
||||||
BufferedWriter bufferedWriter = null;
|
BufferedWriter bufferedWriter = null;
|
||||||
try {
|
try {
|
||||||
// Write string to file
|
// Write text to file
|
||||||
fileOutputStream = new FileOutputStream(filePath, append);
|
fileOutputStream = new FileOutputStream(filePath, append);
|
||||||
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, charset));
|
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, charset));
|
||||||
|
|
||||||
bufferedWriter.write(dataString);
|
bufferedWriter.write(dataString);
|
||||||
bufferedWriter.flush();
|
bufferedWriter.flush();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return FileUtilsErrno.ERRNO_WRITING_STRING_TO_FILE_FAILED_WITH_EXCEPTION.getError(e, label + "file", filePath, e.getMessage());
|
return FileUtilsErrno.ERRNO_WRITING_TEXT_TO_FILE_FAILED_WITH_EXCEPTION.getError(e, label + "file", filePath, e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
closeCloseable(fileOutputStream);
|
closeCloseable(fileOutputStream);
|
||||||
closeCloseable(bufferedWriter);
|
closeCloseable(bufferedWriter);
|
||||||
@@ -1586,7 +1586,7 @@ public class FileUtils {
|
|||||||
FileOutputStream fileOutputStream = null;
|
FileOutputStream fileOutputStream = null;
|
||||||
ObjectOutputStream objectOutputStream = null;
|
ObjectOutputStream objectOutputStream = null;
|
||||||
try {
|
try {
|
||||||
// Write object to file
|
// Write serializable object to file
|
||||||
fileOutputStream = new FileOutputStream(filePath);
|
fileOutputStream = new FileOutputStream(filePath);
|
||||||
objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||||
|
|
||||||
|
@@ -158,18 +158,18 @@ public class FileUtilsTests {
|
|||||||
|
|
||||||
// Write "line1" to dir2/sub_reg1 regular file
|
// Write "line1" to dir2/sub_reg1 regular file
|
||||||
label = dir2__sub_reg1_label; path = dir2__sub_reg1_path;
|
label = dir2__sub_reg1_label; path = dir2__sub_reg1_path;
|
||||||
error = FileUtils.writeStringToFile(label, path, Charset.defaultCharset(), "line1", false);
|
error = FileUtils.writeTextToFile(label, path, Charset.defaultCharset(), "line1", false);
|
||||||
assertEqual("Failed to write string to " + label + " file with append mode false", null, error);
|
assertEqual("Failed to write string to " + label + " file with append mode false", null, error);
|
||||||
if (!FileUtils.regularFileExists(path, false))
|
if (!FileUtils.regularFileExists(path, false))
|
||||||
throwException("The " + label + " file does not exist as expected after writing to it with append mode false");
|
throwException("The " + label + " file does not exist as expected after writing to it with append mode false");
|
||||||
|
|
||||||
// Write "line2" to dir2/sub_reg1 regular file
|
// Write "line2" to dir2/sub_reg1 regular file
|
||||||
error = FileUtils.writeStringToFile(label, path, Charset.defaultCharset(), "\nline2", true);
|
error = FileUtils.writeTextToFile(label, path, Charset.defaultCharset(), "\nline2", true);
|
||||||
assertEqual("Failed to write string to " + label + " file with append mode true", null, error);
|
assertEqual("Failed to write string to " + label + " file with append mode true", null, error);
|
||||||
|
|
||||||
// Read dir2/sub_reg1 regular file
|
// Read dir2/sub_reg1 regular file
|
||||||
StringBuilder dataStringBuilder = new StringBuilder();
|
StringBuilder dataStringBuilder = new StringBuilder();
|
||||||
error = FileUtils.readStringFromFile(label, path, Charset.defaultCharset(), dataStringBuilder, false);
|
error = FileUtils.readTextFromFile(label, path, Charset.defaultCharset(), dataStringBuilder, false);
|
||||||
assertEqual("Failed to read from " + label + " file", null, error);
|
assertEqual("Failed to read from " + label + " file", null, error);
|
||||||
assertEqual("The data read from " + label + " file in not as expected", "line1\nline2", dataStringBuilder.toString());
|
assertEqual("The data read from " + label + " file in not as expected", "line1\nline2", dataStringBuilder.toString());
|
||||||
|
|
||||||
|
@@ -141,7 +141,7 @@ public class ShareUtils {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Error error = FileUtils.writeStringToFile(label, filePath,
|
Error error = FileUtils.writeTextToFile(label, filePath,
|
||||||
Charset.defaultCharset(), text, false);
|
Charset.defaultCharset(), text, false);
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
Logger.logErrorExtended(LOG_TAG, error.toString());
|
Logger.logErrorExtended(LOG_TAG, error.toString());
|
||||||
|
@@ -65,8 +65,8 @@ public class FileUtilsErrno extends Errno {
|
|||||||
|
|
||||||
|
|
||||||
/* Errors for file reading and writing (350-400) */
|
/* Errors for file reading and writing (350-400) */
|
||||||
public static final Errno ERRNO_READING_STRING_FROM_FILE_FAILED_WITH_EXCEPTION = new Errno(TYPE, 350, "Reading string from %1$s at path \"%2$s\" failed.\nException: %3$s");
|
public static final Errno ERRNO_READING_TEXT_FROM_FILE_FAILED_WITH_EXCEPTION = new Errno(TYPE, 350, "Reading text from %1$s at path \"%2$s\" failed.\nException: %3$s");
|
||||||
public static final Errno ERRNO_WRITING_STRING_TO_FILE_FAILED_WITH_EXCEPTION = new Errno(TYPE, 351, "Writing string to %1$s at path \"%2$s\" failed.\nException: %3$s");
|
public static final Errno ERRNO_WRITING_TEXT_TO_FILE_FAILED_WITH_EXCEPTION = new Errno(TYPE, 351, "Writing text to %1$s at path \"%2$s\" failed.\nException: %3$s");
|
||||||
public static final Errno ERRNO_UNSUPPORTED_CHARSET = new Errno(TYPE, 352, "Unsupported charset \"%1$s\"");
|
public static final Errno ERRNO_UNSUPPORTED_CHARSET = new Errno(TYPE, 352, "Unsupported charset \"%1$s\"");
|
||||||
public static final Errno ERRNO_CHECKING_IF_CHARSET_SUPPORTED_FAILED = new Errno(TYPE, 353, "Checking if charset \"%1$s\" is supported failed.\nException: %2$s");
|
public static final Errno ERRNO_CHECKING_IF_CHARSET_SUPPORTED_FAILED = new Errno(TYPE, 353, "Checking if charset \"%1$s\" is supported failed.\nException: %2$s");
|
||||||
public static final Errno ERRNO_READING_SERIALIZABLE_OBJECT_TO_FILE_FAILED_WITH_EXCEPTION = new Errno(TYPE, 354, "Reading serializable object from %1$s at path \"%2$s\" failed.\nException: %3$s");
|
public static final Errno ERRNO_READING_SERIALIZABLE_OBJECT_TO_FILE_FAILED_WITH_EXCEPTION = new Errno(TYPE, 354, "Reading serializable object from %1$s at path \"%2$s\" failed.\nException: %3$s");
|
||||||
|
@@ -253,7 +253,7 @@ public class ResultSender {
|
|||||||
// Write error or output to temp file
|
// Write error or output to temp file
|
||||||
// Check errCode file creation below for explanation for why temp file is used
|
// Check errCode file creation below for explanation for why temp file is used
|
||||||
String temp_filename = resultConfig.resultFileBasename + "-" + AndroidUtils.getCurrentMilliSecondLocalTimeStamp();
|
String temp_filename = resultConfig.resultFileBasename + "-" + AndroidUtils.getCurrentMilliSecondLocalTimeStamp();
|
||||||
error = FileUtils.writeStringToFile(temp_filename, resultConfig.resultDirectoryPath + "/" + temp_filename,
|
error = FileUtils.writeTextToFile(temp_filename, resultConfig.resultDirectoryPath + "/" + temp_filename,
|
||||||
null, error_or_output, false);
|
null, error_or_output, false);
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
return error;
|
return error;
|
||||||
@@ -283,7 +283,7 @@ public class ResultSender {
|
|||||||
// Write stdout to file
|
// Write stdout to file
|
||||||
if (!resultDataStdout.isEmpty()) {
|
if (!resultDataStdout.isEmpty()) {
|
||||||
filename = RESULT_SENDER.RESULT_FILE_STDOUT_PREFIX + resultConfig.resultFilesSuffix;
|
filename = RESULT_SENDER.RESULT_FILE_STDOUT_PREFIX + resultConfig.resultFilesSuffix;
|
||||||
error = FileUtils.writeStringToFile(filename, resultConfig.resultDirectoryPath + "/" + filename,
|
error = FileUtils.writeTextToFile(filename, resultConfig.resultDirectoryPath + "/" + filename,
|
||||||
null, resultDataStdout, false);
|
null, resultDataStdout, false);
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
return error;
|
return error;
|
||||||
@@ -293,7 +293,7 @@ public class ResultSender {
|
|||||||
// Write stderr to file
|
// Write stderr to file
|
||||||
if (!resultDataStderr.isEmpty()) {
|
if (!resultDataStderr.isEmpty()) {
|
||||||
filename = RESULT_SENDER.RESULT_FILE_STDERR_PREFIX + resultConfig.resultFilesSuffix;
|
filename = RESULT_SENDER.RESULT_FILE_STDERR_PREFIX + resultConfig.resultFilesSuffix;
|
||||||
error = FileUtils.writeStringToFile(filename, resultConfig.resultDirectoryPath + "/" + filename,
|
error = FileUtils.writeTextToFile(filename, resultConfig.resultDirectoryPath + "/" + filename,
|
||||||
null, resultDataStderr, false);
|
null, resultDataStderr, false);
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
return error;
|
return error;
|
||||||
@@ -303,7 +303,7 @@ public class ResultSender {
|
|||||||
// Write exitCode to file
|
// Write exitCode to file
|
||||||
if (!resultDataExitCode.isEmpty()) {
|
if (!resultDataExitCode.isEmpty()) {
|
||||||
filename = RESULT_SENDER.RESULT_FILE_EXIT_CODE_PREFIX + resultConfig.resultFilesSuffix;
|
filename = RESULT_SENDER.RESULT_FILE_EXIT_CODE_PREFIX + resultConfig.resultFilesSuffix;
|
||||||
error = FileUtils.writeStringToFile(filename, resultConfig.resultDirectoryPath + "/" + filename,
|
error = FileUtils.writeTextToFile(filename, resultConfig.resultDirectoryPath + "/" + filename,
|
||||||
null, resultDataExitCode, false);
|
null, resultDataExitCode, false);
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
return error;
|
return error;
|
||||||
@@ -313,7 +313,7 @@ public class ResultSender {
|
|||||||
// Write errmsg to file
|
// Write errmsg to file
|
||||||
if (resultData.isStateFailed() && !resultDataErrmsg.isEmpty()) {
|
if (resultData.isStateFailed() && !resultDataErrmsg.isEmpty()) {
|
||||||
filename = RESULT_SENDER.RESULT_FILE_ERRMSG_PREFIX + resultConfig.resultFilesSuffix;
|
filename = RESULT_SENDER.RESULT_FILE_ERRMSG_PREFIX + resultConfig.resultFilesSuffix;
|
||||||
error = FileUtils.writeStringToFile(filename, resultConfig.resultDirectoryPath + "/" + filename,
|
error = FileUtils.writeTextToFile(filename, resultConfig.resultDirectoryPath + "/" + filename,
|
||||||
null, resultDataErrmsg, false);
|
null, resultDataErrmsg, false);
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
return error;
|
return error;
|
||||||
@@ -331,7 +331,7 @@ public class ResultSender {
|
|||||||
// Write errCode to temp file
|
// Write errCode to temp file
|
||||||
String temp_filename = RESULT_SENDER.RESULT_FILE_ERR_PREFIX + "-" + AndroidUtils.getCurrentMilliSecondLocalTimeStamp();
|
String temp_filename = RESULT_SENDER.RESULT_FILE_ERR_PREFIX + "-" + AndroidUtils.getCurrentMilliSecondLocalTimeStamp();
|
||||||
if (!resultConfig.resultFilesSuffix.isEmpty()) temp_filename = temp_filename + "-" + resultConfig.resultFilesSuffix;
|
if (!resultConfig.resultFilesSuffix.isEmpty()) temp_filename = temp_filename + "-" + resultConfig.resultFilesSuffix;
|
||||||
error = FileUtils.writeStringToFile(temp_filename, resultConfig.resultDirectoryPath + "/" + temp_filename,
|
error = FileUtils.writeTextToFile(temp_filename, resultConfig.resultDirectoryPath + "/" + temp_filename,
|
||||||
null, String.valueOf(resultData.getErrCode()), false);
|
null, String.valueOf(resultData.getErrCode()), false);
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
return error;
|
return error;
|
||||||
|
Reference in New Issue
Block a user