Java Code Examples for com.google.common.io.ByteStreams#newDataInput()

The following examples show how to use com.google.common.io.ByteStreams#newDataInput() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatementPatternStorage.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public Tuple getNext() throws IOException {
    try {
        if (reader.nextKeyValue()) {
            Key key = reader.getCurrentKey();
            org.apache.accumulo.core.data.Value value = reader.getCurrentValue();
            ByteArrayDataInput input = ByteStreams.newDataInput(key.getRow().getBytes());
            RyaStatement ryaStatement = ryaContext.deserializeTriple(layout, new TripleRow(key.getRow().getBytes(),
                    key.getColumnFamily().getBytes(), key.getColumnQualifier().getBytes()));

            Tuple tuple = TupleFactory.getInstance().newTuple(7);
            tuple.set(0, ryaStatement.getSubject().getData());
            tuple.set(1, ryaStatement.getPredicate().getData());
            tuple.set(2, ryaStatement.getObject().getData());
            tuple.set(3, (ryaStatement.getContext() != null) ? (ryaStatement.getContext().getData()) : (null));
            tuple.set(4, ryaStatement.getSubject().getDataType());
            tuple.set(5, ryaStatement.getPredicate().getDataType());
            tuple.set(6, ryaStatement.getObject().getDataType());
            return tuple;
        }
    } catch (Exception e) {
        throw new IOException(e);
    }
    return null;
}
 
Example 2
Source File: BungeeListener.java    From HubBasics with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void onPluginMessageReceived(String channel, @NotNull Player player, @NotNull byte[] message) {
    if (!channel.equals("BungeeCord")) {
        return;
    }
    ByteArrayDataInput in = ByteStreams.newDataInput(message);
    String subChannel = in.readUTF();
    if (subChannel.equals("HubBasics")) {
        String action = in.readUTF();
        if (action.equalsIgnoreCase("Lobby")) {
            LobbyModule module = (LobbyModule) HubBasics.getInstance()
                    .getModuleManager().getModule(EnumModules.Lobby);
            HLocation location = module.getLocation();
            if (location != null) {
                location.teleport(player);
            }
        }
    }
}
 
Example 3
Source File: ReadRCFileBuilder.java    From kite with Apache License 2.0 6 votes vote down vote up
private Writable updateColumnValue(RCFileColumn column, BytesRefWritable bytesRef) throws IOException {
  if(bytesRef.getLength() == 0) {
    // This is a null field.
    return NullWritable.get();
  }
  Writable newColumnValue = column.newWritable();
  // Small optimization to bypass DataInput read if the column writable is
  // BytesRefWritable
  if (newColumnValue.getClass() == BytesRefWritable.class) {
    newColumnValue = bytesRef;
  } else {
    byte[] currentRowBytes = Arrays.copyOfRange(bytesRef.getData(),
        bytesRef.getStart(), bytesRef.getStart() + bytesRef.getLength());
    DataInput dataInput = ByteStreams.newDataInput(currentRowBytes);
    newColumnValue.readFields(dataInput);
  }
  return newColumnValue;
}
 
Example 4
Source File: BungeeReceiver.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Processes the given data input and attempts to translate it to a message for the "AuthMe.v2.Broadcast" channel.
 *
 * @param in the input to handle
 */
private void handleBroadcast(final ByteArrayDataInput in) {
    // Read data byte array
    final short dataLength = in.readShort();
    final byte[] dataBytes = new byte[dataLength];
    in.readFully(dataBytes);
    final ByteArrayDataInput dataIn = ByteStreams.newDataInput(dataBytes);

    // Parse type
    final String typeId = dataIn.readUTF();
    final Optional<MessageType> type = MessageType.fromId(typeId);
    if (!type.isPresent()) {
        logger.debug("Received unsupported forwarded bungeecord message type! ({0})", typeId);
        return;
    }

    // Parse argument
    final String argument;
    try {
        argument = dataIn.readUTF();
    } catch (IllegalStateException e) {
        logger.warning("Received invalid forwarded plugin message of type " + type.get().name()
            + ": argument is missing!");
        return;
    }

    // Handle type
    switch (type.get()) {
        case UNREGISTER:
            dataSource.invalidateCache(argument);
            break;
        case REFRESH_PASSWORD:
        case REFRESH_QUITLOC:
        case REFRESH_EMAIL:
        case REFRESH:
            dataSource.refreshCache(argument);
            break;
        default:
    }
}
 
