private void flushBuffers() throws IOException { checkNotClosed(); if (this.count > 0) { final CharBuffer cb = CharBuffer.wrap(this.buffer); cb.limit(this.count); this.buffers.add(cb); } this.emitter.emit(this.buffers); if (!this.buffers.isEmpty()) { this.buffer = this.buffers.get(0).array(); this.buffers.clear(); } this.count = 0; this.threshold = BUFFER_SIZE; }
private void writeAndTryFlush(final char c) throws IOException { this.buffer[this.count++] = c; if (c == this.delimiter) { flushBuffers(); } else if (this.count == this.buffer.length) { checkNotClosed(); this.buffers.add(CharBuffer.wrap(this.buffer)); this.buffer = new char[BUFFER_SIZE]; this.count = 0; this.threshold = 0; } }
@Override public void write(final char[] cbuf, int off, int len) throws IOException { final int available = this.threshold - this.count; if (available >= len) { System.arraycopy(cbuf, off, this.buffer, this.count, len); this.count += len; return; } if (available > 0) { System.arraycopy(cbuf, off, this.buffer, this.count, available); this.count += available; off += available; len -= available; } final int end = off + len; while (off < end) { writeAndTryFlush(cbuf[off++]); } }
@Override public void write(final String str, int off, int len) throws IOException { final int available = this.threshold - this.count; final int end = off + len; if (available >= len) { str.getChars(off, end, this.buffer, this.count); this.count += len; return; } if (available > 0) { str.getChars(off, off + available, this.buffer, this.count); this.count += available; off += available; len -= available; } while (off < end) { writeAndTryFlush(str.charAt(off++)); } }
@Override public void close() throws IOException { synchronized (this.buffers) { if (this.closed) { return; } flushBuffers(); this.closed = true; } this.buffers.clear(); this.buffer = null; this.emitter.close(); this.emitter = null; }
public static Writer parallelBuffer(final Writer writer, final char delimiter) { return new ParallelBufferedWriter(writer, delimiter); }
@Override public void flush() throws IOException { flushBuffers(); }
@Override public void write(final int c) throws IOException { if (this.count < this.threshold) { this.buffer[this.count++] = (char) c; } else { writeAndTryFlush((char) c); } }