|
| 1 | +package com.clickhouse.client.api.internal; |
| 2 | + |
| 3 | +import com.clickhouse.client.api.ClientException; |
| 4 | +import com.clickhouse.data.ClickHouseByteUtils; |
| 5 | +import com.clickhouse.data.ClickHouseCityHash; |
| 6 | +import net.jpountz.lz4.LZ4FastDecompressor; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import java.io.EOFException; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.nio.ByteBuffer; |
| 14 | + |
| 15 | +public class ClickHouseLZ4InputStream extends InputStream { |
| 16 | + |
| 17 | + private static Logger LOG = LoggerFactory.getLogger(ClickHouseLZ4InputStream.class); |
| 18 | + private final LZ4FastDecompressor decompressor; |
| 19 | + |
| 20 | + private final InputStream in; |
| 21 | + |
| 22 | + private ByteBuffer buffer; |
| 23 | + |
| 24 | + private byte[] tmpBuffer = new byte[1]; |
| 25 | + |
| 26 | + |
| 27 | + public ClickHouseLZ4InputStream(InputStream in, LZ4FastDecompressor decompressor) { |
| 28 | + super(); |
| 29 | + this.decompressor = decompressor; |
| 30 | + this.in = in; |
| 31 | + this.buffer = ByteBuffer.allocate(8192); |
| 32 | + this.buffer.limit(0); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public int read() throws IOException { |
| 37 | + int n = read(tmpBuffer, 0, 1); |
| 38 | + return n == -1 ? -1 : tmpBuffer[0] & 0xFF; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public int read(byte[] b, int off, int len) throws IOException { |
| 43 | + if (b == null) { |
| 44 | + throw new NullPointerException("b is null"); |
| 45 | + } else if (off < 0) { |
| 46 | + throw new IndexOutOfBoundsException("off is negative"); |
| 47 | + } else if (len < 0) { |
| 48 | + throw new IndexOutOfBoundsException("len is negative"); |
| 49 | + } else if (off + len > b.length) { |
| 50 | + throw new IndexOutOfBoundsException("off + len is greater than b.length"); |
| 51 | + } else if (len == 0) { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + int readBytes = 0; |
| 56 | + do { |
| 57 | + int remaining = Math.min(len - readBytes, buffer.remaining()); |
| 58 | + buffer.get(b, off + readBytes, remaining); |
| 59 | + readBytes += remaining; |
| 60 | + } while (readBytes < len && refill() != -1); |
| 61 | + |
| 62 | + return readBytes == 0 ? -1 : readBytes; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + static final byte MAGIC = (byte) 0x82; |
| 67 | + static final int HEADER_LENGTH = 25; |
| 68 | + |
| 69 | + static final byte[] headerBuff = new byte[HEADER_LENGTH]; |
| 70 | + |
| 71 | + private int refill() throws IOException { |
| 72 | + |
| 73 | + // read header |
| 74 | + int readBytes = in.read(headerBuff, 0, HEADER_LENGTH); |
| 75 | + if (readBytes == -1) { |
| 76 | + return -1; |
| 77 | + } else if (readBytes < HEADER_LENGTH) { |
| 78 | + throw new IOException("Unexpected end of stream"); |
| 79 | + } |
| 80 | + |
| 81 | + if (headerBuff[16] != MAGIC) { |
| 82 | + // 1 byte - 0x82 (shows this is LZ4) |
| 83 | + throw new ClientException("Invalid LZ4 magic byte: '" + headerBuff[16] + "'"); |
| 84 | + } |
| 85 | + |
| 86 | + // 4 bytes - size of the compressed data including 9 bytes of the header |
| 87 | + int compressedSizeWithHeader = getInt32(headerBuff, 17); |
| 88 | + // 4 bytes - size of uncompressed data |
| 89 | + int uncompressedSize = getInt32(headerBuff, 21); |
| 90 | + |
| 91 | + int offset = 9; |
| 92 | + final byte[] block = new byte[compressedSizeWithHeader]; |
| 93 | + block[0] = MAGIC; |
| 94 | + setInt32(block, 1, compressedSizeWithHeader); |
| 95 | + setInt32(block, 5, uncompressedSize); |
| 96 | + // compressed data: compressed_size - 9 bytes |
| 97 | + int remaining = compressedSizeWithHeader - offset; |
| 98 | + readBytes = in.read(block, offset, remaining); |
| 99 | + if (readBytes == -1) { |
| 100 | + throw new EOFException("Unexpected end of stream"); |
| 101 | + } else if (readBytes < remaining) { |
| 102 | + throw new ClientException("Read bytes: " + readBytes + " but expected: " + remaining); |
| 103 | + } |
| 104 | + |
| 105 | + long[] real = ClickHouseCityHash.cityHash128(block, 0, compressedSizeWithHeader); |
| 106 | + if (real[0] != getInt64(headerBuff, 0) || real[1] != ClickHouseByteUtils.getInt64(headerBuff, 8)) { |
| 107 | + throw new ClientException("Corrupted stream: checksum mismatch"); |
| 108 | + } |
| 109 | + |
| 110 | + if (buffer.capacity() < uncompressedSize) { |
| 111 | + buffer = ByteBuffer.allocate(uncompressedSize); |
| 112 | + LOG.warn("Buffer size is too small, reallocate buffer with size: " + uncompressedSize); |
| 113 | + } |
| 114 | + decompressor.decompress(ByteBuffer.wrap(block), offset, buffer, 0, uncompressedSize); |
| 115 | + buffer.position(0); |
| 116 | + buffer.limit(uncompressedSize); |
| 117 | + return uncompressedSize; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Read int32 Little Endian |
| 122 | + * @param bytes |
| 123 | + * @param offset |
| 124 | + * @return |
| 125 | + */ |
| 126 | + private int getInt32(byte[] bytes, int offset) { |
| 127 | + return (0xFF & bytes[offset]) | ((0xFF & bytes[offset + 1]) << 8) | ((0xFF & bytes[offset + 2]) << 16) |
| 128 | + | ((0xFF & bytes[offset + 3]) << 24); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Read int64 Little Endian |
| 133 | + * @param bytes |
| 134 | + * @param offset |
| 135 | + * @return |
| 136 | + */ |
| 137 | + public long getInt64(byte[] bytes, int offset) { |
| 138 | + return (0xFFL & bytes[offset]) | ((0xFFL & bytes[offset + 1]) << 8) | ((0xFFL & bytes[offset + 2]) << 16) |
| 139 | + | ((0xFFL & bytes[offset + 3]) << 24) | ((0xFFL & bytes[offset + 4]) << 32) |
| 140 | + | ((0xFFL & bytes[offset + 5]) << 40) | ((0xFFL & bytes[offset + 6]) << 48) |
| 141 | + | ((0xFFL & bytes[offset + 7]) << 56); |
| 142 | + } |
| 143 | + |
| 144 | + public void setInt32(byte[] bytes, int offset, int value) { |
| 145 | + bytes[offset] = (byte) (0xFF & value); |
| 146 | + bytes[offset + 1] = (byte) (0xFF & (value >> 8)); |
| 147 | + bytes[offset + 2] = (byte) (0xFF & (value >> 16)); |
| 148 | + bytes[offset + 3] = (byte) (0xFF & (value >> 24)); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void close() throws IOException { |
| 153 | + super.close(); |
| 154 | + } |
| 155 | +} |
0 commit comments