com.google.common.io.LittleEndianDataOutputStream Java Examples

The following examples show how to use com.google.common.io.LittleEndianDataOutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MMapGOV4Function.java    From mph-table with Apache License 2.0 6 votes vote down vote up
public static <T> void writeTo(final GOV4Function<T> gov4Function, final String path) throws NoSuchFieldException,
        IllegalAccessException, IOException {
    final Field dataField = gov4Function.getClass().getDeclaredField("data");
    dataField.setAccessible(true);
    final AbstractBitVector.LongBigListView signaturesData = (AbstractBitVector.LongBigListView) dataField.get(gov4Function);
    final Field bitVectorField = signaturesData.getClass().getDeclaredField("bitVector");
    bitVectorField.setAccessible(true);
    final BitVector bitVector = (BitVector)bitVectorField.get(signaturesData);
    try (final LittleEndianDataOutputStream outputStream = new LittleEndianDataOutputStream(
            new BufferedOutputStream(new FileOutputStream(Files.buildPath(path, "signatures.bin"))))) {
        for (final long value : bitVector.bits()) {
            outputStream.writeLong(value);
        }
    }
    dataField.set(gov4Function, null);
    try (final OutputStream outputStream = new FileOutputStream(Files.buildPath(path, "GOV4Function.bin"));
         final ObjectOutput objectOutput = new ObjectOutputStream(outputStream)) {
        objectOutput.writeObject(gov4Function);
    }
}
 
Example #2
Source File: StringPoolChunk.java    From android-arscblamer with Apache License 2.0 6 votes vote down vote up
@Override
protected void writePayload(DataOutput output, ByteBuffer header, int options)
    throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  int stringOffset = 0;
  ByteBuffer offsets = ByteBuffer.allocate(getOffsetSize());
  offsets.order(ByteOrder.LITTLE_ENDIAN);

  // Write to a temporary payload so we can rearrange this and put the offsets first
  LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos);
  try {
    stringOffset = writeStrings(payload, offsets, options);
    writeStyles(payload, offsets, options);
  } finally {
    Closeables.close(payload, true);
  }

  output.write(offsets.array());
  output.write(baos.toByteArray());
  if (!styles.isEmpty()) {
    header.putInt(STYLE_START_OFFSET, getHeaderSize() + getOffsetSize() + stringOffset);
  }
}
 
Example #3
Source File: Chunk.java    From android-arscblamer with Apache License 2.0 6 votes vote down vote up
/**
 * Converts this chunk into an array of bytes representation. Normally you will not need to
 * override this method unless your header changes based on the contents / size of the payload.
 */
@Override
public final byte[] toByteArray(int options) throws IOException {
  ByteBuffer header = ByteBuffer.allocate(getHeaderSize()).order(ByteOrder.LITTLE_ENDIAN);
  writeHeader(header, 0);  // The chunk size isn't known yet. This will be filled in later.
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos);
  try {
    writePayload(payload, header, options);
  } finally {
    Closeables.close(payload, true);
  }

  byte[] payloadBytes = baos.toByteArray();
  int chunkSize = getHeaderSize() + payloadBytes.length;
  header.putInt(CHUNK_SIZE_OFFSET, chunkSize);

  // Combine results
  ByteBuffer result = ByteBuffer.allocate(chunkSize).order(ByteOrder.LITTLE_ENDIAN);
  result.put(header.array());
  result.put(payloadBytes);
  return result.array();
}
 
Example #4
Source File: StringPoolChunk.java    From android-arscblamer with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] toByteArray(int options) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos);
  try {
    for (StringPoolSpan span : spans()) {
      byte[] encodedSpan = span.toByteArray(options);
      if (encodedSpan.length != StringPoolSpan.SPAN_LENGTH) {
        throw new IllegalStateException("Encountered a span of invalid length.");
      }
      payload.write(encodedSpan);
    }
    payload.writeInt(RES_STRING_POOL_SPAN_END);
  } finally {
    Closeables.close(payload, true);
  }

  return baos.toByteArray();
}
 
