Example usage for com.google.common.io ByteArrayDataOutput writeLong

List of usage examples for com.google.common.io ByteArrayDataOutput writeLong

Introduction

In this page you can find the example usage for com.google.common.io ByteArrayDataOutput writeLong.

Prototype

@Override
    void writeLong(long v);

Source Link

Usage

From source file:org.locationtech.geogig.storage.postgresql.PGId.java

public static PGId valueOf(final int h1, final long h2, final long h3) {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeInt(h1);/*from ww  w.  j a  va 2  s . co m*/
    out.writeLong(h2);
    out.writeLong(h3);
    byte[] raw = out.toByteArray();
    return new PGId(raw);
}

From source file:io.druid.indexer.InputRowSerde.java

public static final byte[] toBytes(final InputRow row, AggregatorFactory[] aggs) {
    try {/*w  w w . ja  v a2s . c o m*/
        ByteArrayDataOutput out = ByteStreams.newDataOutput();

        //write timestamp
        out.writeLong(row.getTimestampFromEpoch());

        //writing all dimensions
        List<String> dimList = row.getDimensions();

        Text[] dims = EMPTY_TEXT_ARRAY;
        if (dimList != null) {
            dims = new Text[dimList.size()];
            for (int i = 0; i < dims.length; i++) {
                dims[i] = new Text(dimList.get(i));
            }
        }
        StringArrayWritable sw = new StringArrayWritable(dims);
        sw.write(out);

        MapWritable mw = new MapWritable();

        if (dimList != null) {
            for (String dim : dimList) {
                List<String> dimValue = row.getDimension(dim);

                if (dimValue == null || dimValue.size() == 0) {
                    continue;
                }

                if (dimValue.size() == 1) {
                    mw.put(new Text(dim), new Text(dimValue.get(0)));
                } else {
                    Text[] dimValueArr = new Text[dimValue.size()];
                    for (int i = 0; i < dimValueArr.length; i++) {
                        dimValueArr[i] = new Text(dimValue.get(i));
                    }
                    mw.put(new Text(dim), new StringArrayWritable(dimValueArr));
                }
            }
        }

        //writing all metrics
        Supplier<InputRow> supplier = new Supplier<InputRow>() {
            @Override
            public InputRow get() {
                return row;
            }
        };
        for (AggregatorFactory aggFactory : aggs) {
            String k = aggFactory.getName();

            Aggregator agg = aggFactory
                    .factorize(IncrementalIndex.makeColumnSelectorFactory(aggFactory, supplier, true));
            agg.aggregate();

            String t = aggFactory.getTypeName();

            if (t.equals("float")) {
                mw.put(new Text(k), new FloatWritable(agg.getFloat()));
            } else if (t.equals("long")) {
                mw.put(new Text(k), new LongWritable(agg.getLong()));
            } else {
                //its a complex metric
                Object val = agg.get();
                ComplexMetricSerde serde = getComplexMetricSerde(t);
                mw.put(new Text(k), new BytesWritable(serde.toBytes(val)));
            }
        }

        mw.write(out);
        return out.toByteArray();
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}

From source file:org.apache.druid.indexer.InputRowSerde.java

public static final SerializeResult toBytes(final Map<String, IndexSerdeTypeHelper> typeHelperMap,
        final InputRow row, AggregatorFactory[] aggs) {
    try {//from  w w  w . ja v a 2s  .c  o m
        List<String> parseExceptionMessages = new ArrayList<>();
        ByteArrayDataOutput out = ByteStreams.newDataOutput();

        //write timestamp
        out.writeLong(row.getTimestampFromEpoch());

        //writing all dimensions
        List<String> dimList = row.getDimensions();

        WritableUtils.writeVInt(out, dimList.size());
        for (String dim : dimList) {
            IndexSerdeTypeHelper typeHelper = typeHelperMap.get(dim);
            if (typeHelper == null) {
                typeHelper = STRING_HELPER;
            }
            writeString(dim, out);

            try {
                typeHelper.serialize(out, row.getRaw(dim));
            } catch (ParseException pe) {
                parseExceptionMessages.add(pe.getMessage());
            }
        }

        //writing all metrics
        Supplier<InputRow> supplier = () -> row;
        WritableUtils.writeVInt(out, aggs.length);
        for (AggregatorFactory aggFactory : aggs) {
            String k = aggFactory.getName();
            writeString(k, out);

            try (Aggregator agg = aggFactory.factorize(IncrementalIndex
                    .makeColumnSelectorFactory(VirtualColumns.EMPTY, aggFactory, supplier, true))) {
                try {
                    agg.aggregate();
                } catch (ParseException e) {
                    // "aggregate" can throw ParseExceptions if a selector expects something but gets something else.
                    log.debug(e, "Encountered parse error, skipping aggregator[%s].", k);
                    parseExceptionMessages.add(e.getMessage());
                }

                String t = aggFactory.getTypeName();
                if (agg.isNull()) {
                    out.writeByte(NullHandling.IS_NULL_BYTE);
                } else {
                    out.writeByte(NullHandling.IS_NOT_NULL_BYTE);
                    if ("float".equals(t)) {
                        out.writeFloat(agg.getFloat());
                    } else if ("long".equals(t)) {
                        WritableUtils.writeVLong(out, agg.getLong());
                    } else if ("double".equals(t)) {
                        out.writeDouble(agg.getDouble());
                    } else {
                        //its a complex metric
                        Object val = agg.get();
                        ComplexMetricSerde serde = getComplexMetricSerde(t);
                        writeBytes(serde.toBytes(val), out);
                    }
                }
            }
        }

        return new SerializeResult(out.toByteArray(), parseExceptionMessages);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:cherry.foundation.crypto.SecureLongEncoder.java

@Override
protected byte[] typeToBytes(Long p) {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeLong(p.longValue());
    return out.toByteArray();
}

From source file:org.wso2.carbon.analytics.data.commons.utils.AnalyticsCommonUtils.java

public static byte[] encodeElement(String name, Object value) throws AnalyticsException {
    ByteArrayDataOutput buffer = ByteStreams.newDataOutput();
    byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
    buffer.writeInt(nameBytes.length);/* w w w  .jav a 2s  .c o m*/
    buffer.write(nameBytes);
    if (value instanceof String) {
        buffer.write(DATA_TYPE_STRING);
        String strVal = (String) value;
        byte[] strBytes = strVal.getBytes(StandardCharsets.UTF_8);
        buffer.writeInt(strBytes.length);
        buffer.write(strBytes);
    } else if (value instanceof Long) {
        buffer.write(DATA_TYPE_LONG);
        buffer.writeLong((Long) value);
    } else if (value instanceof Double) {
        buffer.write(DATA_TYPE_DOUBLE);
        buffer.writeDouble((Double) value);
    } else if (value instanceof Boolean) {
        buffer.write(DATA_TYPE_BOOLEAN);
        boolean boolVal = (Boolean) value;
        if (boolVal) {
            buffer.write(BOOLEAN_TRUE);
        } else {
            buffer.write(BOOLEAN_FALSE);
        }
    } else if (value instanceof Integer) {
        buffer.write(DATA_TYPE_INTEGER);
        buffer.writeInt((Integer) value);
    } else if (value instanceof Float) {
        buffer.write(DATA_TYPE_FLOAT);
        buffer.writeFloat((Float) value);
    } else if (value instanceof byte[]) {
        buffer.write(DATA_TYPE_BINARY);
        byte[] binData = (byte[]) value;
        buffer.writeInt(binData.length);
        buffer.write(binData);
    } else if (value == null) {
        buffer.write(DATA_TYPE_NULL);
    } else {
        buffer.write(DATA_TYPE_OBJECT);
        byte[] binData = serializeObject(value);
        buffer.writeInt(binData.length);
        buffer.write(binData);
    }
    return buffer.toByteArray();
}

From source file:com.facebook.presto.raptor.RaptorColumnIdentity.java

@Override
public byte[] serialize() {
    ByteArrayDataOutput output = newDataOutput(SERIALIZED_SIZE);
    output.write(CURRENT_VERSION);/*from ww w .j a  v  a2  s .co  m*/
    output.writeLong(columnId);
    return output.toByteArray();
}

From source file:com.facebook.presto.raptor.RaptorTableIdentity.java

@Override
public byte[] serialize() {
    ByteArrayDataOutput output = newDataOutput(SERIALIZED_SIZE);
    output.write(CURRENT_VERSION);/*from w  w  w.j  a  va  2 s  .  c  om*/
    output.writeLong(tableId);
    return output.toByteArray();
}

From source file:com.volumetricpixels.rockyplugin.chunk.ChunkCacheHandler.java

/**
 * /*from w  w  w  .  j av  a2s  . c o  m*/
 * @param buffer
 * @throws IOException
 */
public static byte[] handleCompression(String playerName, byte[] buffer) throws IOException {
    Set<Long> playerCache = cache.getPlayerCache(playerName);

    // Each chunk sended is handled by:
    // - BlockType: Whole byte per block
    // - BlockMetaData: Half byte per block
    // - BlockLight: Half byte per block
    // - SkyLight: Half byte per block (Only of handleLight is TRUE)
    // - AddArray: Half byte per block (Only if extraMask has the bit,
    // support for FORGE)
    // - BiomeArray: Whole byte per XZ coordinate (Only if isContinous is
    // TRUE)
    int chunkLen = buffer.length / ChunkCache.CHUNK_PARTITION_SIZE;
    if ((chunkLen & 0x7FF) != 0) {
        chunkLen++;
    }

    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    byte[] chunkData = new byte[ChunkCache.CHUNK_PARTITION_SIZE];

    // Write the magic number
    out.writeInt(ChunkCache.CHUNK_MAGIC_NUMBER);

    // Write the number of section inside the packet
    out.writeShort(chunkLen);

    // For each CHUNK_PARTITION_SIZE block, check the hash of it.
    for (int i = 0; i < chunkLen; i++) {
        int index = i * ChunkCache.CHUNK_PARTITION_SIZE;
        int length = ChunkCache.CHUNK_PARTITION_SIZE;

        if (index + ChunkCache.CHUNK_PARTITION_SIZE > buffer.length) {
            length = buffer.length - index;
        }

        // Calculate the hash of the current block
        System.arraycopy(buffer, index, chunkData, 0x0000, length);
        long hash = ChunkCache.calculateHash(chunkData);

        // Write the hash into the packet
        out.writeLong(hash);

        // Add the hash into the player cache
        boolean isPresent = playerCache.add(hash);

        // Writes the length of the section
        out.writeShort(isPresent ? length : 0);

        // Check for the chunk with the player cache
        if (isPresent) {
            // Writes the data of the section
            out.write(chunkData);
            statCacheMiss.incrementAndGet();
        } else {
            statCacheHit.incrementAndGet();
        }
        statTotal.incrementAndGet();
    }
    return out.toByteArray();
}

From source file:pw.simplyintricate.bitcoin.models.datastructures.NetworkAddress.java

public byte[] toByteArray() {
    ByteArrayDataOutput writer = ByteStreams.newDataOutput();

    //writer.writeInt(EndianUtils.swapInteger(time));
    writer.writeLong(EndianUtils.swapLong(services.longValue()));
    // write the two ipv4 to ipv6 pads
    for (int i = 0; i < 10; i++) {
        writer.writeByte(0);/*from w ww . j  av  a2s.c  o m*/
    }
    writer.writeByte(0xFF);
    writer.writeByte(0xFF);
    writer.write(ipAddress);
    writer.writeShort(port);

    return writer.toByteArray();
}

From source file:org.wso2.carbon.analytics.datasource.core.util.GenericUtils.java

public static byte[] encodeElement(String name, Object value) throws AnalyticsException {
    ByteArrayDataOutput buffer = ByteStreams.newDataOutput();
    byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
    buffer.writeInt(nameBytes.length);/*from  w  w  w  .  ja  va  2  s.c  o m*/
    buffer.write(nameBytes);
    if (value instanceof String) {
        buffer.write(DATA_TYPE_STRING);
        String strVal = (String) value;
        byte[] strBytes = strVal.getBytes(StandardCharsets.UTF_8);
        buffer.writeInt(strBytes.length);
        buffer.write(strBytes);
    } else if (value instanceof Long) {
        buffer.write(DATA_TYPE_LONG);
        buffer.writeLong((Long) value);
    } else if (value instanceof Double) {
        buffer.write(DATA_TYPE_DOUBLE);
        buffer.writeDouble((Double) value);
    } else if (value instanceof Boolean) {
        buffer.write(DATA_TYPE_BOOLEAN);
        boolean boolVal = (Boolean) value;
        if (boolVal) {
            buffer.write(BOOLEAN_TRUE);
        } else {
            buffer.write(BOOLEAN_FALSE);
        }
    } else if (value instanceof Integer) {
        buffer.write(DATA_TYPE_INTEGER);
        buffer.writeInt((Integer) value);
    } else if (value instanceof Float) {
        buffer.write(DATA_TYPE_FLOAT);
        buffer.writeFloat((Float) value);
    } else if (value instanceof byte[]) {
        buffer.write(DATA_TYPE_BINARY);
        byte[] binData = (byte[]) value;
        buffer.writeInt(binData.length);
        buffer.write(binData);
    } else if (value == null) {
        buffer.write(DATA_TYPE_NULL);
    } else {
        buffer.write(DATA_TYPE_OBJECT);
        byte[] binData = GenericUtils.serializeObject(value);
        buffer.writeInt(binData.length);
        buffer.write(binData);
    }
    return buffer.toByteArray();
}