Add a method to append to files in the file utility.
Test: added to FileUtilTest for #writeToFile(InputStream, File, boolean)
Test: added to FileUtilTest for #writeToFile(InputStream, File)
Test: added to FileUtilTest for #writeToFile(String, File)
Test: added to FileUtilTest for #writeToFile(String, File, boolean)
Change-Id: I48665e4ab17b0b5599654880bb005ed478b7a38f
diff --git a/src/com/android/tradefed/util/FileUtil.java b/src/com/android/tradefed/util/FileUtil.java
index fc62504..b029621 100644
--- a/src/com/android/tradefed/util/FileUtil.java
+++ b/src/com/android/tradefed/util/FileUtil.java
@@ -22,12 +22,14 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
+import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
+import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -476,24 +478,48 @@
* A helper method for writing string data to file
*
* @param inputString the input {@link String}
- * @param destFile the dest file to write to
+ * @param destFile the destination file to write to
*/
public static void writeToFile(String inputString, File destFile) throws IOException {
- writeToFile(new ByteArrayInputStream(inputString.getBytes()), destFile);
+ writeToFile(inputString, destFile, false);
+ }
+
+ /**
+ * A helper method for writing or appending string data to file
+ *
+ * @param inputString the input {@link String}
+ * @param destFile the destination file to write or append to
+ * @param append append to end of file if true, overwrite otherwise
+ */
+ public static void writeToFile(String inputString, File destFile, boolean append)
+ throws IOException {
+ writeToFile(new ByteArrayInputStream(inputString.getBytes()), destFile, append);
}
/**
* A helper method for writing stream data to file
*
* @param input the unbuffered input stream
- * @param destFile the dest file to write to
+ * @param destFile the destination file to write to
*/
public static void writeToFile(InputStream input, File destFile) throws IOException {
+ writeToFile(input, destFile, false);
+ }
+
+ /**
+ * A helper method for writing stream data to file
+ *
+ * @param input the unbuffered input stream
+ * @param out the destination file to write or append to
+ * @param append append to end of file if true, overwrite otherwise
+ */
+ public static void writeToFile(
+ InputStream input, File destFile, boolean append) throws IOException {
InputStream origStream = null;
OutputStream destStream = null;
try {
origStream = new BufferedInputStream(input);
- destStream = new BufferedOutputStream(new FileOutputStream(destFile));
+ destStream = new BufferedOutputStream(new FileOutputStream(destFile, append));
StreamUtil.copyStreams(origStream, destStream);
} finally {
StreamUtil.close(origStream);
diff --git a/tests/src/com/android/tradefed/util/FileUtilTest.java b/tests/src/com/android/tradefed/util/FileUtilTest.java
index 7ac66e1..1e316fa 100644
--- a/tests/src/com/android/tradefed/util/FileUtilTest.java
+++ b/tests/src/com/android/tradefed/util/FileUtilTest.java
@@ -19,6 +19,7 @@
import junit.framework.TestCase;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
@@ -160,4 +161,130 @@
FileUtil.deleteFile(testFile);
}
}
+
+ /**
+ * Test {@link FileUtil#writeToFile(InputStream, File, boolean)} succeeds overwriting an
+ * existent file.
+ */
+ public void testWriteToFile_overwrites_exists() throws IOException {
+ File testFile = null;
+ try {
+ testFile = File.createTempFile("doesnotmatter", ".txt");
+ FileUtil.writeToFile(new ByteArrayInputStream("write1".getBytes()), testFile, false);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write1");
+ FileUtil.writeToFile(new ByteArrayInputStream("write2".getBytes()), testFile, false);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write2");
+ } finally {
+ FileUtil.deleteFile(testFile);
+ }
+ }
+
+ /**
+ * Test {@link FileUtil#writeToFile(InputStream, File, boolean)} succeeds appending to an
+ * existent file.
+ */
+ public void testWriteToFile_appends_exists() throws IOException {
+ File testFile = null;
+ try {
+ testFile = File.createTempFile("doesnotmatter", ".txt");
+ FileUtil.writeToFile(new ByteArrayInputStream("write1".getBytes()), testFile, true);
+ FileUtil.writeToFile(new ByteArrayInputStream("write2".getBytes()), testFile, true);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write1write2");
+ } finally {
+ FileUtil.deleteFile(testFile);
+ }
+ }
+
+ /**
+ * Test {@link FileUtil#writeToFile(InputStream, File, boolean)} succeeds writing to an
+ * uncreated file.
+ */
+ public void testWriteToFile_overwrites_doesNotExist() throws IOException {
+ File testFile = null;
+ try {
+ testFile = new File("nonexistant");
+ FileUtil.writeToFile(new ByteArrayInputStream("write1".getBytes()), testFile, false);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write1");
+ } finally {
+ FileUtil.deleteFile(testFile);
+ }
+ }
+
+ /**
+ * Test {@link FileUtil#writeToFile(InputStream, File, boolean)} succeeds appending to an
+ * uncreated file.
+ */
+ public void testWriteToFile_appends_doesNotExist() throws IOException {
+ File testFile = null;
+ try {
+ testFile = new File("nonexistant");
+ FileUtil.writeToFile(new ByteArrayInputStream("write1".getBytes()), testFile, true);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write1");
+ } finally {
+ FileUtil.deleteFile(testFile);
+ }
+ }
+
+ /**
+ * Test {@link FileUtil#writeToFile(InputStream, File)} succeeds overwriting to a file.
+ */
+ public void testWriteToFile_stream_overwrites() throws IOException {
+ File testFile = null;
+ try {
+ testFile = File.createTempFile("doesnotmatter", ".txt");
+ FileUtil.writeToFile(new ByteArrayInputStream("write1".getBytes()), testFile);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write1");
+ FileUtil.writeToFile(new ByteArrayInputStream("write2".getBytes()), testFile);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write2");
+ } finally {
+ FileUtil.deleteFile(testFile);
+ }
+ }
+
+ /**
+ * Test {@link FileUtil#writeToFile(String, File, boolean)} succeeds overwriting to a file.
+ */
+ public void testWriteToFile_string_overwrites() throws IOException {
+ File testFile = null;
+ try {
+ testFile = File.createTempFile("doesnotmatter", ".txt");
+ FileUtil.writeToFile("write1", testFile, false);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write1");
+ FileUtil.writeToFile("write2", testFile, false);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write2");
+ } finally {
+ FileUtil.deleteFile(testFile);
+ }
+ }
+
+ /**
+ * Test {@link FileUtil#writeToFile(String, File, boolean)} succeeds appending to a file.
+ */
+ public void testWriteToFile_string_appends() throws IOException {
+ File testFile = null;
+ try {
+ testFile = File.createTempFile("doesnotmatter", ".txt");
+ FileUtil.writeToFile("write1", testFile, true);
+ FileUtil.writeToFile("write2", testFile, true);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write1write2");
+ } finally {
+ FileUtil.deleteFile(testFile);
+ }
+ }
+
+ /**
+ * Test {@link FileUtil#writeToFile(String, File)} succeeds overwriting to a file.
+ */
+ public void testWriteToFile_string_defaultOverwrites() throws IOException {
+ File testFile = null;
+ try {
+ testFile = File.createTempFile("doesnotmatter", ".txt");
+ FileUtil.writeToFile("write1", testFile);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write1");
+ FileUtil.writeToFile("write2", testFile);
+ assertEquals(FileUtil.readStringFromFile(testFile), "write2");
+ } finally {
+ FileUtil.deleteFile(testFile);
+ }
+ }
}