Skip to content

Commit 3d12a7b

Browse files
committed
[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.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1401648 13f79535-47bb-0310-9956-ffa450edef68
1 parent 558e03e commit 3d12a7b

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parent>
2020
<groupId>org.apache.commons</groupId>
2121
<artifactId>commons-parent</artifactId>
22-
<version>26</version>
22+
<version>27</version>
2323
</parent>
2424
<modelVersion>4.0.0</modelVersion>
2525
<groupId>commons-io</groupId>

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="201?-??-??" description="New features and bug fixes.">
50+
<action issue="IO-349" dev="ggregory" type="add" due-to="scop">
51+
Add API with array offset and length argument to FileUtils.writeByteArrayToFile.
52+
</action>
5053
<action issue="IO-352" dev="ggregory" type="fix" due-to="scop">
5154
Spelling fixes.
5255
</action>

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2089,10 +2089,44 @@ public static void writeByteArrayToFile(File file, byte[] data) throws IOExcepti
20892089
* @since IO 2.1
20902090
*/
20912091
public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException {
2092+
writeByteArrayToFile(file, data, 0, data.length, append);
2093+
}
2094+
2095+
/**
2096+
* Writes {@code len} bytes from the specified byte array starting
2097+
* at offset {@code off} to a file, creating the file if it does
2098+
* not exist.
2099+
*
2100+
* @param file the file to write to
2101+
* @param data the content to write to the file
2102+
* @param off the start offset in the data
2103+
* @param len the number of bytes to write
2104+
* @throws IOException in case of an I/O error
2105+
* @since 2.5
2106+
*/
2107+
public static void writeByteArrayToFile(File file, byte[] data, int off, int len) throws IOException {
2108+
writeByteArrayToFile(file, data, off, len, false);
2109+
}
2110+
2111+
/**
2112+
* Writes {@code len} bytes from the specified byte array starting
2113+
* at offset {@code off} to a file, creating the file if it does
2114+
* not exist.
2115+
*
2116+
* @param file the file to write to
2117+
* @param data the content to write to the file
2118+
* @param off the start offset in the data
2119+
* @param len the number of bytes to write
2120+
* @param append if {@code true}, then bytes will be added to the
2121+
* end of the file rather than overwriting
2122+
* @throws IOException in case of an I/O error
2123+
* @since 2.5
2124+
*/
2125+
public static void writeByteArrayToFile(File file, byte[] data, int off, int len, boolean append) throws IOException {
20922126
OutputStream out = null;
20932127
try {
20942128
out = openOutputStream(file, append);
2095-
out.write(data);
2129+
out.write(data, off, len);
20962130
out.close(); // don't swallow close Exception if copy completes normally
20972131
} finally {
20982132
IOUtils.closeQuietly(out);

src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,15 @@ public void testWriteByteArrayToFile() throws Exception {
17331733
assertEqualContent(data, file);
17341734
}
17351735

1736+
public void testWriteByteArrayToFile_WithOffsetAndLength() throws Exception {
1737+
File file = new File(getTestDirectory(), "write.obj");
1738+
byte[] data = new byte[] { 11, 21, 32, 41, 51 };
1739+
byte[] writtenData = new byte[3];
1740+
System.arraycopy(data, 1, writtenData, 0, 3);
1741+
FileUtils.writeByteArrayToFile(file, data, 1, 3);
1742+
assertEqualContent(writtenData, file);
1743+
}
1744+
17361745
public void testWriteLines_4arg() throws Exception {
17371746
Object[] data = new Object[] {
17381747
"hello", new StringBuffer("world"), "", "this is", null, "some text"};
@@ -2023,6 +2032,30 @@ public void testWriteByteArrayToFile_WithAppendOptionFalse_ShouldDeletePreviousF
20232032
assertEquals(expected, actual);
20242033
}
20252034

2035+
public void testWriteByteArrayToFile_WithOffsetAndLength_WithAppendOptionTrue_ShouldNotDeletePreviousFileLines() throws Exception {
2036+
File file = newFile("lines.txt");
2037+
FileUtils.writeStringToFile(file, "This line was there before you...");
2038+
2039+
byte[] data = "SKIP_THIS_this is brand new data_AND_SKIP_THIS".getBytes(Charsets.UTF_8);
2040+
FileUtils.writeByteArrayToFile(file, data, 10, 22, true);
2041+
2042+
String expected = "This line was there before you..." + "this is brand new data";
2043+
String actual = FileUtils.readFileToString(file, Charsets.UTF_8);
2044+
assertEquals(expected, actual);
2045+
}
2046+
2047+
public void testWriteByteArrayToFile_WithOffsetAndLength_WithAppendOptionTrue_ShouldDeletePreviousFileLines() throws Exception {
2048+
File file = newFile("lines.txt");
2049+
FileUtils.writeStringToFile(file, "This line was there before you...");
2050+
2051+
byte[] data = "SKIP_THIS_this is brand new data_AND_SKIP_THIS".getBytes(Charsets.UTF_8);
2052+
FileUtils.writeByteArrayToFile(file, data, 10, 22, false);
2053+
2054+
String expected = "this is brand new data";
2055+
String actual = FileUtils.readFileToString(file, Charsets.UTF_8);
2056+
assertEquals(expected, actual);
2057+
}
2058+
20262059
//-----------------------------------------------------------------------
20272060
public void testChecksumCRC32() throws Exception {
20282061
// create a test file

0 commit comments

Comments
 (0)