Example usage for com.google.common.io ByteArrayDataInput readFully

List of usage examples for com.google.common.io ByteArrayDataInput readFully

Introduction

In this page you can find the example usage for com.google.common.io ByteArrayDataInput readFully.

Prototype

@Override
    void readFully(byte b[]);

Source Link

Usage

From source file:cpw.mods.fml.common.network.ModIdMapPacket.java

@Override
public FMLPacket consumePacket(byte[] data) {
    ByteArrayDataInput bdi = ByteStreams.newDataInput(data);
    int chunkIdx = UnsignedBytes.toInt(bdi.readByte());
    int chunkTotal = UnsignedBytes.toInt(bdi.readByte());
    int chunkLength = bdi.readInt();
    if (partials == null) {
        partials = new byte[chunkTotal][];
    }// w  w w . j a va  2  s  . c o m
    partials[chunkIdx] = new byte[chunkLength];
    bdi.readFully(partials[chunkIdx]);
    for (int i = 0; i < partials.length; i++) {
        if (partials[i] == null) {
            return null;
        }
    }
    return this;
}

From source file:net.minecraftforge.gradle.user.patcherUser.TaskApplyBinPatches.java

private ClassPatch readPatch(JarEntry patchEntry, JarInputStream jis) throws IOException {
    log("\t%s", patchEntry.getName());
    ByteArrayDataInput input = ByteStreams.newDataInput(ByteStreams.toByteArray(jis));

    String name = input.readUTF();
    String sourceClassName = input.readUTF();
    String targetClassName = input.readUTF();
    boolean exists = input.readBoolean();
    int inputChecksum = 0;
    if (exists) {
        inputChecksum = input.readInt();
    }//from  w ww  .  ja v a 2 s . com
    int patchLength = input.readInt();
    byte[] patchBytes = new byte[patchLength];
    input.readFully(patchBytes);

    return new ClassPatch(name, sourceClassName, targetClassName, exists, inputChecksum, patchBytes);
}

From source file:com.volumetricpixels.rockyplugin.packet.RockyPacketHandler.java

/**
 * /*from   w  ww.j a va 2  s . c o  m*/
 * @param data
 * @return
 */
public com.volumetricpixels.rockyapi.packet.Packet readMessagePlugin(byte[] data) {
    com.volumetricpixels.rockyapi.packet.Packet packet = null;
    ByteArrayDataInput bis = ByteStreams.newDataInput(data);

    int packetID = 0;
    int length = 0;

    try {
        packetID = bis.readShort();
        length = bis.readShort();

        packet = PacketType.getPacketFromId(packetID).getClazz().newInstance();
    } catch (InstantiationException e) {
        RockyManager.printConsole("Failed to identify packet id: ", packetID);
    } catch (IllegalAccessException e) {
        RockyManager.printConsole("Failed to identify packet id: ", packetID);
    }
    try {
        byte[] dataPacket = new byte[length];
        bis.readFully(dataPacket);

        PacketInputStream in = new PacketInputStream(ByteBuffer.wrap(dataPacket));
        packet.readData(in);
    } catch (IOException ex) {
        RockyManager.printConsole("------------------------");
        RockyManager.printConsole(
                "Unexpected Exception: " + PacketType.getPacketFromId(packetID) + ", " + packetID);
        RockyManager.printConsole(ex.getMessage());
        RockyManager.printConsole("------------------------");
    }
    return packet;
}

From source file:com.demigodsrpg.chitchat.bungee.BungeeChitchat.java

