@Override public void flush() { flushBufferToOutputStream(); }
@Override public void close() { flushBufferToOutputStream(); }
@Override public void flush() { flushBufferToOutputStream(); }
@Override public void close() { flushBufferToOutputStream(); }
private void ensureWritableBytes(int minWritableBytes) { int neededBufferSize = bufferPosition + minWritableBytes; if (neededBufferSize <= slice.length()) { return; } if (slice.length() >= maxBufferSize) { flushBufferToOutputStream(); return; } // grow the buffer size int newBufferSize = min(max(slice.length() * 2, minWritableBytes), maxBufferSize); if (neededBufferSize <= newBufferSize) { // we have capacity in the new buffer; just copy the data to the new buffer byte[] previousBuffer = buffer; buffer = new byte[newBufferSize]; slice = wrappedBuffer(buffer); System.arraycopy(previousBuffer, 0, buffer, 0, bufferPosition); } else { // there is no enough capacity in the new buffer; flush the data and allocate the new buffer flushBufferToOutputStream(); buffer = new byte[newBufferSize]; slice = wrappedBuffer(buffer); } }
private void ensureWritableBytes(int minWritableBytes) { int neededBufferSize = bufferPosition + minWritableBytes; if (neededBufferSize <= slice.length()) { return; } if (slice.length() >= maxBufferSize) { flushBufferToOutputStream(); return; } // grow the buffer size int newBufferSize = min(max(slice.length() * 2, minWritableBytes), maxBufferSize); if (neededBufferSize <= newBufferSize) { // we have capacity in the new buffer; just copy the data to the new buffer byte[] previousBuffer = buffer; buffer = new byte[newBufferSize]; slice = wrappedBuffer(buffer); System.arraycopy(previousBuffer, 0, buffer, 0, bufferPosition); } else { // there is no enough capacity in the new buffer; flush the data and allocate the new buffer flushBufferToOutputStream(); buffer = new byte[newBufferSize]; slice = wrappedBuffer(buffer); } }
@Override public void writeBytes(byte[] source, int sourceIndex, int length) { // Write huge chunks direct to OutputStream if (length >= DIRECT_FLUSH_SIZE) { // todo fill buffer before flushing flushBufferToOutputStream(); writeDirectlyToOutputStream(source, sourceIndex, length); bufferOffset += length; } else { ensureWritableBytes(length); slice.setBytes(bufferPosition, source, sourceIndex, length); bufferPosition += length; } }
@Override public void writeBytes(byte[] source, int sourceIndex, int length) { // Write huge chunks direct to OutputStream if (length >= DIRECT_FLUSH_SIZE) { // todo fill buffer before flushing flushBufferToOutputStream(); writeDirectlyToOutputStream(source, sourceIndex, length); bufferOffset += length; } else { ensureWritableBytes(length); slice.setBytes(bufferPosition, source, sourceIndex, length); bufferPosition += length; } }
@Override public void writeBytes(Slice source, int sourceIndex, int length) { // Write huge chunks direct to OutputStream if (length >= DIRECT_FLUSH_SIZE) { flushBufferToOutputStream(); writeDirectlyToOutputStream((byte[]) source.getBase(), sourceIndex + (int) (source.getAddress() - ARRAY_BYTE_BASE_OFFSET), length); bufferOffset += length; } else { ensureWritableBytes(length); slice.setBytes(bufferPosition, source, sourceIndex, length); bufferPosition += length; } }
@Override public void writeBytes(Slice source, int sourceIndex, int length) { // Write huge chunks direct to OutputStream if (length >= DIRECT_FLUSH_SIZE) { flushBufferToOutputStream(); writeDirectlyToOutputStream((byte[]) source.getBase(), sourceIndex + (int) (source.getAddress() - ARRAY_BYTE_BASE_OFFSET), length); bufferOffset += length; } else { ensureWritableBytes(length); slice.setBytes(bufferPosition, source, sourceIndex, length); bufferPosition += length; } }