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

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

Introduction

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

Prototype

@Override
    void write(byte b[]);

Source Link

Usage

From source file:org.tyrannyofheaven.bukkit.util.ToHMessageUtils.java

/**
 * Sends a plugin message to BungeeCord, signing it with zPermissions as 
 * the channel and any information to write to it.
 * /*from  w ww. jav  a  2  s  .com*/
 * @param plugin plugin to send with
 * @param channel sub channel to send to
 * @param objects objects to write
 */
public static void sendPluginMessage(Plugin plugin, String channel, String... objects) {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeUTF("zPermissions");
    out.writeUTF(channel);

    try (ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
            DataOutputStream msgout = new DataOutputStream(msgbytes)) {
        for (String s : objects) {
            msgout.writeUTF(s);
        }

        byte[] byteArray = msgbytes.toByteArray();
        out.writeShort(byteArray.length);
        out.write(byteArray);
        for (Player player : Bukkit.getOnlinePlayers()) {
            player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
            return;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.drill.exec.cache.local.LocalCache.java

private static BytesHolder serialize(Object obj, SerializationMode mode) {
    if (obj instanceof String) {
        return new BytesHolder(((String) obj).getBytes(Charsets.UTF_8));
    }/*ww  w  .jav  a 2  s. com*/
    try {
        switch (mode) {

        case DRILL_SERIALIZIABLE: {
            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            OutputStream outputStream = DataOutputOutputStream.constructOutputStream(out);
            ((DrillSerializable) obj).writeToStream(outputStream);
            outputStream.flush();
            return new BytesHolder(out.toByteArray());
        }

        case JACKSON: {
            ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out.write(mapper.writeValueAsBytes(obj));
            return new BytesHolder(out.toByteArray());
        }

        case PROTOBUF:
            return new BytesHolder(((Message) obj).toByteArray());

        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    throw new UnsupportedOperationException();
}

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 ww .  java 2s.c  om*/
    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();
}

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

public static byte[] encodeRecordValues(Map<String, Object> values) throws AnalyticsException {
    ByteArrayDataOutput byteOut = ByteStreams.newDataOutput();
    String name;//from w w w . j a  va2 s. co m
    Object value;
    for (Map.Entry<String, Object> entry : values.entrySet()) {
        name = entry.getKey();
        value = entry.getValue();
        byteOut.write(encodeElement(name, value));
    }
    return byteOut.toByteArray();
}

From source file:net.shadowmage.ancientwarfare.structure.api.NBTTools.java

public static void writeTagToStream(NBTTagCompound tag, ByteArrayDataOutput data) {
    if (tag == null) {
        data.writeShort(-1);// w w  w .  j a v a2 s. c  o  m
    } else {
        byte[] var2;
        try {
            var2 = CompressedStreamTools.compress(tag);
            data.writeShort((short) var2.length);
            data.write(var2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

@Override
protected byte[] typeToBytes(BigDecimal p) {
    int scale = p.scale();
    BigInteger bi = p.scaleByPowerOfTen(scale).toBigIntegerExact();
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeInt(scale);/*  ww  w  . j  a  va 2 s  .  co m*/
    out.write(bi.toByteArray());
    return out.toByteArray();
}

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

@Override
public byte[] serialize() {
    ByteArrayDataOutput output = newDataOutput(SERIALIZED_SIZE);
    output.write(CURRENT_VERSION);
    output.writeLong(columnId);//from www .  j a  v  a  2 s  . c  o m
    return output.toByteArray();
}

From source file:cherry.goods.crypto.DefaultVersionStrategy.java

@Override
public byte[] encode(byte[] data, Integer version) {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeInt(version.intValue());//  w w  w .ja v  a  2  s  .  c o  m
    out.write(data);
    return out.toByteArray();
}

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

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

From source file:org.xdi.oxauth.service.fido.u2f.RawAuthenticationService.java

private byte[] packBytesToSign(byte[] appIdHash, byte userPresence, long counter, byte[] challengeHash) {
    ByteArrayDataOutput encoded = ByteStreams.newDataOutput();
    encoded.write(appIdHash);
    encoded.write(userPresence);//from   ww  w  . j ava2 s  .  co  m
    encoded.writeInt((int) counter);
    encoded.write(challengeHash);

    return encoded.toByteArray();
}