1
0
mirror of https://github.com/moparisthebest/rcrdit synced 2025-01-04 10:08:22 -05:00

Continue appending to same file if InputStream breaks

This commit is contained in:
Travis Burtrum 2017-02-28 22:52:10 -05:00
parent 9df6f7b9f7
commit b293263462

View File

@ -62,9 +62,7 @@ public class RecordThread extends Thread {
while (running) { while (running) {
log.debug("recording started {}", recording); log.debug("recording started {}", recording);
File outputFile = null; File outputFile = null;
try (InputStream is = getInputStream.apply(recording); try (OutputStream os = new FileOutputStream(outputFile = recording.getNewFileToWrite(ext), true);
OutputStream os = new FileOutputStream(outputFile = recording.getNewFileToWrite(ext), true);
ReadableByteChannel ic = Channels.newChannel(is);
WritableByteChannel oc = Channels.newChannel(os); WritableByteChannel oc = Channels.newChannel(os);
) { ) {
// output file exists now, start onStart events // output file exists now, start onStart events
@ -79,20 +77,31 @@ public class RecordThread extends Thread {
*/ */
// new io from https://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/ // new io from https://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
final ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize); final ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
while (ic.read(buffer) != -1) {
// prepare the buffer to be drained while(running) {
buffer.flip(); try (InputStream is = getInputStream.apply(recording);
// write to the channel, may block ReadableByteChannel ic = Channels.newChannel(is);
oc.write(buffer); ) {
// If partial transfer, shift remainder down while (ic.read(buffer) != -1) {
// If buffer is empty, same as doing clear() // prepare the buffer to be drained
buffer.compact(); buffer.flip();
} // write to the channel, may block
// EOF will leave buffer in fill state oc.write(buffer);
buffer.flip(); // If partial transfer, shift remainder down
// make sure the buffer is fully drained. // If buffer is empty, same as doing clear()
while (buffer.hasRemaining()) { buffer.compact();
oc.write(buffer); }
// EOF will leave buffer in fill state
buffer.flip();
// make sure the buffer is fully drained.
while (buffer.hasRemaining()) {
oc.write(buffer);
}
} catch (InterruptedIOException | java.nio.channels.ClosedByInterruptException e) {
// ignore
} catch (Throwable e) {
log.error("unknown read exception", e);
}
} }
} catch (InterruptedIOException | java.nio.channels.ClosedByInterruptException e) { } catch (InterruptedIOException | java.nio.channels.ClosedByInterruptException e) {
// ignore // ignore