commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1401648 - in /commons/proper/io/trunk: pom.xml src/changes/changes.xml src/main/java/org/apache/commons/io/FileUtils.java src/test/java/org/apache/commons/io/FileUtilsTestCase.java
Date Wed, 24 Oct 2012 12:34:14 GMT
Author: ggregory
Date: Wed Oct 24 12:34:13 2012
New Revision: 1401648

URL: http://svn.apache.org/viewvc?rev=1401648&view=rev
Log:
[IO-349] Add API with array offset and length argument to FileUtils.writeByteArrayToFile.
Applied patch with changes: (1) Fixed bugs in unit tests (2) Added @since tags (3) Fixed formatting.

Modified:
    commons/proper/io/trunk/pom.xml
    commons/proper/io/trunk/src/changes/changes.xml
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Modified: commons/proper/io/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/pom.xml?rev=1401648&r1=1401647&r2=1401648&view=diff
==============================================================================
--- commons/proper/io/trunk/pom.xml (original)
+++ commons/proper/io/trunk/pom.xml Wed Oct 24 12:34:13 2012
@@ -19,7 +19,7 @@
   <parent>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-parent</artifactId>
-    <version>26</version>
+    <version>27</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
   <groupId>commons-io</groupId>

Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1401648&r1=1401647&r2=1401648&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Wed Oct 24 12:34:13 2012
@@ -47,6 +47,9 @@ The <action> type attribute can be add,u
   <body>
     <!-- The release date is the date RC is cut -->
     <release version="2.5" date="201?-??-??" description="New features and bug fixes.">
+      <action issue="IO-349" dev="ggregory" type="add" due-to="scop">
+        Add API with array offset and length argument to FileUtils.writeByteArrayToFile.
+      </action>            
       <action issue="IO-352" dev="ggregory" type="fix" due-to="scop">
         Spelling fixes.
       </action>            

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1401648&r1=1401647&r2=1401648&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Wed Oct 24
12:34:13 2012
@@ -2089,10 +2089,44 @@ public class FileUtils {
      * @since IO 2.1
      */
     public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws
IOException {
+        writeByteArrayToFile(file, data, 0, data.length, append);
+    }
+
+    /**
+     * Writes {@code len} bytes from the specified byte array starting
+     * at offset {@code off} to a file, creating the file if it does
+     * not exist.
+     *
+     * @param file  the file to write to
+     * @param data  the content to write to the file
+     * @param off   the start offset in the data
+     * @param len   the number of bytes to write
+     * @throws IOException in case of an I/O error
+     * @since 2.5
+     */
+    public static void writeByteArrayToFile(File file, byte[] data, int off, int len) throws
IOException {
+        writeByteArrayToFile(file, data, off, len, false);
+    }
+
+    /**
+     * Writes {@code len} bytes from the specified byte array starting
+     * at offset {@code off} to a file, creating the file if it does
+     * not exist.
+     *
+     * @param file  the file to write to
+     * @param data  the content to write to the file
+     * @param off   the start offset in the data
+     * @param len   the number of bytes to write
+     * @param append if {@code true}, then bytes will be added to the
+     * end of the file rather than overwriting
+     * @throws IOException in case of an I/O error
+     * @since 2.5
+     */
+    public static void writeByteArrayToFile(File file, byte[] data, int off, int len, boolean
append) throws IOException {
         OutputStream out = null;
         try {
             out = openOutputStream(file, append);
-            out.write(data);
+            out.write(data, off, len);
             out.close(); // don't swallow close Exception if copy completes normally
         } finally {
             IOUtils.closeQuietly(out);

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1401648&r1=1401647&r2=1401648&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Wed
Oct 24 12:34:13 2012
@@ -1733,6 +1733,15 @@ public class FileUtilsTestCase extends F
         assertEqualContent(data, file);
     }
 
+    public void testWriteByteArrayToFile_WithOffsetAndLength() throws Exception {
+        File file = new File(getTestDirectory(), "write.obj");
+        byte[] data = new byte[] { 11, 21, 32, 41, 51 };
+        byte[] writtenData = new byte[3];
+        System.arraycopy(data, 1, writtenData, 0, 3);
+        FileUtils.writeByteArrayToFile(file, data, 1, 3);
+        assertEqualContent(writtenData, file);
+    }
+
     public void testWriteLines_4arg() throws Exception {
         Object[] data = new Object[] {
             "hello", new StringBuffer("world"), "", "this is", null, "some text"};
@@ -2023,6 +2032,30 @@ public class FileUtilsTestCase extends F
         assertEquals(expected, actual);
     }
     
+    public void testWriteByteArrayToFile_WithOffsetAndLength_WithAppendOptionTrue_ShouldNotDeletePreviousFileLines()
throws Exception {
+        File file = newFile("lines.txt");
+        FileUtils.writeStringToFile(file, "This line was there before you...");
+
+        byte[] data = "SKIP_THIS_this is brand new data_AND_SKIP_THIS".getBytes(Charsets.UTF_8);
+        FileUtils.writeByteArrayToFile(file, data, 10, 22, true);
+
+        String expected = "This line was there before you..." + "this is brand new data";
+        String actual = FileUtils.readFileToString(file, Charsets.UTF_8);
+        assertEquals(expected, actual);
+    }
+
+    public void testWriteByteArrayToFile_WithOffsetAndLength_WithAppendOptionTrue_ShouldDeletePreviousFileLines()
throws Exception {
+        File file = newFile("lines.txt");
+        FileUtils.writeStringToFile(file, "This line was there before you...");
+
+        byte[] data = "SKIP_THIS_this is brand new data_AND_SKIP_THIS".getBytes(Charsets.UTF_8);
+        FileUtils.writeByteArrayToFile(file, data, 10, 22, false);
+
+        String expected = "this is brand new data";
+        String actual = FileUtils.readFileToString(file, Charsets.UTF_8);
+        assertEquals(expected, actual);
+    }
+
     //-----------------------------------------------------------------------
     public void testChecksumCRC32() throws Exception {
         // create a test file



Mime
View raw message