Example #5
Source File: ByteOpTest.java    From webarchive-commons with Apache License 2.0 6 votes vote down vote up
public void testReadShort() throws IOException {
	byte a[] = new byte[]{0,1,2,3};
	ByteArrayInputStream bais = new ByteArrayInputStream(a);
	int bos = ByteOp.readShort(bais);
	System.out.format("BO.Read short(%d)\n", bos);
	DataInputStream dis = new DataInputStream(new ByteArrayInputStream(a));
	int disv = dis.readUnsignedShort();
	System.out.format("DI.Read short(%d)\n", disv);
	for(int i = 0; i < 256 * 256; i++) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(2);
		LittleEndianDataOutputStream dos = new LittleEndianDataOutputStream(baos);
		dos.writeShort(i);
		ByteArrayInputStream bais2 = new ByteArrayInputStream(baos.toByteArray());
		int gotI = ByteOp.readShort(bais2);
		assertEquals(i, gotI);
	}
}
 
Example #6
Source File: StringPoolChunk.java    From android-chunk-utils with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] toByteArray(boolean shrink) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
    for (StringPoolSpan span : spans()) {
      byte[] encodedSpan = span.toByteArray(shrink);
      if (encodedSpan.length != StringPoolSpan.SPAN_LENGTH) {
        throw new IllegalStateException("Encountered a span of invalid length.");
      }
      payload.write(encodedSpan);
    }
    payload.writeInt(RES_STRING_POOL_SPAN_END);
  }

  return baos.toByteArray();
}
 
Example #7
Source File: StringPoolChunk.java    From android-chunk-utils with Apache License 2.0 6 votes vote down vote up
@Override
protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
    throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  int stringOffset = 0;
  ByteBuffer offsets = ByteBuffer.allocate(getOffsetSize());
  offsets.order(ByteOrder.LITTLE_ENDIAN);

  // Write to a temporary payload so we can rearrange this and put the offsets first
  try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
    stringOffset = writeStrings(payload, offsets, shrink);
    writeStyles(payload, offsets, shrink);
  }

  output.write(offsets.array());
  output.write(baos.toByteArray());
  if (!styles.isEmpty()) {
    header.putInt(STYLE_START_OFFSET, getHeaderSize() + getOffsetSize() + stringOffset);
  }
}
 
Example #8
Source File: Chunk.java    From android-chunk-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Converts this chunk into an array of bytes representation. Normally you will not need to
 * override this method unless your header changes based on the contents / size of the payload.
 */
@Override
public final byte[] toByteArray(boolean shrink) throws IOException {
  ByteBuffer header = ByteBuffer.allocate(getHeaderSize()).order(ByteOrder.LITTLE_ENDIAN);
  writeHeader(header, 0);  // The chunk size isn't known yet. This will be filled in later.
  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
    writePayload(payload, header, shrink);
  }

  byte[] payloadBytes = baos.toByteArray();
  int chunkSize = getHeaderSize() + payloadBytes.length;
  header.putInt(CHUNK_SIZE_OFFSET, chunkSize);

  // Combine results
  ByteBuffer result = ByteBuffer.allocate(chunkSize).order(ByteOrder.LITTLE_ENDIAN);
  result.put(header.array());
  result.put(payloadBytes);
  return result.array();
}
 
Example #9
Source File: StringPoolChunk.java    From apkfile with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] toByteArray(boolean shrink) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
        for (StringPoolSpan span : spans()) {
            byte[] encodedSpan = span.toByteArray(shrink);
            if (encodedSpan.length != StringPoolSpan.SPAN_LENGTH) {
                throw new IllegalStateException("Encountered a span of invalid length.");
            }
            payload.write(encodedSpan);
        }
        payload.writeInt(RES_STRING_POOL_SPAN_END);
    }

    return baos.toByteArray();
}
 
