Example usage for java.io ObjectOutput writeBytes

List of usage examples for java.io ObjectOutput writeBytes

Introduction

In this page you can find the example usage for java.io ObjectOutput writeBytes.

Prototype

void writeBytes(String s) throws IOException;

Source Link

Document

Writes a string to the output stream.

Usage

From source file:com.facebook.presto.bloomfilter.BloomFilter.java

public Slice serialize() {
    byte[] bytes = new byte[0];
    byte[] bytesPre = new byte[0];
    try {/* w  ww .  j  a v  a  2 s. c  o  m*/
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutput output = new ObjectOutputStream(buffer);
        output.writeObject(instance);
        bytes = buffer.toByteArray();

        ByteArrayOutputStream bufferPre = new ByteArrayOutputStream();
        ObjectOutput outputPre = new ObjectOutputStream(bufferPre);
        outputPre.writeObject(instancePreFilter);
        bytesPre = bufferPre.toByteArray();
    } catch (Exception ix) {
        log.error(ix);
    }

    // Create hash
    byte[] bfHash = Hashing.sha256().hashBytes(bytes).asBytes();

    // Compress
    byte[] compressed;
    try {
        compressed = compress(bytes);
    } catch (IOException ix) {
        log.error(ix);
        compressed = new byte[0];
    }
    int size = compressed.length;

    // Compress
    byte[] compressedPre;
    try {
        compressedPre = compress(bytesPre);
    } catch (IOException ix) {
        log.error(ix);
        compressedPre = new byte[0];
    }
    int sizePre = compressedPre.length;

    // To slice
    DynamicSliceOutput output = new DynamicSliceOutput(size);

    // Write hash
    output.writeBytes(bfHash); // 32 bytes

    // Write the length of the bloom filter
    output.appendInt(size);

    // Write the length of the pre bloom filter
    output.appendInt(sizePre);

    // Params
    output.appendInt(expectedInsertions);
    output.appendDouble(falsePositivePercentage);

    // Write the bloom filter
    output.appendBytes(compressed);

    // Write the bloom filter
    output.appendBytes(compressedPre);

    return output.slice();
}