1
0
mirror of https://github.com/moparisthebest/rcrdit synced 2025-03-11 06:50:11 -04: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) {
log.debug("recording started {}", recording);
File outputFile = null;
try (InputStream is = getInputStream.apply(recording);
OutputStream os = new FileOutputStream(outputFile = recording.getNewFileToWrite(ext), true);
ReadableByteChannel ic = Channels.newChannel(is);
try (OutputStream os = new FileOutputStream(outputFile = recording.getNewFileToWrite(ext), true);
WritableByteChannel oc = Channels.newChannel(os);
) {
// output file exists now, start onStart events
@ -79,6 +77,11 @@ public class RecordThread extends Thread {
*/
// new io from https://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
final ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
while(running) {
try (InputStream is = getInputStream.apply(recording);
ReadableByteChannel ic = Channels.newChannel(is);
) {
while (ic.read(buffer) != -1) {
// prepare the buffer to be drained
buffer.flip();
@ -96,6 +99,12 @@ public class RecordThread extends Thread {
}
} catch (InterruptedIOException | java.nio.channels.ClosedByInterruptException e) {
// ignore
} catch (Throwable e) {
log.error("unknown read exception", e);
}
}
} catch (InterruptedIOException | java.nio.channels.ClosedByInterruptException e) {
// ignore
} catch (Throwable e) {
log.error("unknown exception", e);
}