@Override public void writeBytes(InputStream in, int length) throws IOException { while (length > 0) { int batch = tryEnsureBatchSize(length); slice.setBytes(bufferPosition, in, batch); bufferPosition += batch; length -= batch; } }
@Override public void writeBytes(InputStream in, int length) throws IOException { while (length > 0) { int batch = ensureBatchSize(length); slice.setBytes(bufferPosition, in, batch); bufferPosition += batch; length -= batch; } }
@Override public void writeBytes(Slice source, int sourceIndex, int length) { while (length > 0) { int batch = tryEnsureBatchSize(length); slice.setBytes(bufferPosition, source, sourceIndex, batch); bufferPosition += batch; sourceIndex += batch; length -= batch; } }
@Override public void writeBytes(Slice source, int sourceIndex, int length) { while (length > 0) { int batch = tryEnsureBatchSize(length); slice.setBytes(bufferPosition, source, sourceIndex, batch); bufferPosition += batch; sourceIndex += batch; length -= batch; } }
@Override public void writeBytes(InputStream in, int length) throws IOException { while (length > 0) { int batch = tryEnsureBatchSize(length); slice.setBytes(bufferPosition, in, batch); bufferPosition += batch; length -= batch; } }
@Override public void writeBytes(InputStream in, int length) throws IOException { while (length > 0) { int batch = ensureBatchSize(length); slice.setBytes(bufferPosition, in, batch); bufferPosition += batch; length -= batch; } }
@Override public void writeBytes(byte[] source, int sourceIndex, int length) { while (length > 0) { int batch = tryEnsureBatchSize(length); slice.setBytes(bufferPosition, source, sourceIndex, batch); bufferPosition += batch; sourceIndex += batch; length -= batch; } }
@Override public void writeBytes(byte[] source, int sourceIndex, int length) { while (length > 0) { int batch = tryEnsureBatchSize(length); slice.setBytes(bufferPosition, source, sourceIndex, batch); bufferPosition += batch; sourceIndex += batch; length -= batch; } }
public static Slice pack(BigInteger unscaledValue, Slice result) { pack(0, 0, false, result); byte[] bytes = unscaledValue.abs().toByteArray(); if (bytes.length > UNSCALED_DECIMAL_128_SLICE_LENGTH || (bytes.length == UNSCALED_DECIMAL_128_SLICE_LENGTH && (bytes[0] & SIGN_BYTE_MASK) != 0)) { throwOverflowException(); } // convert to little-endian order reverse(bytes); result.setBytes(0, bytes); if (unscaledValue.signum() < 0) { setNegative(result, true); } throwIfOverflows(result); return result; }
@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 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; } }
@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 decompress(Slice compressed, Slice uncompressed) throws RcFileCorruptionException { checkState(!destroyed, "Codec has been destroyed"); decompressor.reset(); try (CompressionInputStream decompressorStream = codec.createInputStream(compressed.getInput(), decompressor)) { uncompressed.setBytes(0, decompressorStream, uncompressed.length()); } catch (IndexOutOfBoundsException | IOException e) { throw new RcFileCorruptionException(e, "Compressed stream is truncated"); } }
@Override public void decompress(Slice compressed, Slice uncompressed) throws RcFileCorruptionException { try (CompressionInputStream decompressorStream = codec.createInputStream(compressed.getInput())) { uncompressed.setBytes(0, decompressorStream, uncompressed.length()); } catch (IndexOutOfBoundsException | IOException e) { throw new RcFileCorruptionException(e, "Compressed stream is truncated"); } }
public static byte[] serializeModels(Model... models) { List<byte[]> serializedModels = new ArrayList<>(); int size = SIZE_OF_INT + SIZE_OF_INT * models.length; for (Model model : models) { byte[] bytes = serialize(model).getBytes(); size += bytes.length; serializedModels.add(bytes); } Slice slice = Slices.allocate(size); slice.setInt(0, models.length); for (int i = 0; i < models.length; i++) { slice.setInt(SIZE_OF_INT * (i + 1), serializedModels.get(i).length); } int offset = SIZE_OF_INT + SIZE_OF_INT * models.length; for (byte[] bytes : serializedModels) { slice.setBytes(offset, bytes); offset += bytes.length; } return slice.getBytes(); }
public static Slice padSpaces(Slice slice, int length) { int textLength = countCodePoints(slice); // if our string is bigger than requested then truncate if (textLength > length) { throw new IllegalArgumentException("pad length is smaller than slice length"); } // if our target length is the same as our string then return our string if (textLength == length) { return slice; } // preallocate the result int bufferSize = slice.length() + length - textLength; Slice buffer = Slices.allocate(bufferSize); // fill in the existing string buffer.setBytes(0, slice); // fill padding spaces for (int i = slice.length(); i < bufferSize; ++i) { buffer.setByte(i, ' '); } return buffer; }
@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; } }
@Description("concatenates given character strings") @ScalarFunction @LiteralParameters({"x", "y", "u"}) @Constraint(variable = "u", expression = "x + y") @SqlType("char(u)") public static Slice concat(@LiteralParameter("x") Long x, @SqlType("char(x)") Slice left, @SqlType("char(y)") Slice right) { int rightLength = right.length(); if (rightLength == 0) { return left; } Slice paddedLeft = padSpaces(left, x.intValue()); int leftLength = paddedLeft.length(); Slice result = Slices.allocate(leftLength + rightLength); result.setBytes(0, paddedLeft); result.setBytes(leftLength, right); return result; } }
private static List<Long> getSyncPositionsBruteForce(RcFileReader recordReader, File file) { Slice slice = Slices.allocate((int) file.length()); try (InputStream in = new FileInputStream(file)) { slice.setBytes(0, in, slice.length()); } catch (IOException e) { throw new UncheckedIOException(e); } List<Long> syncPositionsBruteForce = new ArrayList<>(); Slice sync = Slices.allocate(SIZE_OF_INT + SIZE_OF_LONG + SIZE_OF_LONG); sync.setInt(0, -1); sync.setBytes(SIZE_OF_INT, recordReader.getSync()); long syncPosition = 0; while (syncPosition >= 0) { syncPosition = slice.indexOf(sync, (int) syncPosition); if (syncPosition > 0) { syncPositionsBruteForce.add(syncPosition); syncPosition++; } } return syncPositionsBruteForce; }