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,6 +77,11 @@ 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(running) {
try (InputStream is = getInputStream.apply(recording);
ReadableByteChannel ic = Channels.newChannel(is);
) {
while (ic.read(buffer) != -1) { while (ic.read(buffer) != -1) {
// prepare the buffer to be drained // prepare the buffer to be drained
buffer.flip(); buffer.flip();
@ -96,6 +99,12 @@ public class RecordThread extends Thread {
} }
} catch (InterruptedIOException | java.nio.channels.ClosedByInterruptException e) { } catch (InterruptedIOException | java.nio.channels.ClosedByInterruptException e) {
// ignore // ignore
} catch (Throwable e) {
log.error("unknown read exception", e);
}
}
} catch (InterruptedIOException | java.nio.channels.ClosedByInterruptException e) {
// ignore
} catch (Throwable e) { } catch (Throwable e) {
log.error("unknown exception", e); log.error("unknown exception", e);
} }