com.google.common.io.ByteStreams#newDataInput ( )源码实例Demo

下面列出了com.google.common.io.ByteStreams#newDataInput ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: rya   文件: StatementPatternStorage.java
@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;
}
 
源代码2 项目: HubBasics   文件: BungeeListener.java
@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);
            }
        }
    }
}
 
源代码3 项目: kite   文件: ReadRCFileBuilder.java
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;
}
 
源代码4 项目: AuthMeReloaded   文件: BungeeReceiver.java
/**
 * 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:
    }
}
 
源代码5 项目: eagle   文件: PartitionedEventSerializerTest.java
@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);
}
 
源代码6 项目: SkyWarsReloaded   文件: SkyWarsReloaded.java
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();
		} 
    }
}
 
源代码7 项目: eagle   文件: TypedByteArrayComparator.java
/**
 * 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;
}
 
源代码8 项目: suro   文件: MessageSerDe.java
@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();
    }
}
 
源代码9 项目: FastLogin   文件: BungeeListener.java
@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);
        }
    }
}
 
源代码10 项目: Eagle   文件: RowValueFilter.java
/**
 * 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;
}
 
源代码11 项目: phoenix-tephra   文件: TransactionEditTest.java
@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);
}
 
源代码12 项目: phoenix-tephra   文件: TransactionEditTest.java
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);
}
 
源代码13 项目: LuckPerms   文件: PluginMessageMessenger.java
@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);
}
 
源代码14 项目: eagle   文件: PartitionedEventSerializerTest.java
@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());
}
 
源代码15 项目: datawave   文件: UpgradeCounterValues.java
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);
            }
        }
        
    }
}
 
源代码16 项目: Velocity   文件: PluginMessageEvent.java
public ByteArrayDataInput dataAsDataStream() {
  return ByteStreams.newDataInput(data);
}
 
源代码17 项目: Eagle   文件: ProtoBufConverter.java
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;
}
 
源代码18 项目: Skript   文件: Utils.java
/**
 * 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;
}
 
源代码19 项目: PlayerSQL   文件: PlayerSqlProtocol.java
public static PlayerSqlProtocol decode(byte[] input) {
    ByteArrayDataInput buf = ByteStreams.newDataInput(input);
    Protocol protocol = Protocol.values()[buf.readByte()];
    return protocol.decode(buf);
}
 
源代码20 项目: turbine   文件: ByteReader.java
public ByteReader(byte[] bytes, int pos) {
  this.bytes = bytes;
  this.indexed = new IndexedByteArrayInputStream(bytes, pos, bytes.length);
  this.input = ByteStreams.newDataInput(indexed);
}