@EventHandler(priority = EventPriority.HIGHEST)
public void onMessage(final PluginMessageEvent event) {
    if (event.getTag().equals("BungeeCord")) {
        // Get origin server
        ServerInfo origin = Iterables.find(getProxy().getServers().values(), new Predicate<ServerInfo>() {
            @Override/*w w  w. ja  v  a2  s .co m*/
            public boolean apply(ServerInfo serverInfo) {
                return serverInfo.getAddress().equals(event.getSender().getAddress());
            }
        }, null);

        // If origin doesn't exist (impossible?) throw an exception
        if (origin == null) {
            throw new NullPointerException("Origin server for a chat message was null.");
        }

        // Get the data
        ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());

        // Needed for it to work because whynot
        String messageType = in.readUTF();
        String targetServer = in.readUTF();

        // Ignore messages that aren't the correct type
        if (!"Forward".equals(messageType)) {
            return;
        }

        // Get the chat channel and speaking player
        String chatChannelRaw = in.readUTF();

        // Don't continue if this message isn't from chitchat
        if (!chatChannelRaw.startsWith("chitchat")) {
            return;
        }

        // Grab the speaking player and clean up the chat channel name
        String speakingPlayerName = chatChannelRaw.split("\\$")[1];
        String chatChannel = chatChannelRaw.split("\\$")[2];

        // Is this a mute update?
        if (chatChannelRaw.startsWith("chatchatmute$")) {
            MUTED_PLAYERS.put(chatChannel, speakingPlayerName.toLowerCase());
            return;
        }

        // Is this an unmute update?
        if (chatChannelRaw.startsWith("chatchatunmute$")) {
            MUTED_PLAYERS.remove(chatChannel, speakingPlayerName.toLowerCase());
            return;
        }

        // Ignore muted players
        if (MUTED_PLAYERS.containsEntry(chatChannel, speakingPlayerName.toLowerCase())) {
            return;
        }

        // Is this a valid chat channel?
        if (!CHAT_CHANNELS.containsEntry(chatChannel, origin.getName())) {
            CHAT_CHANNELS.put(chatChannel, origin.getName());
        }

        // Process the data
        short len = in.readShort();
        byte[] msgbytes = new byte[len];
        in.readFully(msgbytes);

        DataInputStream msgin = new DataInputStream(new ByteArrayInputStream(msgbytes));

        try {
            // Finally get the message
            String message = msgin.readUTF();

            // Send the message to appropriate servers
            for (ServerInfo server : getProxy().getServers().values()) {
                // Continue from invalid targets
                if (origin.getName().equals(server.getName())
                        || !CHAT_CHANNELS.containsEntry(chatChannel, server.getName())) {
                    continue;
                }

                // Send the message to the appropriate players
                for (ProxiedPlayer player : server.getPlayers()) {
                    player.sendMessage(message);
                }
            }
        } catch (IOException oops) {
            oops.printStackTrace();
        }
    }
}

From source file:io.github.leonardosnt.bungeechannelapi.BungeeChannelApi.java

@SuppressWarnings("unchecked")
private void onPluginMessageReceived(String channel, Player player, byte[] message) {
    if (!channel.equalsIgnoreCase("BungeeCord"))
        return;// ww  w. java 2s .  c o  m

    ByteArrayDataInput input = ByteStreams.newDataInput(message);
    String subchannel = input.readUTF();

    synchronized (callbackMap) {
        Queue<CompletableFuture<?>> callbacks;

        if (subchannel.equals("PlayerCount") || subchannel.equals("PlayerList")
                || subchannel.equals("UUIDOther") || subchannel.equals("ServerIP")) {
            String identifier = input.readUTF(); // Server/player name
            callbacks = callbackMap.get(subchannel + "-" + identifier);

            if (callbacks == null || callbacks.isEmpty()) {
                return;
            }

            CompletableFuture<?> callback = callbacks.poll();

            try {
                switch (subchannel) {
                case "PlayerCount":
                    ((CompletableFuture<Integer>) callback).complete(Integer.valueOf(input.readInt()));
                    break;

                case "PlayerList":
                    ((CompletableFuture<List<String>>) callback)
                            .complete(Arrays.asList(input.readUTF().split(", ")));
                    break;

                case "UUIDOther":
                    ((CompletableFuture<String>) callback).complete(input.readUTF());
                    break;

                case "ServerIP": {
                    String ip = input.readUTF();
                    int port = input.readUnsignedShort();
                    ((CompletableFuture<InetSocketAddress>) callback).complete(new InetSocketAddress(ip, port));
                    break;
                }
                }
            } catch (Exception ex) {
                callback.completeExceptionally(ex);
            }

            return;
        }

        callbacks = callbackMap.get(subchannel);

        if (callbacks == null) {
            short dataLength = input.readShort();
            byte[] data = new byte[dataLength];
            input.readFully(data);

            if (globalForwardListener != null) {
                globalForwardListener.accept(subchannel, player, data);
            }

            if (forwardListeners != null) {
                synchronized (forwardListeners) {
                    ForwardConsumer listener = forwardListeners.get(subchannel);
                    if (listener != null) {
                        listener.accept(subchannel, player, data);
                    }
                }
            }

            return;
        }

        if (callbacks.isEmpty()) {
            return;
        }

        final CompletableFuture<?> callback = callbacks.poll();

        try {
            switch (subchannel) {
            case "GetServers":
                ((CompletableFuture<List<String>>) callback)
                        .complete(Arrays.asList(input.readUTF().split(", ")));
                break;

            case "GetServer":
            case "UUID":
                ((CompletableFuture<String>) callback).complete(input.readUTF());
                break;

            case "IP": {
                String ip = input.readUTF();
                int port = input.readInt();
                ((CompletableFuture<InetSocketAddress>) callback).complete(new InetSocketAddress(ip, port));
                break;
            }

            default:
                break;
            }
        } catch (Exception ex) {
            callback.completeExceptionally(ex);
        }
    }
}