Skip to content

Commit f29c460

Browse files
committed
Add RandomAccessFileInputStream.copy(long, long, OutputStream)
1 parent 2ad1a78 commit f29c460

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The <action> type attribute can be add,update,fix,remove.
6060
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileOutputStream.getRandomAccessFile().</action>
6161
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyInputStream.setReference(InputStream), was package-private setIn(InputStream).</action>
6262
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyOutputStream.setReference(OutputStream).</action>
63+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileInputStream.copy(long, long, OutputStream).</action>
6364
<!-- UPDATE -->
6465
<action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump commons.bytebuddy.version from 1.15.10 to 1.15.11 #710.</action>
6566
</release>

src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
import java.io.File;
2121
import java.io.IOException;
22+
import java.io.OutputStream;
2223
import java.io.RandomAccessFile;
2324
import java.util.Objects;
2425

26+
import org.apache.commons.io.IOUtils;
2527
import org.apache.commons.io.build.AbstractOrigin;
2628
import org.apache.commons.io.build.AbstractStreamBuilder;
2729

@@ -188,6 +190,21 @@ public void close() throws IOException {
188190
}
189191
}
190192

193+
/**
194+
* Copies our bytes from the given starting position for a size to the output stream.
195+
*
196+
* @param pos Where to start copying. The offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
197+
* @param size The number of bytes to copy.
198+
* @param os Where to copy.
199+
* @return The number of bytes copied..
200+
* @throws IOException if {@code pos} is less than {@code 0} or if an I/O error occurs.
201+
* @since 2.19.0
202+
*/
203+
public long copy(final long pos, final long size, final OutputStream os) throws IOException {
204+
randomAccessFile.seek(pos);
205+
return IOUtils.copyLarge(this, os, 0, size);
206+
}
207+
191208
/**
192209
* Gets the underlying file.
193210
*

src/test/java/org/apache/commons/io/input/RandomAccessFileInputStreamTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.nio.file.StandardOpenOption;
3535

3636
import org.apache.commons.io.RandomAccessFileMode;
37+
import org.apache.commons.io.output.ByteArrayOutputStream;
3738
import org.junit.jupiter.api.Test;
3839

3940
/**
@@ -150,6 +151,37 @@ public void testConstructorCloseOnCloseFalse() throws IOException {
150151
}
151152
}
152153

154+
@Test
155+
public void testCopy() throws IOException {
156+
// @formatter:off
157+
try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder()
158+
.setRandomAccessFile(createRandomAccessFile())
159+
.setCloseOnClose(true)
160+
.get()) {
161+
// @formatter:on
162+
// A Test Line.
163+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
164+
// 0 and 12
165+
assertEquals(12, inputStream.copy(0, 12, baos));
166+
assertArrayEquals("A Test Line.".getBytes(StandardCharsets.ISO_8859_1), baos.toByteArray());
167+
// 0 and 1
168+
baos.reset();
169+
assertEquals(1, inputStream.copy(0, 1, baos));
170+
assertArrayEquals("A".getBytes(StandardCharsets.ISO_8859_1), baos.toByteArray());
171+
// 11 and 1
172+
baos.reset();
173+
assertEquals(1, inputStream.copy(11, 1, baos));
174+
assertArrayEquals(".".getBytes(StandardCharsets.ISO_8859_1), baos.toByteArray());
175+
// 1 and 10
176+
baos.reset();
177+
assertEquals(10, inputStream.copy(1, 10, baos));
178+
assertArrayEquals(" Test Line".getBytes(StandardCharsets.ISO_8859_1), baos.toByteArray());
179+
// next
180+
assertEquals('.', inputStream.read());
181+
}
182+
}
183+
}
184+
153185
@SuppressWarnings("resource") // instance variable access
154186
@Test
155187
public void testDeprecatedConstructorCloseOnCloseTrue() throws IOException {

0 commit comments

Comments
 (0)