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

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

Introduction

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

Prototype

@Override
    void writeShort(int v);

Source Link

Usage

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

/**
 * Writes a compressed NBTTagCompound to the OutputStream
 *///w  ww . j  a  v a 2  s  . com
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 .ja v  a  2s .co  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);
    } else {//  w  w  w  .  j a  va2 s.c  o  m
        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);
    } else {/*w  w  w .  j ava  2 s. co  m*/
        byte[] var2 = CompressedStreamTools.compress(tag);
        dataStream.writeShort((short) var2.length);
        dataStream.write(var2);
    }
}

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

/**
 * //from  w  w w .  ja  v  a2 s .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: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 w  w w . j av  a 2 s.com
 * @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: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  w w.  j a  v  a 2  s .  c om
 * @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:net.shadowmage.ancientwarfare.structure.api.NBTTools.java

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

From source file:nxminetilities.network.MultilightToolPacket.java

@Override
public void write(ByteArrayDataOutput out) {
    out.writeShort(is.itemID);
    out.writeByte(is.stackSize);// w w  w  .  ja va 2 s.c  om
    out.writeShort(is.getItemDamage());

    try {
        byte[] abyte;
        abyte = CompressedStreamTools.compress(is.stackTagCompound);
        out.writeShort((short) abyte.length);
        out.write(abyte);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:garmintools.adapters.garmin.MetadataGarminAdapter.java

private void writeMetadata(Proto.Metadata data, ByteArrayDataOutput output) {
    output.writeShort(data.getCycleNumber());
    writeDate(output, data.getEffectiveDate());
    writeDate(output, data.getExpiresDate());
    writeDate(output, data.getAeronauticalDataSnapshotDate());
    output.write(data.getUnknownData1());
    output.writeShort(data.getUnknownData2());
    output.write(data.getUnknownData3());
    output.write(SIMPLE_ENCODING.encode(StringUtil.pad(data.getPartNumber(), PART_NUMBER_LENGTH)));
    writeStringAndPadWithSpace(output, StringUtil.pad(data.getCoverageRegion(), COVERAGE_REGION_PAD_LENGTH),
            COVERAGE_REGION_LENGTH);//from w w  w . j a v  a2 s  . com
    writeStringAndPadWithSpace(output, data.getCopyrightLine1(), COPYRIGHT_LINE_LENGTH);
    writeStringAndPadWithSpace(output, data.getCopyrightLine2(), COPYRIGHT_LINE_LENGTH);
    output.write(data.getUnknownData4());
    for (int i = 0; i < TRAILING_ZERO_BYTE_LENGTH; ++i) {
        output.write(0);
    }
}