Example 5
Source File: PluginMessageMessenger.java    From LuckPerms with MIT License 5 votes vote down vote up
@Override
public void onPluginMessageReceived(String s, @NonNull Player player, @NonNull byte[] bytes) {
    if (!s.equals(CHANNEL)) {
        return;
    }

    ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
    String msg = in.readUTF();

    this.consumer.consumeIncomingMessageAsString(msg);
}
 
Example 6
Source File: TransactionEditTest.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
private void assertSerializedEdit(TransactionEdit originalEdit) throws IOException {
  ByteArrayDataOutput out = ByteStreams.newDataOutput();
  originalEdit.write(out);

  TransactionEdit decodedEdit = new TransactionEdit();
  DataInput in = ByteStreams.newDataInput(out.toByteArray());
  decodedEdit.readFields(in);

  Assert.assertEquals(originalEdit, decodedEdit);
}
 
Example 7
Source File: TransactionEditTest.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void verifyDecodingSupportsOlderVersion(TransactionEdit edit, 
                                                TransactionEditCodecs.TransactionEditCodec olderCodec)
  throws IOException {
  // encoding with older version of codec
  ByteArrayDataOutput out = ByteStreams.newDataOutput();
  TransactionEditCodecs.encode(edit, out, olderCodec);

  // decoding
  TransactionEdit decodedEdit = new TransactionEdit();
  DataInput in = ByteStreams.newDataInput(out.toByteArray());
  decodedEdit.readFields(in);

  Assert.assertEquals(edit, decodedEdit);
}
 
Example 8
Source File: RowValueFilter.java    From Eagle with Apache License 2.0 5 votes vote down vote up
/**
 * TODO: Currently still use older serialization method from hbase-0.94, need to migrate into ProtoBuff based
 */
// Override static method
public static Filter parseFrom(final byte [] pbBytes) throws DeserializationException {
    ByteArrayDataInput byteArrayDataInput = ByteStreams.newDataInput(pbBytes);
    RowValueFilter filter = new RowValueFilter();
    try {
        filter.readFields(byteArrayDataInput);
    } catch (IOException e) {
        LOG.error("Got error to deserialize RowValueFilter from PB bytes",e);
        throw new DeserializationException(e);
    }
    return filter;
}
 
Example 9
Source File: BungeeListener.java    From FastLogin with MIT License 5 votes vote down vote up
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
    ByteArrayDataInput dataInput = ByteStreams.newDataInput(message);

    LoginActionMessage loginMessage = new LoginActionMessage();
    loginMessage.readFrom(dataInput);

    plugin.getLog().debug("Received plugin message {}", loginMessage);

    Player targetPlayer = player;
    if (!loginMessage.getPlayerName().equals(player.getName())) {
        targetPlayer = Bukkit.getPlayerExact(loginMessage.getPlayerName());;
    }

    if (targetPlayer == null) {
        plugin.getLog().warn("Force action player {} not found", loginMessage.getPlayerName());
        return;
    }

    // fail if target player is blacklisted because already authenticated or wrong bungeecord id
    if (targetPlayer.hasMetadata(plugin.getName())) {
        plugin.getLog().warn("Received message {} from a blacklisted player {}", loginMessage, targetPlayer);
    } else {
        UUID sourceId = loginMessage.getProxyId();
        if (plugin.getBungeeManager().isProxyAllowed(sourceId)) {
            readMessage(targetPlayer, loginMessage);
        } else {
            plugin.getLog().warn("Received proxy id: {} that doesn't exist in the proxy whitelist file", sourceId);
        }
    }
}
 
