Skip to content

Commit 6a71042

Browse files
committed
Add AbstractOrigin.getByteArray(long, int).
1 parent 4e606b3 commit 6a71042

9 files changed

Lines changed: 106 additions & 9 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ The <action> type attribute can be add,update,fix,remove.
7171
<action dev="ggregory" type="add" due-to="Gary Gregory">
7272
Add some missing conversions to AbstractOrigin subclasses.
7373
</action>
74+
<action dev="ggregory" type="add" due-to="Gary Gregory">
75+
Add AbstractOrigin.getByteArray(long, int).
76+
</action>
7477
<!-- UPDATE -->
7578
<action dev="ggregory" type="update" due-to="Gary Gregory, Dependabot">
7679
Bump commons-parent from 57 to 58.

src/main/java/org/apache/commons/io/build/AbstractOrigin.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.nio.file.OpenOption;
3333
import java.nio.file.Path;
3434
import java.nio.file.Paths;
35+
import java.util.Arrays;
3536
import java.util.Objects;
3637

3738
import org.apache.commons.io.IOUtils;
@@ -381,6 +382,27 @@ public byte[] getByteArray() throws IOException {
381382
return Files.readAllBytes(getPath());
382383
}
383384

385+
/**
386+
* Gets this origin as a byte array, if possible.
387+
*
388+
* @param from the initial index of the range to be copied, inclusive.
389+
* @param length How many bytes to copy.
390+
* @return this origin as a byte array, if possible.
391+
* @throws IOException if an I/O error occurs.
392+
* @throws UnsupportedOperationException if the origin cannot be converted to a Path.
393+
* @since 2.13.0
394+
*/
395+
public byte[] getByteArray(final long from, final int length) throws IOException {
396+
final byte[] bytes = getByteArray();
397+
final int start = (int) from;
398+
// We include a separate check for int overflow.
399+
if (start < 0 || length < 0 || start + length < 0 || start + length > bytes.length) {
400+
throw new IllegalArgumentException("Couldn't read array (start: " + start + ", length: " + length
401+
+ ", data length: " + bytes.length + ").");
402+
}
403+
return Arrays.copyOfRange(bytes, start, start + length);
404+
}
405+
384406
/**
385407
* Gets this origin as a byte array, if possible.
386408
*

src/test/java/org/apache/commons/io/build/AbstractOriginTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.commons.io.build;
1818

19+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1920
import static org.junit.jupiter.api.Assertions.assertNotNull;
2021

2122
import java.io.IOException;
@@ -36,7 +37,8 @@
3637
*/
3738
public abstract class AbstractOriginTest<T, B extends AbstractOrigin<T, B>> {
3839

39-
protected static final String FILE_NAME_RO = "src/test/resources/org/apache/commons/io/test-file-20byteslength.bin";
40+
protected static final String FILE_RES_RO = "/org/apache/commons/io/test-file-20byteslength.bin";
41+
protected static final String FILE_NAME_RO = "src/test/resources" + FILE_RES_RO;
4042
protected static final String FILE_NAME_RW = "target/" + AbstractOriginTest.class.getSimpleName() + ".txt";
4143

4244
protected AbstractOrigin<T, B> originRo;
@@ -63,6 +65,21 @@ public void testGetByteArray() throws IOException {
6365
assertNotNull(getOriginRo().getByteArray());
6466
}
6567

68+
@Test
69+
public void testGetByteArrayAt_0_0() throws IOException {
70+
assertArrayEquals(new byte[] {}, getOriginRo().getByteArray(0, 0));
71+
}
72+
73+
@Test
74+
public void testGetByteArrayAt_0_1() throws IOException {
75+
assertArrayEquals(new byte[] { '1' }, getOriginRo().getByteArray(0, 1));
76+
}
77+
78+
@Test
79+
public void testGetByteArrayAt_1_1() throws IOException {
80+
assertArrayEquals(new byte[] { '2' }, getOriginRo().getByteArray(1, 1));
81+
}
82+
6683
@Test
6784
public void testGetCharSequence() throws IOException {
6885
assertNotNull(getOriginRo().getCharSequence(Charset.defaultCharset()));

src/test/java/org/apache/commons/io/build/ByteArrayOriginTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Paths;
24+
2125
import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin;
2226
import org.junit.jupiter.api.BeforeEach;
2327
import org.junit.jupiter.api.Test;
@@ -30,8 +34,8 @@
3034
public class ByteArrayOriginTest extends AbstractOriginTest<byte[], ByteArrayOrigin> {
3135

3236
@BeforeEach
33-
public void beforeEach() {
34-
setOriginRo(new ByteArrayOrigin(new byte[] { 0 }));
37+
public void beforeEach() throws IOException {
38+
setOriginRo(new ByteArrayOrigin(Files.readAllBytes(Paths.get(FILE_NAME_RO))));
3539
setOriginRw(new ByteArrayOrigin(new byte[] { 1 }));
3640
}
3741

src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

21+
import java.io.FileNotFoundException;
22+
import java.io.IOException;
23+
import java.nio.charset.StandardCharsets;
24+
25+
import org.apache.commons.io.IOUtils;
2126
import org.apache.commons.io.build.AbstractOrigin.CharSequenceOrigin;
2227
import org.junit.jupiter.api.BeforeEach;
2328
import org.junit.jupiter.api.Test;
@@ -30,8 +35,8 @@
3035
public class CharSequenceOriginTest extends AbstractOriginTest<CharSequence, CharSequenceOrigin> {
3136

3237
@BeforeEach
33-
public void beforeEach() {
34-
setOriginRo(new CharSequenceOrigin("Hello"));
38+
public void beforeEach() throws FileNotFoundException, IOException {
39+
setOriginRo(new CharSequenceOrigin(IOUtils.resourceToString(FILE_RES_RO, StandardCharsets.UTF_8)));
3540
setOriginRw(new CharSequenceOrigin("World"));
3641
}
3742

src/test/java/org/apache/commons/io/build/InputStreamOriginTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

21+
import java.io.FileInputStream;
22+
import java.io.FileNotFoundException;
2123
import java.io.InputStream;
2224

2325
import org.apache.commons.io.build.AbstractOrigin.InputStreamOrigin;
@@ -34,8 +36,8 @@ public class InputStreamOriginTest extends AbstractOriginTest<InputStream, Input
3436

3537
@SuppressWarnings("resource")
3638
@BeforeEach
37-
public void beforeEach() {
38-
setOriginRo(new InputStreamOrigin(CharSequenceInputStream.builder().setCharSequence("Hello").get()));
39+
public void beforeEach() throws FileNotFoundException {
40+
setOriginRo(new InputStreamOrigin(new FileInputStream(FILE_NAME_RO)));
3941
setOriginRw(new InputStreamOrigin(CharSequenceInputStream.builder().setCharSequence("World").get()));
4042
}
4143

src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ public void testGetByteArray() {
4646
assertThrows(UnsupportedOperationException.class, super::testGetByteArray);
4747
}
4848

49+
@Override
50+
@Test
51+
public void testGetByteArrayAt_0_0() {
52+
// Cannot convert a OutputStream to a byte[].
53+
assertThrows(UnsupportedOperationException.class, super::testGetByteArrayAt_0_0);
54+
}
55+
56+
@Override
57+
@Test
58+
public void testGetByteArrayAt_0_1() {
59+
// Cannot convert a OutputStream to a byte[].
60+
assertThrows(UnsupportedOperationException.class, super::testGetByteArrayAt_0_1);
61+
}
62+
63+
@Override
64+
@Test
65+
public void testGetByteArrayAt_1_1() {
66+
// Cannot convert a OutputStream to a byte[].
67+
assertThrows(UnsupportedOperationException.class, super::testGetByteArrayAt_1_1);
68+
}
69+
4970
@Override
5071
@Test
5172
public void testGetCharSequence() {

src/test/java/org/apache/commons/io/build/ReaderOriginTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

21+
import java.io.FileNotFoundException;
22+
import java.io.FileReader;
2123
import java.io.Reader;
2224

2325
import org.apache.commons.io.build.AbstractOrigin.ReaderOrigin;
@@ -34,8 +36,8 @@ public class ReaderOriginTest extends AbstractOriginTest<Reader, ReaderOrigin> {
3436

3537
@SuppressWarnings("resource")
3638
@BeforeEach
37-
public void beforeEach() {
38-
setOriginRo(new ReaderOrigin(new CharSequenceReader("Hello")));
39+
public void beforeEach() throws FileNotFoundException {
40+
setOriginRo(new ReaderOrigin(new FileReader(FILE_NAME_RO)));
3941
setOriginRw(new ReaderOrigin(new CharSequenceReader("World")));
4042
}
4143

src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ public void testGetByteArray() {
4545
assertThrows(UnsupportedOperationException.class, super::testGetByteArray);
4646
}
4747

48+
@Override
49+
@Test
50+
public void testGetByteArrayAt_0_0() {
51+
// Cannot convert a OutputStream to a byte[].
52+
assertThrows(UnsupportedOperationException.class, super::testGetByteArrayAt_0_0);
53+
}
54+
55+
@Override
56+
@Test
57+
public void testGetByteArrayAt_0_1() {
58+
// Cannot convert a OutputStream to a byte[].
59+
assertThrows(UnsupportedOperationException.class, super::testGetByteArrayAt_0_1);
60+
}
61+
62+
@Override
63+
@Test
64+
public void testGetByteArrayAt_1_1() {
65+
// Cannot convert a OutputStream to a byte[].
66+
assertThrows(UnsupportedOperationException.class, super::testGetByteArrayAt_1_1);
67+
}
68+
4869
@Override
4970
@Test
5071
public void testGetCharSequence() {

0 commit comments

Comments
 (0)