- Common ways to obtain LittleEndianDataInputStream
private void myMethod () {LittleEndianDataInputStream l =
InputStream in;new LittleEndianDataInputStream(in)
- Smart code suggestions by Codota
}
private static byte [] getByteArray(LittleEndianDataInputStream ledis) throws IOException { short fieldLength = ledis.readShort(); byte [] value = new byte[fieldLength]; ledis.readFully(value); return value; } }
private static void checkStartBytes(KdbxHeader kdbxHeader, InputStream decryptedInputStream) throws IOException { LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(decryptedInputStream); byte [] startBytes = new byte[32]; ledis.readFully(startBytes); if (!Arrays.equals(startBytes, kdbxHeader.getStreamStartBytes())) { throw new IllegalStateException("Inconsistent stream start bytes. This usually means the credentials were wrong."); } }
private byte[] readNextBlock() throws IOException { int read = stream.read(); if (read < 0) return null; byte[] checksum = new byte[16]; checksum[0] = (byte)read; // checksum - 16 bytes. dataWrapper.readFully(checksum, 1, 15); ClickHouseBlockChecksum expected = ClickHouseBlockChecksum.fromBytes(checksum); // header: // 1 byte - 0x82 (shows this is LZ4) int magic = dataWrapper.readUnsignedByte(); if (magic != MAGIC) throw new IOException("Magic is not correct: " + magic); // 4 bytes - size of the compressed data including 9 bytes of the header int compressedSizeWithHeader = dataWrapper.readInt(); // 4 bytes - size of uncompressed data int uncompressedSize = dataWrapper.readInt(); int compressedSize = compressedSizeWithHeader - 9; //header byte[] block = new byte[compressedSize]; // compressed data: compressed_size - 9 байт. dataWrapper.readFully(block); ClickHouseBlockChecksum real = ClickHouseBlockChecksum.calculateForBlock((byte)magic, compressedSizeWithHeader, uncompressedSize, block, compressedSize); if (!real.equals(expected)) { throw new IllegalArgumentException("Checksum doesn't match: corrupted data."); } byte[] decompressed = new byte[uncompressedSize]; LZ4FastDecompressor decompressor = factory.fastDecompressor(); decompressor.decompress(block, 0, decompressed, 0, uncompressedSize); return decompressed; }
private byte[] readNextBlock() throws IOException { int read = stream.read(); if (read < 0) return null; byte[] checksum = new byte[16]; checksum[0] = (byte)read; // checksum - 16 bytes. dataWrapper.readFully(checksum, 1, 15); ClickHouseBlockChecksum expected = ClickHouseBlockChecksum.fromBytes(checksum); // header: // 1 byte - 0x82 (shows this is LZ4) int magic = dataWrapper.readUnsignedByte(); if (magic != MAGIC) throw new IOException("Magic is not correct: " + magic); // 4 bytes - size of the compressed data including 9 bytes of the header int compressedSizeWithHeader = dataWrapper.readInt(); // 4 bytes - size of uncompressed data int uncompressedSize = dataWrapper.readInt(); int compressedSize = compressedSizeWithHeader - 9; //header byte[] block = new byte[compressedSize]; // compressed data: compressed_size - 9 байт. dataWrapper.readFully(block); ClickHouseBlockChecksum real = ClickHouseBlockChecksum.calculateForBlock((byte)magic, compressedSizeWithHeader, uncompressedSize, block, compressedSize); if (!real.equals(expected)) { throw new IllegalArgumentException("Checksum doesn't match: corrupted data."); } byte[] decompressed = new byte[uncompressedSize]; LZ4FastDecompressor decompressor = factory.fastDecompressor(); decompressor.decompress(block, 0, decompressed, 0, uncompressedSize); return decompressed; }