Commons IO
  1. Commons IO
  2. IO-349

Add API with array offset and length argument to FileUtils.writeByteArrayToFile

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.4
    • Fix Version/s: 2.5
    • Component/s: Utilities
    • Labels:

      Description

      The FileUtils.writeByteArrayToFile method does not allow a subset of an array to be written to a file. Instead, the subset must be copied to a separate array, increasing the lines of code and (for all JVMs I know about) runtime.

      Sister methods that take an offset and length should be added, inline with the byte array-oriented methods in the Java standard library.

      Attached is a patch that implements FileUtils.writeByteArrayToFile(File file, byte[] data, int offset, int length) and FileUtils.writeByteArrayToFile(File file, byte[] data, int offset, int length, boolean append) and associated testcases in FileUtilsTestCase.java.

        Activity

        Hide
        Gary Gregory added a comment -

        YW. Keep 'em coming.

        Show
        Gary Gregory added a comment - YW. Keep 'em coming.
        Hide
        David Bild added a comment -

        Ah, yes indeed. Thanks for the lesson.

        Show
        David Bild added a comment - Ah, yes indeed. Thanks for the lesson.
        Hide
        Gary Gregory added a comment -

        The issue with the tests I changed is that these tests used hard offsets into the byte array returned by String.getBytes(). There is no guarantee that these offsets will address the expected bytes when the test is run on platforms with encodings that create byte arrays of different sizes. For example UTF-16 and UTF-32 type of encodings. This is unlikely but removing the mystery answer from getBytes() is simple.

        Show
        Gary Gregory added a comment - The issue with the tests I changed is that these tests used hard offsets into the byte array returned by String.getBytes(). There is no guarantee that these offsets will address the expected bytes when the test is run on platforms with encodings that create byte arrays of different sizes. For example UTF-16 and UTF-32 type of encodings. This is unlikely but removing the mystery answer from getBytes() is simple.
        Hide
        David Bild added a comment -

        Thanks much for the code review and commit.

        FYI, the tests for the existing #writeByteArrayToFile methods use the platform default charset for String encoding. Is there a reason to prefer UTF-8 (those were the only modifications I saw in the unit tests)? If so, I can submit a patch to update the existing tests as well.

        Show
        David Bild added a comment - Thanks much for the code review and commit. FYI, the tests for the existing #writeByteArrayToFile methods use the platform default charset for String encoding. Is there a reason to prefer UTF-8 (those were the only modifications I saw in the unit tests)? If so, I can submit a patch to update the existing tests as well.
        Hide
        Gary Gregory added a comment -

        It's simple unless the unit tests have subtle bugs

        commit -m "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." C:/svn/org/apache/commons/trunks-proper/io/src/test/java/org/apache/commons/io/FileUtilsTestCase.java C:/svn/org/apache/commons/trunks-proper/io/src/main/java/org/apache/commons/io/FileUtils.java C:/svn/org/apache/commons/trunks-proper/io/src/changes/changes.xml C:/svn/org/apache/commons/trunks-proper/io/pom.xml
        Sending C:/svn/org/apache/commons/trunks-proper/io/pom.xml
        Sending C:/svn/org/apache/commons/trunks-proper/io/src/changes/changes.xml
        Sending C:/svn/org/apache/commons/trunks-proper/io/src/main/java/org/apache/commons/io/FileUtils.java
        Sending C:/svn/org/apache/commons/trunks-proper/io/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
        Transmitting file data ...
        Committed revision 1401648.

        Show
        Gary Gregory added a comment - It's simple unless the unit tests have subtle bugs commit -m " 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." C:/svn/org/apache/commons/trunks-proper/io/src/test/java/org/apache/commons/io/FileUtilsTestCase.java C:/svn/org/apache/commons/trunks-proper/io/src/main/java/org/apache/commons/io/FileUtils.java C:/svn/org/apache/commons/trunks-proper/io/src/changes/changes.xml C:/svn/org/apache/commons/trunks-proper/io/pom.xml Sending C:/svn/org/apache/commons/trunks-proper/io/pom.xml Sending C:/svn/org/apache/commons/trunks-proper/io/src/changes/changes.xml Sending C:/svn/org/apache/commons/trunks-proper/io/src/main/java/org/apache/commons/io/FileUtils.java Sending C:/svn/org/apache/commons/trunks-proper/io/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Transmitting file data ... Committed revision 1401648.
        Hide
        David Bild added a comment -

        No comments? Is this functionality already available and I just missed it? Seems like a pretty simple patch to accept if not.

        Show
        David Bild added a comment - No comments? Is this functionality already available and I just missed it? Seems like a pretty simple patch to accept if not.

          People

          • Assignee:
            Unassigned
            Reporter:
            David Bild
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - 1h
              1h
              Remaining:
              Remaining Estimate - 1h
              1h
              Logged:
              Time Spent - Not Specified
              Not Specified

                Development