Changed!: Rename FileUtils readStringFromFile() to readTextFromFile() and writeStringToFile() to writeTextToFile()

This commit is contained in:
agnostic-apollo
2021-10-25 18:37:40 +05:00
parent 7bbc12c7c9
commit 37b9bcf5af
7 changed files with 27 additions and 27 deletions

View File

@@ -69,7 +69,7 @@ public class CrashUtils {
StringBuilder reportStringBuilder = new StringBuilder();
// 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) {
Logger.logErrorExtended(logTag, error.toString());
return;

View File

@@ -70,7 +70,7 @@ public class CrashHandler implements Thread.UncaughtExceptionHandler {
Logger.logError(reportString.toString());
// 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);
if (error != null) {
Logger.logErrorExtended(LOG_TAG, error.toString());

View File

@@ -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 filePath The {@code path} for file to read.
* @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 ignoreNonExistentFile The {@code boolean} that decides if it should be considered an
* error if file to read doesn't exist.
* @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 + " ");
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;
@@ -1423,7 +1423,7 @@ public class FileUtils {
FileInputStream fileInputStream = null;
BufferedReader bufferedReader = null;
try {
// Read string from file
// Read text from file
fileInputStream = new FileInputStream(filePath);
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), "-"));
} 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 {
closeCloseable(fileInputStream);
closeCloseable(bufferedReader);
@@ -1497,7 +1497,7 @@ public class FileUtils {
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
try {
// Read string from file
// Read serializable object from file
fileInputStream = new FileInputStream(filePath);
objectInputStream = new ObjectInputStream(fileInputStream);
//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 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.
* @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 + " ");
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;
@@ -1547,14 +1547,14 @@ public class FileUtils {
FileOutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;
try {
// Write string to file
// Write text to file
fileOutputStream = new FileOutputStream(filePath, append);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, charset));
bufferedWriter.write(dataString);
bufferedWriter.flush();
} 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 {
closeCloseable(fileOutputStream);
closeCloseable(bufferedWriter);
@@ -1586,7 +1586,7 @@ public class FileUtils {
FileOutputStream fileOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
// Write object to file
// Write serializable object to file
fileOutputStream = new FileOutputStream(filePath);
objectOutputStream = new ObjectOutputStream(fileOutputStream);

View File

@@ -158,18 +158,18 @@ public class FileUtilsTests {
// Write "line1" to dir2/sub_reg1 regular file
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);
if (!FileUtils.regularFileExists(path, 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
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);
// Read dir2/sub_reg1 regular file
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("The data read from " + label + " file in not as expected", "line1\nline2", dataStringBuilder.toString());

View File

@@ -141,7 +141,7 @@ public class ShareUtils {
return;
}
Error error = FileUtils.writeStringToFile(label, filePath,
Error error = FileUtils.writeTextToFile(label, filePath,
Charset.defaultCharset(), text, false);
if (error != null) {
Logger.logErrorExtended(LOG_TAG, error.toString());

View File

@@ -65,8 +65,8 @@ public class FileUtilsErrno extends Errno {
/* 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_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_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_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_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");

View File

@@ -253,7 +253,7 @@ public class ResultSender {
// Write error or output to temp file
// Check errCode file creation below for explanation for why temp file is used
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);
if (error != null) {
return error;
@@ -283,7 +283,7 @@ public class ResultSender {
// Write stdout to file
if (!resultDataStdout.isEmpty()) {
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);
if (error != null) {
return error;
@@ -293,7 +293,7 @@ public class ResultSender {
// Write stderr to file
if (!resultDataStderr.isEmpty()) {
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);
if (error != null) {
return error;
@@ -303,7 +303,7 @@ public class ResultSender {
// Write exitCode to file
if (!resultDataExitCode.isEmpty()) {
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);
if (error != null) {
return error;
@@ -313,7 +313,7 @@ public class ResultSender {
// Write errmsg to file
if (resultData.isStateFailed() && !resultDataErrmsg.isEmpty()) {
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);
if (error != null) {
return error;
@@ -331,7 +331,7 @@ public class ResultSender {
// Write errCode to temp file
String temp_filename = RESULT_SENDER.RESULT_FILE_ERR_PREFIX + "-" + AndroidUtils.getCurrentMilliSecondLocalTimeStamp();
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);
if (error != null) {
return error;