Example #10
Source File: StringPoolChunk.java    From apkfile with Apache License 2.0 6 votes vote down vote up
@Override
protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int stringOffset = 0;
    ByteBuffer offsets = ByteBuffer.allocate(getOffsetSize());
    offsets.order(ByteOrder.LITTLE_ENDIAN);

    // Write to a temporary payload so we can rearrange this and put the offsets first
    try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
        stringOffset = writeStrings(payload, offsets, shrink);
        writeStyles(payload, offsets, shrink);
    }

    output.write(offsets.array());
    output.write(baos.toByteArray());
    if (!styles.isEmpty()) {
        header.putInt(STYLE_START_OFFSET, getHeaderSize() + getOffsetSize() + stringOffset);
    }
}
 
Example #11
Source File: Chunk.java    From apkfile with Apache License 2.0 6 votes vote down vote up
/**
 * Converts this chunk into an array of bytes representation. Normally you will not need to
 * override this method unless your header changes based on the contents / size of the payload.
 */
@Override
public final byte[] toByteArray(boolean shrink) throws IOException {
    ByteBuffer header = ByteBuffer.allocate(getHeaderSize()).order(ByteOrder.LITTLE_ENDIAN);
    writeHeader(header, 0);  // The chunk size isn't known yet. This will be filled in later.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
        writePayload(payload, header, shrink);
    }

    byte[] payloadBytes = baos.toByteArray();
    int chunkSize = getHeaderSize() + payloadBytes.length;
    header.putInt(CHUNK_SIZE_OFFSET, chunkSize);

    // Combine results
    ByteBuffer result = ByteBuffer.allocate(chunkSize).order(ByteOrder.LITTLE_ENDIAN);
    result.put(header.array());
    result.put(payloadBytes);
    return result.array();
}
 
Example #12
Source File: TypeChunk.java    From android-arscblamer with Apache License 2.0 5 votes vote down vote up
@Override
protected void writePayload(DataOutput output, ByteBuffer header, int options)
    throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  ByteBuffer offsets = ByteBuffer.allocate(getOffsetSize()).order(ByteOrder.LITTLE_ENDIAN);
  LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos);
  try {
    writeEntries(payload, offsets, options);
  } finally {
    Closeables.close(payload, true);
  }
  output.write(offsets.array());
  output.write(baos.toByteArray());
}
 
Example #13
Source File: TestWeirdStuff.java    From util with Apache License 2.0 5 votes vote down vote up
private void writeFile() throws IOException {
    mmapFile = new File(tmpDir, "mmap");
    LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(new BufferedOutputStream(new FileOutputStream(mmapFile), 65536));
    //write 8 mb of crap to ensure multiple pages have been written
    for (int i = 0; i < 2 * 1024 * 1024; i++) {
        out.writeInt(i);
    }
    out.close();
}
 
Example #14
Source File: GuavaIOUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenWriteReadLittleEndian_thenCorrect() throws IOException {
    final int value = 100;

    final LittleEndianDataOutputStream writer = new LittleEndianDataOutputStream(new FileOutputStream("src/test/resources/test_le.txt"));
    writer.writeInt(value);
    writer.close();

    final LittleEndianDataInputStream reader = new LittleEndianDataInputStream(new DataInputStream(new FileInputStream("src/test/resources/test_le.txt")));
    final int result = reader.readInt();
    reader.close();

    assertEquals(value, result);
}
 
Example #15
Source File: ImmutableBTreeIndex.java    From lsmtree with Apache License 2.0 5 votes vote down vote up
/**
 * @param path root lsm tree index directory
 * @param iterator the iterator
 * @param keySerializer the key serializer
 * @param valueSerializer the value serializer
 * @param blocksize block size
 * @param keepDeletions true to keep deletion
 * @param <K> the key type
 * @param <V> the value type
 * @throws IOException  if an I/O error occurs
 */
