@Override public SliceOutput appendBytes(byte[] source) { writeBytes(source); return this; }
@Override public void close() throws IOException { if (!closed) { closed = true; super.close(); } }
@Override public void writeZero(int length) { checkArgument(length >= 0, "length must be 0 or greater than 0."); while (length > 0) { int batch = ensureBatchSize(length); Arrays.fill(buffer, bufferPosition, bufferPosition + batch, (byte) 0); bufferPosition += batch; length -= batch; } }
private void ensureWritableBytes(int minWritableBytes) { if (minWritableBytes > getFreeBufferLength()) { flushBufferToOutputStream(); } }
@Override public void writeBytes(Slice source, int sourceIndex, int length) { if (length >= CHUNK_SIZE) { if (bufferPosition > 0) { // fill up the current buffer int flushLength = getFreeBufferLength(); slice.setBytes(bufferPosition, source, sourceIndex, flushLength); bufferPosition = CHUNK_SIZE; flushBufferToOutputStream(); sourceIndex += flushLength; length -= flushLength; } // line up the chunk to chunk size and flush directly to OutputStream while (length >= CHUNK_SIZE) { writeToOutputStream(source, sourceIndex, CHUNK_SIZE); sourceIndex += CHUNK_SIZE; length -= CHUNK_SIZE; bufferOffset += CHUNK_SIZE; } } if (length > 0) { // buffer the remaining data ensureWritableBytes(length); slice.setBytes(bufferPosition, source, sourceIndex, length); bufferPosition += length; } }
BufferedOutputStreamSliceOutput output = new BufferedOutputStreamSliceOutput(byteOutputStream); for (int i = 0; i < offsets.length - 1; i++) { output.writeBytes(inputArray, offsets[i], offsets[i + 1] - offsets[i]); output.flush(); assertEquals(byteOutputStream.toByteArray(), inputArray); byteOutputStream.close(); output = new BufferedOutputStreamSliceOutput(byteOutputStream); for (int i = 0; i < offsets.length - 1; i++) { output.writeBytes(inputSlice, offsets[i], offsets[i + 1] - offsets[i]); output.flush(); assertEquals(byteOutputStream.toByteArray(), inputArray); byteOutputStream.close();
private int ensureBatchSize(int length) { ensureWritableBytes(Math.min(CHUNK_SIZE, length)); return Math.min(length, CHUNK_SIZE - bufferPosition); }
@Override public void close() throws IOException { try { flushBufferToOutputStream(); } finally { outputStream.close(); } }
@Override public String toString(Charset charset) { return toString(); }
@Override public long getRetainedSize() { return super.getRetainedSize() + bufferedOutput.getRetainedSize(); }
@Override public void writeBytes(byte[] source, int sourceIndex, int length) { if (length >= CHUNK_SIZE) { if (bufferPosition > 0) { // fill up the current buffer int flushLength = getFreeBufferLength(); slice.setBytes(bufferPosition, source, sourceIndex, flushLength); bufferPosition = CHUNK_SIZE; flushBufferToOutputStream(); sourceIndex += flushLength; length -= flushLength; } // line up the chunk to chunk size and flush directly to OutputStream while (length >= CHUNK_SIZE) { writeToOutputStream(source, sourceIndex, CHUNK_SIZE); sourceIndex += CHUNK_SIZE; length -= CHUNK_SIZE; bufferOffset += CHUNK_SIZE; } } if (length > 0) { // buffer the remaining data ensureWritableBytes(length); slice.setBytes(bufferPosition, source, sourceIndex, length); bufferPosition += length; } }
BufferedOutputStreamSliceOutput output = new BufferedOutputStreamSliceOutput(byteOutputStream); for (int i = 0; i < offsets.length - 1; i++) { output.writeBytes(inputArray, offsets[i], offsets[i + 1] - offsets[i]); output.flush(); assertEquals(byteOutputStream.toByteArray(), inputArray); byteOutputStream.close(); output = new BufferedOutputStreamSliceOutput(byteOutputStream); for (int i = 0; i < offsets.length - 1; i++) { output.writeBytes(inputSlice, offsets[i], offsets[i + 1] - offsets[i]); output.flush(); assertEquals(byteOutputStream.toByteArray(), inputArray); byteOutputStream.close();
@Override public void writeByte(int value) { ensureWritableBytes(SIZE_OF_BYTE); slice.setByte(bufferPosition, value); bufferPosition += SIZE_OF_BYTE; }
private void ensureWritableBytes(int minWritableBytes) { if (minWritableBytes > getFreeBufferLength()) { flushBufferToOutputStream(); } }
@Override public void flush() throws IOException { flushBufferToOutputStream(); outputStream.flush(); }
@Override public String toString(Charset charset) { return toString(); }
@Override public long getRetainedSize() { return super.getRetainedSize() + bufferedOutput.getRetainedSize(); }
@Override public void writeBytes(Slice source, int sourceIndex, int length) { if (length >= CHUNK_SIZE) { if (bufferPosition > 0) { // fill up the current buffer int flushLength = getFreeBufferLength(); slice.setBytes(bufferPosition, source, sourceIndex, flushLength); bufferPosition = CHUNK_SIZE; flushBufferToOutputStream(); sourceIndex += flushLength; length -= flushLength; } // line up the chunk to chunk size and flush directly to OutputStream while (length >= CHUNK_SIZE) { writeToOutputStream(source, sourceIndex, CHUNK_SIZE); sourceIndex += CHUNK_SIZE; length -= CHUNK_SIZE; bufferOffset += CHUNK_SIZE; } } if (length > 0) { // buffer the remaining data ensureWritableBytes(length); slice.setBytes(bufferPosition, source, sourceIndex, length); bufferPosition += length; } }
@Override public SliceOutput appendBytes(Slice slice) { writeBytes(slice); return this; }
@Override public void writeShort(int value) { ensureWritableBytes(SIZE_OF_SHORT); slice.setShort(bufferPosition, value); bufferPosition += SIZE_OF_SHORT; }