Example 10
Source File: MessageSerDe.java    From suro with Apache License 2.0 5 votes vote down vote up
@Override
public Message deserialize(byte[] payload) {
    try {
        DataInput dataInput = ByteStreams.newDataInput(payload);
        Class<? extends Message> clazz = Message.classMap.get(dataInput.readByte());
        Message msg = clazz.newInstance();
        msg.readFields(dataInput);
        return msg;
    } catch (Exception e) {
        log.error("Exception on deserialize: " + e.getMessage(), e);
        return new Message();
    }
}
 
Example 11
Source File: TypedByteArrayComparator.java    From eagle with Apache License 2.0 5 votes vote down vote up
/**
 * For hbase 0.98
 *
 * @param bytes raw byte array
 * @return Comparator instance
 * @throws DeserializationException
 */
public static TypedByteArrayComparator parseFrom(final byte[] bytes) throws DeserializationException {
    TypedByteArrayComparator comparator = new TypedByteArrayComparator();
    ByteArrayDataInput byteArrayDataInput = ByteStreams.newDataInput(bytes);
    try {
        comparator.readFields(byteArrayDataInput);
    } catch (IOException e) {
        LOG.error("Got error to deserialize TypedByteArrayComparator from PB bytes", e);
        throw new DeserializationException(e);
    }
    return comparator;
}
 
