@Override public void detachInputStream() { PMS.get().getFrame().setReadValue(0, ""); if (attachedThread != null) { attachedThread.setReadyToStop(true); } Runnable checkEnd = new Runnable() { @Override public void run() { try { Thread.sleep(CHECK_END_OF_PROCESS); } catch (InterruptedException e) { LOGGER.error(null, e); } if (attachedThread != null && attachedThread.isReadyToStop()) { if (!attachedThread.isDestroyed()) { attachedThread.stopProcess(); } reset(); } } }; new Thread(checkEnd, "Buffered IO End Checker").start(); }
@Override public int read(boolean firstRead, long readCount) { if (readCount > INITIAL_BUFFER_SIZE && readCount < maxMemorySize) { int newMargin = maxMemorySize - MARGIN_MEDIUM; if (bufferOverflowWarning != newMargin) { LOGGER.debug("Setting margin to 2Mb"); } this.bufferOverflowWarning = newMargin; } if (eof && readCount >= writeCount) { return -1; } int c = 0; int minBufferS = firstRead ? minMemorySize : secondread_minsize; while (writeCount - readCount <= minBufferS && !eof && c < 15) { if (c == 0) { LOGGER.trace("Suspend Read: readCount=" + readCount + " / writeCount=" + writeCount); } c++; try { Thread.sleep(CHECK_INTERVAL); } catch (InterruptedException e) { } } if (attachedThread != null) { attachedThread.setReadyToStop(false); } if (c > 0) { LOGGER.trace("Resume Read: readCount=" + readCount + " / writeCount=" + writeCount); } if (buffer == null || !buffered) { return -1; } try { return 0xff & buffer[(int) (readCount % maxMemorySize)]; } catch (ArrayIndexOutOfBoundsException ex) { LOGGER.error("Buffer read ArrayIndexOutOfBoundsException error.", ex); LOGGER.error("buffer.length: " + formatter.format(buffer.length) + " bytes."); LOGGER.error("readCount: \"" + readCount + "\""); LOGGER.error("maxMemorySize: \"" + maxMemorySize + "\""); return -1; } }
@Override public InputStream getInputStream(long newReadPosition) { if (attachedThread != null) { attachedThread.setReadyToStop(false); } WaitBufferedInputStream atominputStream; if (!configuration.getTrancodeBlocksMultipleConnections() || getCurrentInputStream() == null) { atominputStream = new WaitBufferedInputStream(this); inputStreams.add(atominputStream); } else { if (configuration.getTrancodeKeepFirstConnections()) { LOGGER.debug( "BufferedOutputFile is already attached to an InputStream: " + getCurrentInputStream()); } else { // Ditlew - fixes the above (the above iterator breaks on items getting close, cause they // will remove them self from the arraylist) while (inputStreams.size() > 0) { try { inputStreams.get(0).close(); } catch (IOException e) { LOGGER.error("Error: ", e); } } inputStreams.clear(); atominputStream = new WaitBufferedInputStream(this); inputStreams.add(atominputStream); LOGGER.debug("Reassign inputstream: " + getCurrentInputStream()); } return null; } if (newReadPosition > 0) { LOGGER.debug("Setting InputStream new position to: " + formatter.format(newReadPosition)); atominputStream.setReadCount(newReadPosition); } return atominputStream; }
@Override public int read(boolean firstRead, long readCount, byte buf[], int off, int len) { if (readCount > INITIAL_BUFFER_SIZE && readCount < maxMemorySize) { int newMargin = maxMemorySize - MARGIN_MEDIUM; if (bufferOverflowWarning != newMargin) { LOGGER.debug("Setting margin to 2Mb"); } this.bufferOverflowWarning = newMargin; } if (eof && readCount >= writeCount) { return -1; } int c = 0; int minBufferS = firstRead ? minMemorySize : secondread_minsize; while (writeCount - readCount <= minBufferS && !eof && c < 15) { if (c == 0) { LOGGER.trace("Suspend Read: readCount=" + readCount + " / writeCount=" + writeCount); } c++; try { Thread.sleep(CHECK_INTERVAL); } catch (InterruptedException e) { } } if (attachedThread != null) { attachedThread.setReadyToStop(false); } if (c > 0) { LOGGER.trace("Resume Read: readCount=" + readCount + " / writeCount=" + writeCount); } if (buffer == null || !buffered) { return -1; } int mb = (int) (readCount % maxMemorySize); int endOF = buffer.length; int cut = 0; if (eof && (writeCount - readCount) < len) { cut = (int) (len - (writeCount - readCount)); } if (mb >= endOF - len) { try { System.arraycopy(buffer, mb, buf, off, endOF - mb - cut); } catch (ArrayIndexOutOfBoundsException ex) { LOGGER.error("Something went wrong with the buffer.", ex); LOGGER.error("buffer.length: " + formatter.format(buffer.length) + " bytes."); LOGGER.error("mb: " + mb); LOGGER.error("buf.length: " + formatter.format(buf.length) + " bytes."); LOGGER.error("off: " + off); LOGGER.error("endOF - mb - cut: " + (endOF - mb - cut)); } return endOF - mb; } else { System.arraycopy(buffer, mb, buf, off, len - cut); return len; } }