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:io.airlift.security.der.DerUtils.java

/**
 * Encodes an octet string./*from   w  ww. j  a v a  2s. c  om*/
 */
public static byte[] encodeOctetString(byte[] value) {
    byte[] lengthEncoded = encodeLength(value.length);
    ByteArrayDataOutput out = ByteStreams.newDataOutput(2 + lengthEncoded.length + value.length);
    out.write(OCTET_STRING_TAG);
    out.write(lengthEncoded);
    out.write(value);
    return out.toByteArray();
}

From source file:io.airlift.security.der.DerUtils.java

/**
 * Encodes a sequence of encoded values.
 *//*ww w.j  a v a2s.  c  o  m*/
public static byte[] encodeSequence(byte[]... encodedValues) {
    int length = 0;
    for (byte[] encodedValue : encodedValues) {
        length += encodedValue.length;
    }
    byte[] lengthEncoded = encodeLength(length);

    ByteArrayDataOutput out = ByteStreams.newDataOutput(1 + lengthEncoded.length + length);
    out.write(SEQUENCE_TAG);
    out.write(lengthEncoded);
    for (byte[] entry : encodedValues) {
        out.write(entry);
    }
    return out.toByteArray();
}

From source file:io.airlift.security.der.DerUtils.java

/**
 * Encodes a bit string padded with the specified number of bits.
 * The encoding is a byte containing the padBits followed by the value bytes.
 */// w ww. ja v  a 2  s  .  c  o m
public static byte[] encodeBitString(int padBits, byte[] value) {
    checkArgument(padBits >= 0 && padBits < 8, "Invalid pad bits");

    byte[] lengthEncoded = encodeLength(value.length + 1);
    ByteArrayDataOutput out = ByteStreams.newDataOutput(2 + lengthEncoded.length + value.length);
    out.write(BIT_STRING_TAG);
    out.write(lengthEncoded);
    out.write(padBits);
    out.write(value);
    return out.toByteArray();
}

From source file:io.opencensus.implcore.tags.propagation.SerializationUtils.java

private static final void putVarInt(int input, ByteArrayDataOutput byteArrayDataOutput) {
    byte[] output = new byte[VarInt.varIntSize(input)];
    VarInt.putVarInt(input, output, 0);//from   w w  w  . j av  a 2  s.  co m
    byteArrayDataOutput.write(output);
}

From source file:com.google.devrel.gmscore.tools.apk.arsc.ResourceString.java

/**
 * Encodes a string in either UTF-8 or UTF-16 and returns the bytes of the encoded string.
 * Strings are prefixed by 2 values. The first is the number of characters in the string.
 * The second is the encoding length (number of bytes in the string).
 *
 * <p>Here's an example UTF-8-encoded string of ab:
 * <pre>03 04 61 62 C2 A9 00</pre>
 *
 * @param str The string to be encoded.//from www.jav a  2  s . c  om
 * @param type The encoding type that the {@link ResourceString} should be encoded in.
 * @return The encoded string.
 */
public static byte[] encodeString(String str, Type type) {
    byte[] bytes = str.getBytes(type.charset());
    // The extra 5 bytes is for metadata (character count + byte count) and the NULL terminator.
    ByteArrayDataOutput output = ByteStreams.newDataOutput(bytes.length + 5);
    encodeLength(output, str.length(), type);
    if (type == Type.UTF8) { // Only UTF-8 strings have the encoding length.
        encodeLength(output, bytes.length, type);
    }
    output.write(bytes);
    // NULL-terminate the string
    if (type == Type.UTF8) {
        output.write(0);
    } else {
        output.writeShort(0);
    }
    return output.toByteArray();
}

From source file:shadowmage.ancient_framework.common.utils.ByteTools.java

/**
 * Writes a compressed NBTTagCompound to the OutputStream
 *//*from w  w  w  . j  a  v a  2 s .c  o m*/
public static void writeNBTTagCompound(NBTTagCompound par0NBTTagCompound, ByteArrayDataOutput data) {
    if (par0NBTTagCompound == null) {
        data.writeShort(-1);
    } else {

        byte[] var2;
        try {
            var2 = CompressedStreamTools.compress(par0NBTTagCompound);
            data.writeShort((short) var2.length);
            data.write(var2);
        } catch (IOException e) {
            AWFramework.instance.logError("Severe error writing NBTTagCompound to dataStream");
            e.printStackTrace();
        }
    }
}

From source file:shadowmage.ancient_warfare.common.utils.ByteTools.java

/**
 * Writes a compressed NBTTagCompound to the OutputStream
 *///www. j a  va 2s. c o m
public static void writeNBTTagCompound(NBTTagCompound par0NBTTagCompound, ByteArrayDataOutput data) {
    if (par0NBTTagCompound == null) {
        data.writeShort(-1);
    } else {

        byte[] var2;
        try {
            var2 = CompressedStreamTools.compress(par0NBTTagCompound);
            data.writeShort((short) var2.length);
            data.write(var2);
        } catch (IOException e) {
            Config.logError("Severe error writing NBTTagCompound to dataStream");
            e.printStackTrace();
        }
    }
}

From source file:shadowmage.ancient_warfare.common.utils.NBTWriter.java

public static void writeTagToStream(NBTTagCompound tag, ByteArrayDataOutput data) {
    if (tag == null) {
        data.writeShort(-1);/*from   w w w.  j  ava2  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:micdoodle8.mods.galacticraft.core.network.GCCorePacketManager.java

public static void writeNBTTagCompound(NBTTagCompound tag, ByteArrayDataOutput dataStream) throws IOException {
    if (tag == null) {
        dataStream.writeShort(-1);/*from w w w.j a  v a2 s  .co  m*/
    } else {
        byte[] var2 = CompressedStreamTools.compress(tag);
        dataStream.writeShort((short) var2.length);
        dataStream.write(var2);
    }
}

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);/*www  .  j a v a  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 = serializeObject(value);
        buffer.writeInt(binData.length);
        buffer.write(binData);
    }
    return buffer.toByteArray();
}