@Override public FileWriteTaskResult call() throws FileNotFoundException, IOException { FileChannel openedChannel; if (channel == null) { openedChannel = openChannel(file); } else { openedChannel = channel; } buffer.flipFloats(); openedChannel.write(buffer.asByteBuffer()); buffer.clear(); FileWriteTaskResult result = new FileWriteTaskResult(openedChannel, buffer); return result; }
/** * Stops writing and starts to close the output file. * * <p>Note: this function is non-blocking and can be called from within the process tread. */ public WriterResult stop() { synchronized (processingLock) { if (!started) { throw new RuntimeException("Attempt to stop altough not started."); } startBuffer.flipFloats(); Future<FileChannel> readerChannel = null; FileChannel channel = null; if (startBufferDone) { // wait for the bussyBufferProvider to terminate streaming. // ToDo: this ought to be done inside LastFileWriteTask (beware of possible deadlocks) if (bussyBufferProvider != null) { if (!bussyBufferProvider.isDone()) { logger.warning("Waiting for a previous buffer to be streamed."); overflowCount++; } try { channel = bussyBufferProvider.get().getChannel(); } catch (InterruptedException | ExecutionException ex) { logger.log(Level.SEVERE, null, ex); } } FileWriteTaskResult result = null; try { result = currentBufferProvider.get(); } catch (InterruptedException | ExecutionException ex) { logger.log(Level.SEVERE, null, ex); } if (result != null) { SyncBuffer buffer = result.getbuffer(); LastFileWriteTask lastFileWriteTask = new LastFileWriteTask(outputFile, channel, buffer); readerChannel = executor.submit(lastFileWriteTask); } } WriterResult result = new WriterResult(startBuffer, readerChannel, samplesProcessed); currentBufferProvider = null; bussyBufferProvider = null; startBuffer = null; started = false; return result; } }