public static <K, V> void write(
        Path path,
        Iterator<Generation.Entry<K,V>> iterator,
        Serializer<K> keySerializer,
        Serializer<V> valueSerializer,
        final int blocksize,
        boolean keepDeletions
) throws IOException {
    if (blocksize > 65536) throw new IllegalArgumentException("block size must be less than 65536");
    Files.createDirectories(path);
    final BufferedFileDataOutputStream fileOut = new BufferedFileDataOutputStream(path.resolve("index.bin"));
    final CountingOutputStream out = new CountingOutputStream(fileOut);
    //tempFile is deleted in writeIndex
    final Path tempPath = Files.createTempFile("tmp", ".bin");
    final WriteLevelResult result = writeLevel(out, tempPath, iterator, keySerializer, valueSerializer, blocksize, keepDeletions);
    final int tmpCount = result.tmpCount;
    final long size = result.size;

    final long valueLevelLength = out.getCount();
    final Header header = writeIndex(out, tempPath, tmpCount, keySerializer, blocksize);
    header.valueLevelLength = valueLevelLength;
    header.size = size;
    header.hasDeletions = keepDeletions;
    new HeaderSerializer().write(header, new LittleEndianDataOutputStream(out));
    fileOut.sync();
    out.close();
}
 
Example #16
Source File: FileSerializationBenchmark.java    From imhotep with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(int[] a, File file) throws IOException {
    LittleEndianDataOutputStream os = new LittleEndianDataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
    for (int val : a) {
        os.writeInt(val);
    }
    os.close();
}
 
Example #17
Source File: BinGenerationTool.java    From riiablo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void generate(FileHandle txt, FileHandle bin, Class<Excel> excelClass) throws Exception {
  Gdx.app.log(TAG, bin.toString());
  Excel<Excel.Entry> excel = Excel.load(excelClass, txt, Excel.EXPANSION);
  OutputStream out = null;
  try {
    out = bin.write(false, 8192);
    LittleEndianDataOutputStream dos = new LittleEndianDataOutputStream(out);
    excel.writeBin(dos);
  } catch (Throwable t) {
    Gdx.app.error(TAG, t.getMessage(), t);
  } finally {
    IOUtils.closeQuietly(out);
  }

  if (VALIDATE_BIN) {
    Class<Excel.Entry> entryClass = getEntryClass(excelClass);
    String binClassName = excelClass.getName() + "Bin";
    Class binClass = Class.forName(binClassName);
    Method equals = binClass.getMethod("equals", entryClass, entryClass);
    Excel binExcel = Excel.load(excelClass, txt, bin, null);
    if (binExcel.size() != excel.size()) Gdx.app.error(TAG, "excel sizes do not match!");
    for (Excel.Entry e1 : excel) {
      Excel.Entry eq = getEqual(equals, e1, binExcel);
      if (eq == null) {
        Gdx.app.log(TAG, "ERROR at index " + e1);
        //break;
      } else {
        //Gdx.app.log(TAG, e1 + "=" + eq);
      }
    }
  }
}
 
Example #18
Source File: TypeChunk.java    From apkfile with Apache License 2.0 5 votes vote down vote up
@Override
protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteBuffer offsets = ByteBuffer.allocate(getOffsetSize()).order(ByteOrder.LITTLE_ENDIAN);
    try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
        writeEntries(payload, offsets, shrink);
    }
    output.write(offsets.array());
    output.write(baos.toByteArray());
}
 
Example #19
Source File: ClickHouseRowBinaryStream.java    From clickhouse-jdbc with Apache License 2.0 5 votes vote down vote up
public ClickHouseRowBinaryStream(OutputStream outputStream, TimeZone timeZone, ClickHouseProperties properties) {
    this.out = new LittleEndianDataOutputStream(outputStream);
    if (properties.isUseServerTimeZoneForDates()) {
        this.timeZone = timeZone;
    } else {
        this.timeZone = TimeZone.getDefault();
    }
}
 
Example #20
Source File: TableWriter.java    From mph-table with Apache License 2.0 5 votes vote down vote up
private static void writeLongs(final File outputFile, final long[] values) throws IOException {
    try (final LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(new BufferedFileDataOutputStream(outputFile))) {
        for (final long value : values) {
            out.writeLong(value);
        }
        out.flush();
    }
    outputFile.setReadOnly();
}
 