Example 12
Source File: SkyWarsReloaded.java    From SkyWarsReloaded with GNU General Public License v3.0 5 votes vote down vote up
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
	if (!channel.equals("BungeeCord")) {
		return;
    }
    ByteArrayDataInput in = ByteStreams.newDataInput(message);
    String subchannel = in.readUTF();
   
    if (subchannel.equals("GetServer")) {
    	servername = in.readUTF();
    }
    
    if (subchannel.equals("SWRMessaging")) {
    	short len = in.readShort();
    	byte[] msgbytes = new byte[len];
    	in.readFully(msgbytes);

    	DataInputStream msgin = new DataInputStream(new ByteArrayInputStream(msgbytes));
    	try {
			String header = msgin.readUTF();
			if (header.equalsIgnoreCase("RequestUpdate")) {
				String sendToServer = msgin.readUTF();
				String playerCount = "" + GameMap.getMaps().get(0).getAlivePlayers().size();
				String maxPlayers = "" + GameMap.getMaps().get(0).getMaxPlayers();
				String gameStarted = "" + GameMap.getMaps().get(0).getMatchState().toString();
				ArrayList<String> messages = new ArrayList<>();
				messages.add("ServerUpdate");
				messages.add(servername);
				messages.add(playerCount);
				messages.add(maxPlayers);
				messages.add(gameStarted);
				sendSWRMessage(player, sendToServer, messages);					
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
    }
}
 
Example 13
Source File: PartitionedEventSerializerTest.java    From eagle with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Test
public void testPartitionEventSerialization() throws IOException {
    PartitionedEvent partitionedEvent = MockSampleMetadataFactory.createPartitionedEventGroupedByName("sampleStream", System.currentTimeMillis());
    ;
    PartitionedEventSerializerImpl serializer = new PartitionedEventSerializerImpl(MockSampleMetadataFactory::createSampleStreamDefinition);

    ByteArrayDataOutput dataOutput1 = ByteStreams.newDataOutput();
    serializer.serialize(partitionedEvent, dataOutput1);
    byte[] serializedBytes = dataOutput1.toByteArray();
    PartitionedEvent deserializedEvent = serializer.deserialize(ByteStreams.newDataInput(serializedBytes));
    Assert.assertEquals(partitionedEvent, deserializedEvent);

    PartitionedEventSerializerImpl compressSerializer = new PartitionedEventSerializerImpl(MockSampleMetadataFactory::createSampleStreamDefinition, true);

    byte[] serializedBytesCompressed = compressSerializer.serialize(partitionedEvent);
    PartitionedEvent deserializedEventCompressed = compressSerializer.deserialize(serializedBytesCompressed);
    Assert.assertEquals(partitionedEvent, deserializedEventCompressed);

    PartitionedEventDigestSerializer serializer2 = new PartitionedEventDigestSerializer(MockSampleMetadataFactory::createSampleStreamDefinition);
    ByteArrayDataOutput dataOutput2 = ByteStreams.newDataOutput();
    serializer2.serialize(partitionedEvent, dataOutput2);
    byte[] serializedBytes2 = dataOutput2.toByteArray();
    ByteArrayDataInput dataInput2 = ByteStreams.newDataInput(serializedBytes2);
    PartitionedEvent deserializedEvent2 = serializer2.deserialize(dataInput2);
    Assert.assertEquals(partitionedEvent, deserializedEvent2);

    byte[] javaSerialization = new DefaultSerializationDelegate().serialize(partitionedEvent);
    Kryo kryo = new DefaultKryoFactory.KryoSerializableDefault();
    Output output = new Output(10000);
    kryo.writeClassAndObject(output, partitionedEvent);
    byte[] kryoBytes = output.toBytes();
    Input input = new Input(kryoBytes);
    PartitionedEvent kryoDeserializedEvent = (PartitionedEvent) kryo.readClassAndObject(input);
    Assert.assertEquals(partitionedEvent, kryoDeserializedEvent);
    LOG.info("\nCached Stream:{}\nCompressed Cached Stream :{}\nCached Stream + Cached Partition: {}\nJava Native: {}\nKryo: {}\nKryo + Cached Stream: {}\nKryo + Cached Stream + Cached Partition: {}", serializedBytes.length, serializedBytesCompressed.length, serializedBytes2.length, javaSerialization.length, kryoBytes.length, kryoSerialize(serializedBytes).length, kryoSerialize(serializedBytes2).length);
}
 
Example 14
Source File: PluginMessageEvent.java    From Velocity with MIT License 4 votes vote down vote up
public ByteArrayDataInput dataAsDataStream() {
  return ByteStreams.newDataInput(data);
}
 
Example 15
Source File: ProtoBufConverter.java    From Eagle with Apache License 2.0 4 votes vote down vote up
public static AggregateResult fromPBResult(AggregateProtos.AggregateResult pbResult) throws IOException {
    ByteArrayDataInput byteArrayDataInput = ByteStreams.newDataInput(pbResult.getByteArray().toByteArray());;
    AggregateResult result = new AggregateResult();
    result.readFields(byteArrayDataInput);
    return result;
}
 
Example 16
Source File: Utils.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Sends a plugin message.
 *
 * Example usage using the "GetServers" bungee plugin message channel via an overload:
 * <code>
 *     Utils.sendPluginMessage("BungeeCord", r -> "GetServers".equals(r.readUTF()), "GetServers")
 *     			.thenAccept(response -> Bukkit.broadcastMessage(response.readUTF()) // comma delimited server broadcast
 *     			.exceptionally(ex -> {
 *     			 	Skript.warning("Failed to get servers because there are no players online");
 *     			 	return null;
 *     			});
 * </code>
 *
 * @param player the player to send the plugin message through
 * @param channel the channel for this plugin message
 * @param messageVerifier verifies that a plugin message is the response to the sent message
 * @param data the data to add to the outgoing message
 * @return a completable future for the message of the responding plugin message, if there is one.
 * this completable future will complete exceptionally if the player is null.
 */
public static CompletableFuture<ByteArrayDataInput> sendPluginMessage(Player player, String channel,
		Predicate<ByteArrayDataInput> messageVerifier, String... data) {
	CompletableFuture<ByteArrayDataInput> completableFuture = new CompletableFuture<>();

	if (player == null) {
		completableFuture.completeExceptionally(new IllegalStateException("Can't send plugin messages from a null player"));
		return completableFuture;
	}

	Skript skript = Skript.getInstance();
	Messenger messenger = Bukkit.getMessenger();

	messenger.registerOutgoingPluginChannel(skript, channel);

	PluginMessageListener listener = (sendingChannel, sendingPlayer, message) -> {
		ByteArrayDataInput input = ByteStreams.newDataInput(message);
		if (channel.equals(sendingChannel) && sendingPlayer == player && !completableFuture.isDone()
				&& !completableFuture.isCancelled() && messageVerifier.test(input)) {
			completableFuture.complete(input);
		}
	};

	messenger.registerIncomingPluginChannel(skript, channel, listener);

	completableFuture.whenComplete((r, ex) -> messenger.unregisterIncomingPluginChannel(skript, channel, listener));

	// if we haven't gotten a response after a minute, let's just assume there wil never be one
	Bukkit.getScheduler().scheduleSyncDelayedTask(skript, () -> {

		if (!completableFuture.isDone())
			completableFuture.cancel(true);

	}, 60 * 20);

	ByteArrayDataOutput out = ByteStreams.newDataOutput();
	Stream.of(data).forEach(out::writeUTF);
	player.sendPluginMessage(Skript.getInstance(), channel, out.toByteArray());

	return completableFuture;
}
 
Example 17
Source File: UpgradeCounterValues.java    From datawave with Apache License 2.0 4 votes vote down vote up
protected void run(String[] args) throws ParseException, AccumuloSecurityException, AccumuloException, TableNotFoundException, IOException {
    parseConfig(args);
    
    ZooKeeperInstance instance = new ZooKeeperInstance(ClientConfiguration.loadDefault().withInstance(instanceName).withZkHosts(zookeepers));
    Connector connector = instance.getConnector(username, new PasswordToken(password));
    Authorizations auths = connector.securityOperations().getUserAuthorizations(connector.whoami());
    
    try (BatchWriter writer = connector.createBatchWriter(tableName, new BatchWriterConfig().setMaxWriteThreads(bwThreads).setMaxMemory(bwMemory)
                    .setMaxLatency(60, TimeUnit.SECONDS));
                    BatchScanner scanner = connector.createBatchScanner(tableName, auths, bsThreads)) {
        scanner.setRanges(ranges);
        
        for (Entry<Key,Value> entry : scanner) {
            Key key = entry.getKey();
            
            ByteArrayDataInput in = ByteStreams.newDataInput(entry.getValue().get());
            Counters counters = new Counters();
            try {
                counters.readFields(in);
            } catch (IOException e) {
                // The IO exception means the counters are in the wrong format. We *assume* that they are in
                // the old (CDH3) format, and de-serialize according to that, and re-write the key with the new value.
                in = ByteStreams.newDataInput(entry.getValue().get());
                int numGroups = in.readInt();
                while (numGroups-- > 0) {
                    String groupName = Text.readString(in);
                    String groupDisplayName = Text.readString(in);
                    CounterGroup group = counters.addGroup(groupName, groupDisplayName);
                    
                    int groupSize = WritableUtils.readVInt(in);
                    for (int i = 0; i < groupSize; i++) {
                        String counterName = Text.readString(in);
                        String counterDisplayName = counterName;
                        if (in.readBoolean())
                            counterDisplayName = Text.readString(in);
                        long value = WritableUtils.readVLong(in);
                        group.addCounter(counterName, counterDisplayName, value);
                    }
                }
                
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                counters.write(out);
                Mutation m = new Mutation(key.getRow());
                m.put(key.getColumnFamily(), key.getColumnQualifier(), key.getColumnVisibilityParsed(), key.getTimestamp() + 1,
                                new Value(out.toByteArray()));
                writer.addMutation(m);
            }
        }
        
    }
}
 
Example 18
Source File: PartitionedEventSerializerTest.java    From eagle with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Test
public void testPartitionEventSerializationEfficiency() throws IOException {
    PartitionedEvent partitionedEvent = MockSampleMetadataFactory.createPartitionedEventGroupedByName("sampleStream", System.currentTimeMillis());
    ;
    PartitionedEventSerializerImpl serializer = new PartitionedEventSerializerImpl(MockSampleMetadataFactory::createSampleStreamDefinition);

    int count = 100000;
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    int i = 0;
    while (i < count) {
        ByteArrayDataOutput dataOutput1 = ByteStreams.newDataOutput();
        serializer.serialize(partitionedEvent, dataOutput1);
        byte[] serializedBytes = dataOutput1.toByteArray();
        PartitionedEvent deserializedEvent = serializer.deserialize(ByteStreams.newDataInput(serializedBytes));
        Assert.assertEquals(partitionedEvent, deserializedEvent);
        i++;
    }
    stopWatch.stop();
    LOG.info("Cached Stream: {} ms", stopWatch.getTime());
    stopWatch.reset();
    PartitionedEventSerializerImpl compressSerializer = new PartitionedEventSerializerImpl(MockSampleMetadataFactory::createSampleStreamDefinition, true);
    i = 0;
    stopWatch.start();
    while (i < count) {
        byte[] serializedBytesCompressed = compressSerializer.serialize(partitionedEvent);
        PartitionedEvent deserializedEventCompressed = compressSerializer.deserialize(serializedBytesCompressed);
        Assert.assertEquals(partitionedEvent, deserializedEventCompressed);
        i++;
    }
    stopWatch.stop();
    LOG.info("Compressed Cached Stream: {} ms", stopWatch.getTime());
    stopWatch.reset();

    i = 0;
    stopWatch.start();
    while (i < count) {
        PartitionedEventDigestSerializer serializer2 = new PartitionedEventDigestSerializer(MockSampleMetadataFactory::createSampleStreamDefinition);
        ByteArrayDataOutput dataOutput2 = ByteStreams.newDataOutput();
        serializer2.serialize(partitionedEvent, dataOutput2);
        byte[] serializedBytes2 = dataOutput2.toByteArray();
        ByteArrayDataInput dataInput2 = ByteStreams.newDataInput(serializedBytes2);
        PartitionedEvent deserializedEvent2 = serializer2.deserialize(dataInput2);
        Assert.assertEquals(partitionedEvent, deserializedEvent2);
        i++;
    }
    stopWatch.stop();
    LOG.info("Cached Stream&Partition: {} ms", stopWatch.getTime());
    stopWatch.reset();
    i = 0;
    stopWatch.start();
    while (i < count) {
        byte[] javaSerialization = new DefaultSerializationDelegate().serialize(partitionedEvent);
        PartitionedEvent javaSerializedEvent = (PartitionedEvent) new DefaultSerializationDelegate().deserialize(javaSerialization);
        Assert.assertEquals(partitionedEvent, javaSerializedEvent);
        i++;
    }
    stopWatch.stop();
    LOG.info("Java Native: {} ms", stopWatch.getTime());
    stopWatch.reset();
    i = 0;
    stopWatch.start();
    Kryo kryo = new DefaultKryoFactory.KryoSerializableDefault();
    while (i < count) {
        Output output = new Output(10000);
        kryo.writeClassAndObject(output, partitionedEvent);
        byte[] kryoBytes = output.toBytes();
        Input input = new Input(kryoBytes);
        PartitionedEvent kryoDeserializedEvent = (PartitionedEvent) kryo.readClassAndObject(input);
        Assert.assertEquals(partitionedEvent, kryoDeserializedEvent);
        i++;
    }
    stopWatch.stop();
    LOG.info("Kryo: {} ms", stopWatch.getTime());
}
 
Example 19
Source File: PlayerSqlProtocol.java    From PlayerSQL with GNU General Public License v2.0 4 votes vote down vote up
public static PlayerSqlProtocol decode(byte[] input) {
    ByteArrayDataInput buf = ByteStreams.newDataInput(input);
    Protocol protocol = Protocol.values()[buf.readByte()];
    return protocol.decode(buf);
}
 
Example 20
Source File: ByteReader.java    From turbine with Apache License 2.0 4 votes vote down vote up
public ByteReader(byte[] bytes, int pos) {
  this.bytes = bytes;
  this.indexed = new IndexedByteArrayInputStream(bytes, pos, bytes.length);
  this.input = ByteStreams.newDataInput(indexed);
}