Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
2.0.0
-----

### New features

- Added `RecordingInputStream.asOutputStream()` for direct writing of recorded data without an input stream. [#108](https://github.com/iipc/webarchive-commons/pull/108)

### Removals

#### Removed Apache HttpClient 3.1
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/archive/io/RecordingInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,12 @@ public synchronized void mark(int readlimit) {

@Override
public boolean markSupported() {
return this.in.markSupported();
return in != null && this.in.markSupported();
}

@Override
public synchronized void reset() throws IOException {
this.in.reset();
if (in != null) this.in.reset();
this.recordingOutputStream.reset();
}

Expand Down Expand Up @@ -418,4 +418,13 @@ public void chopAtMessageBodyBegin() {
public void clearForReuse() throws IOException {
recordingOutputStream.clearForReuse();
}

/**
* Returns an OutputStream that can be used for recording input data. This is useful if the input comes in some
* form other than an InputStream. For example, if the input is provided by a callback periodically called with
* a chunk of data.
*/
public RecordingOutputStream asOutputStream() {
return this.recordingOutputStream;
}
}
13 changes: 12 additions & 1 deletion src/test/java/org/archive/io/RecordingInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class RecordingInputStreamTest {
@TempDir
File tempDir;


/**
* Test readFullyOrUntil soft (no exception) and hard (exception)
* length cutoffs, timeout, and rate-throttling.
Expand Down Expand Up @@ -128,4 +127,16 @@ public void run() {
}.start();

}

@Test
public void testAsOutputStream() throws IOException {
RecordingInputStream ris = new RecordingInputStream(16384, (new File(
tempDir, "testAsOutputStream").getAbsolutePath()));
ris.open(null);
ris.asOutputStream().write("hello".getBytes());
ris.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ris.getReplayInputStream().readFullyTo(baos);
assertEquals("hello", baos.toString());
}
}