Example #21
Source File: TableWriter.java    From mph-table with Apache License 2.0 5 votes vote down vote up
private static <K, V> void writeToIndexedOffsets(
        final File inputData,
        final File outputData,
        final File outputOffsets,
        final TableMeta<K, V> meta,
        final Iterable<Pair<K, V>> entries,
        final long dataSize) throws IOException {
    final long numEntries = meta.numEntries();
    final int offsetSize = meta.getConfig().bytesPerOffset(numEntries, dataSize);
    final long totalOffsetSize = numEntries * offsetSize;
    final BufferedFileDataOutputStream fileOut = new BufferedFileDataOutputStream(outputData);
    final CountingOutputStream countOut = new CountingOutputStream(fileOut);
    final long startMillis = System.currentTimeMillis();
    try (final MMapBuffer offsets = new MMapBuffer(outputOffsets, 0L, totalOffsetSize, FileChannel.MapMode.READ_WRITE, ByteOrder.nativeOrder());
         final LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(countOut)) {
        for (final Pair<K, V> e : entries) {
            final long hash = meta.getHash(e.getFirst());
            if (hash < 0) {
                throw new IOException("inconsistent mph, known key hashed to -1: " + e.getFirst());
            }
            final long offset = countOut.getCount();
            if (offsetSize == 2) {
                offsets.memory().putShort(hash * 2L, (short) offset);
            } else if (offsetSize == 4) {
                offsets.memory().putInt(hash * 4L, (int) offset);
            } else {
                offsets.memory().putLong(hash * 8L, offset);
            }
            meta.getConfig().write(e.getFirst(), e.getSecond(), out);
        }
        offsets.sync(0L, totalOffsetSize);
        out.flush();
    }
    outputData.setReadOnly();
    outputOffsets.setReadOnly();
    LOGGER.info("wrote " + numEntries + " offsets for " + dataSize + " bytes of data in " +
                (System.currentTimeMillis() - startMillis) + " ms");
}
 
Example #22
Source File: TableWriter.java    From mph-table with Apache License 2.0 5 votes vote down vote up
/**
 * As above, using a one-time iterator.  The entries are written
 * to local temp data, making this suitable for use e.g. when
 * reading from a slow source such as hdfs.
 *
 * @param <K> key type
 * @param <V> value type
 * @param outputDir directory to write the hash table files to
 * @param config    a {@link TableConfig} specifying at least a key serializer
 * @param entries   an iterable of key-value Pairs representing entries in the table
 * @param tempDir   directory to write temporary files to
 * @throws IOException if unable to write the files or serialize the data
 */
public static <K, V> void writeWithTempStorage(
        final File outputDir,
        final TableConfig<K, V> config,
        final Iterator<Pair<K, V>> entries,
        final File tempDir) throws IOException {
    if (!config.isValid()) {
        throw new IOException("invalid table config: " + config);
    }
    ensureOutputDirectory(tempDir);
    final File tempDataFile = File.createTempFile("tmp_entries", ".bin", tempDir);
    final BufferedFileDataOutputStream fileOut = new BufferedFileDataOutputStream(tempDataFile);
    long dataSize = 0;
    try (final LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(fileOut)) {
        while (entries.hasNext()) {
            final Pair<K, V> e = entries.next();
            if (e.getFirst() == null || (e.getSecond() == null && config.getValueSerializer() != null)) {
                throw new IllegalArgumentException("can't store nulls: " + e);
            }
            dataSize += config.sizeOf(e.getFirst(), e.getSecond());
            config.getKeySerializer().write(e.getFirst(), out); // write here even if implicit
            if (config.getValueSerializer() != null) {
                config.getValueSerializer().write(e.getSecond(), out);
            }
        }
    }
    try {
        final Iterable<Pair<K, V>> tempEntries =
            new SerializedKeyValueIterable(tempDataFile, config.getKeySerializer(), config.getValueSerializer());
        write(outputDir, config, tempEntries, dataSize);
    } finally {
        tempDataFile.delete();
    }
}
 
Example #23
Source File: TypeChunk.java    From android-chunk-utils with Apache License 2.0 5 votes vote down vote up
@Override
protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
    throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  ByteBuffer offsets = ByteBuffer.allocate(getOffsetSize()).order(ByteOrder.LITTLE_ENDIAN);
  try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
    writeEntries(payload, offsets, shrink);
  }
  output.write(offsets.array());
  output.write(baos.toByteArray());
}
 
Example #24
Source File: KdbxSerializer.java    From KeePassJava2 with Apache License 2.0 4 votes vote down vote up
private static void writeStartBytes(KdbxHeader kdbxHeader, OutputStream encryptedOutputStream) throws IOException {
    LittleEndianDataOutputStream ledos = new LittleEndianDataOutputStream(encryptedOutputStream);
    ledos.write(kdbxHeader.getStreamStartBytes());
}
 
Example #25
Source File: KdbxSerializer.java    From KeePassJava2 with Apache License 2.0 4 votes vote down vote up
/**
 * Write a KdbxHeader to the output stream supplied. The header is updated with the
 * message digest of the written stream.
 * @param kdbxHeader the header to write and update
 * @param outputStream the output stream
 * @throws IOException on error
 */
public static void writeKdbxHeader(KdbxHeader kdbxHeader, OutputStream outputStream) throws IOException {
    MessageDigest messageDigest = Encryption.getMessageDigestInstance();
    DigestOutputStream digestOutputStream = new DigestOutputStream(outputStream, messageDigest);
    LittleEndianDataOutputStream ledos = new LittleEndianDataOutputStream(digestOutputStream);

    // write the magic number
    ledos.writeInt(SIG1);
    ledos.writeInt(SIG2);
    // write a file version
    ledos.writeInt(FILE_VERSION_32);

    ledos.writeByte(HeaderType.CIPHER_ID);
    ledos.writeShort(16);
    byte[] b = new byte[16];
    ByteBuffer bb = ByteBuffer.wrap(b);
    bb.putLong(kdbxHeader.getCipherUuid().getMostSignificantBits());
    bb.putLong(8, kdbxHeader.getCipherUuid().getLeastSignificantBits());
    ledos.write(b);

    ledos.writeByte(HeaderType.COMPRESSION_FLAGS);
    ledos.writeShort(4);
    ledos.writeInt(kdbxHeader.getCompressionFlags().ordinal());

    ledos.writeByte(HeaderType.MASTER_SEED);
    ledos.writeShort(kdbxHeader.getMasterSeed().length);
    ledos.write(kdbxHeader.getMasterSeed());

    ledos.writeByte(HeaderType.TRANSFORM_SEED);
    ledos.writeShort(kdbxHeader.getTransformSeed().length);
    ledos.write(kdbxHeader.getTransformSeed());

    ledos.writeByte(HeaderType.TRANSFORM_ROUNDS);
    ledos.writeShort(8);
    ledos.writeLong(kdbxHeader.getTransformRounds());

    ledos.writeByte(HeaderType.ENCRYPTION_IV);
    ledos.writeShort(kdbxHeader.getEncryptionIv().length);
    ledos.write(kdbxHeader.getEncryptionIv());

    ledos.writeByte(HeaderType.PROTECTED_STREAM_KEY);
    ledos.writeShort(kdbxHeader.getProtectedStreamKey().length);
    ledos.write(kdbxHeader.getProtectedStreamKey());

    ledos.writeByte(HeaderType.STREAM_START_BYTES);
    ledos.writeShort(kdbxHeader.getStreamStartBytes().length);
    ledos.write(kdbxHeader.getStreamStartBytes());

    ledos.writeByte(HeaderType.INNER_RANDOM_STREAM_ID);
    ledos.writeShort(4);
    ledos.writeInt(kdbxHeader.getProtectedStreamAlgorithm().ordinal());

    ledos.writeByte(HeaderType.END);
    ledos.writeShort(0);

    MessageDigest digest = digestOutputStream.getMessageDigest();
    kdbxHeader.setHeaderHash(digest.digest());
}
 
Example #26
Source File: ClickHouseLZ4OutputStream.java    From clickhouse-jdbc with Apache License 2.0 4 votes vote down vote up
public ClickHouseLZ4OutputStream(OutputStream stream, int maxCompressBlockSize) {
    dataWrapper = new LittleEndianDataOutputStream(stream);
    compressor = factory.fastCompressor();
    currentBlock = new byte[maxCompressBlockSize];
    compressedBlock = new byte[compressor.maxCompressedLength(maxCompressBlockSize)];
}
 
Example #27
Source File: ImmutableBTreeIndex.java    From lsmtree with Apache License 2.0 4 votes vote down vote up
private static <K,V> WriteLevelResult writeLevel(
        final CountingOutputStream counter,
        final Path tempPath,
        final Iterator<Generation.Entry<K,V>> iterator,
        final Serializer<K> keySerializer,
        final Serializer<V> valueSerializer,
        final int blocksize,
        final boolean keepDeletions
) throws IOException {
    Generation.Entry<K,V> next;
    if (!iterator.hasNext()) {
        return new WriteLevelResult(0, 0);
    }
    next = iterator.next();
    final LittleEndianDataOutputStream tmpOut = new LittleEndianDataOutputStream(new BufferedOutputStream(Files.newOutputStream(tempPath), 131072));
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    final LittleEndianDataOutputStream bufferDataOutput = new LittleEndianDataOutputStream(buffer);
    final ByteArrayOutputStream currentBlock = new ByteArrayOutputStream(blocksize);
    final CharArrayList keyOffsets = new CharArrayList();
    int tmpCount = 0;
    boolean done = false;
    final LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(counter);
    long count = 0;
    outer: while (!done) {

        currentBlock.reset();
        keyOffsets.clear();
        if (!keepDeletions) {
            while (next.isDeleted()) {
                if (!iterator.hasNext()) break outer;
                next = iterator.next();
            }
        }
        keySerializer.write(next.getKey(), tmpOut);
        tmpOut.writeLong(counter.getCount());
        tmpCount++;
        while (true) {
            buffer.reset();
            final boolean skipDeleted = updateBuffer(next, keySerializer, valueSerializer, keepDeletions, bufferDataOutput);
            if (4+2*keyOffsets.size()+2+currentBlock.size()+buffer.size() > blocksize) {
                if (currentBlock.size() == 0) {
                    throw new IllegalArgumentException("key value pair is greater than block size");
                }
                break;
            }
            if (!skipDeleted) {
                keyOffsets.add((char)currentBlock.size());
                buffer.writeTo(currentBlock);
                count++;
            }
            if (!iterator.hasNext()) {
                done = true;
                break;
            }
            next = iterator.next();
        }
        if (keyOffsets.size() > 0) {
            final long start = counter.getCount();
            out.writeInt(keyOffsets.size());
            for (int i = 0; i < keyOffsets.size(); i++) {
                out.writeChar(keyOffsets.getChar(i));
            }
            currentBlock.writeTo(out);
            if (counter.getCount()-start > blocksize) {
                log.error("too big");
            }
        }
    }
    tmpOut.close();
    return new WriteLevelResult(tmpCount, count);
}
 
Example #28
Source File: OutputStreamSliceOutput.java    From hive-dwrf with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
public OutputStreamSliceOutput(OutputStream outputStream)
{
    countingOutputStream = new CountingOutputStream(outputStream);
    dataOutputStream = new LittleEndianDataOutputStream(countingOutputStream);
}
 
Example #29
Source File: NBTOutputStream.java    From WorldPainter with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a new {@code NBTOutputStream}, which will write data to the
 * specified underlying output stream.
 *
 * @param os The output stream.
 * @param littleEndian Whether the data should be written in little endian
 *                     byte order.
 */
public NBTOutputStream(OutputStream os, boolean littleEndian) {
    this.os = littleEndian ? new LittleEndianDataOutputStream(os) : new DataOutputStream(os);
}