Java Code Examples for com.google.common.io.ByteArrayDataOutput#toByteArray()

The following examples show how to use com.google.common.io.ByteArrayDataOutput#toByteArray() . 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: BungeeCordImpl.java    From helper with MIT License 6 votes vote down vote up
private void sendToChannel(MessageAgent agent, Player player) {
    ensureSetup();

    // create a new data output stream for the message
    ByteArrayDataOutput out = ByteStreams.newDataOutput();

    // write the channel
    out.writeUTF(agent.getSubChannel());
    // append the agents data
    agent.appendPayload(out);

    byte[] buf = out.toByteArray();
    player.sendPluginMessage(this.plugin, CHANNEL, buf);

    // if the agent is also a MessageCallback, register it
    if (agent instanceof MessageCallback) {
        MessageCallback callback = (MessageCallback) agent;
        registerCallback(callback);
    }
}
 
Example 2
Source File: BinarySerializationUtils.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
static byte[] serializeBinary(TagContext tags) throws TagContextSerializationException {
  // Use a ByteArrayDataOutput to avoid needing to handle IOExceptions.
  final ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
  byteArrayDataOutput.write(VERSION_ID);
  int totalChars = 0; // Here chars are equivalent to bytes, since we're using ascii chars.
  for (Iterator<Tag> i = InternalUtils.getTags(tags); i.hasNext(); ) {
    Tag tag = i.next();
    if (TagTtl.NO_PROPAGATION.equals(tag.getTagMetadata().getTagTtl())) {
      continue;
    }
    totalChars += tag.getKey().getName().length();
    totalChars += tag.getValue().asString().length();
    encodeTag(tag, byteArrayDataOutput);
  }
  if (totalChars > TAGCONTEXT_SERIALIZED_SIZE_LIMIT) {
    throw new TagContextSerializationException(
        "Size of TagContext exceeds the maximum serialized size "
            + TAGCONTEXT_SERIALIZED_SIZE_LIMIT);
  }
  return byteArrayDataOutput.toByteArray();
}
 
Example 3
Source File: VectorIterator.java    From Indra with MIT License 6 votes vote down vote up
private void setCurrentContent() throws IOException {
    if (numberOfVectors > 0) {
        numberOfVectors--;

        ByteArrayDataOutput out = ByteStreams.newDataOutput();

        byte b;
        while ((b = input.readByte()) != ' ') {
            out.writeByte(b);
        }

        String word = new String(out.toByteArray(), StandardCharsets.UTF_8);
        if (this.sparse) {
            this.currentVector = new TermVector(true, word, readSparseVector(this.dimensions));
        } else {
            this.currentVector = new TermVector(false, word, readDenseVector(this.dimensions));
        }

    } else {
        this.currentVector = null;
    }
}
 
Example 4
Source File: ResourceString.java    From android-arscblamer with Apache License 2.0 6 votes vote down vote up
/**
 * 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.
 * @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();
}
 
Example 5
Source File: PacketChunker.java    From OpenModsLib with MIT License 6 votes vote down vote up
public byte[][] splitIntoChunks(byte[] data, int maxChunkSize) {

		final int numChunks = (data.length + maxChunkSize - 1) / maxChunkSize;
		Preconditions.checkArgument(numChunks < 256, "%s chunks? Way too much data, man.", numChunks);
		byte[][] result = new byte[numChunks][];

		int chunkOffset = 0;
		for (int chunkIndex = 0; chunkIndex < numChunks; chunkIndex++) {
			// size of the current chunk
			int chunkSize = Math.min(data.length - chunkOffset, maxChunkSize);

			ByteArrayDataOutput buf = ByteStreams.newDataOutput(maxChunkSize);

			buf.writeByte(numChunks);
			if (numChunks > 1) {
				buf.writeByte(chunkIndex);
				buf.writeByte(packetId);
			}

			buf.write(data, chunkOffset, chunkSize);
			result[chunkIndex] = buf.toByteArray();
			chunkOffset += chunkSize;
		}
		packetId++;
		return result;
	}
 
Example 6
Source File: RedisCommandParser.java    From redis-mock with MIT License 5 votes vote down vote up
@VisibleForTesting
Slice consumeSlice(long len) throws ParseErrorException {
    ByteArrayDataOutput bo = ByteStreams.newDataOutput();
    for (long i = 0; i < len; i++) {
        try {
            bo.write(consumeByte());
        } catch (EOFException e) {
            throw new ParseErrorException();
        }
    }
    return new Slice(bo.toByteArray());
}
 
Example 7
Source File: AttributeWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
public void writeParameterAnnotations(Attribute.ParameterAnnotations attribute) {
  output.writeShort(pool.utf8(attribute.kind().signature()));
  ByteArrayDataOutput tmp = ByteStreams.newDataOutput();
  tmp.writeByte(attribute.annotations().size());
  for (List<AnnotationInfo> parameterAnnotations : attribute.annotations()) {
    tmp.writeShort(parameterAnnotations.size());
    for (AnnotationInfo annotation : parameterAnnotations) {
      new AnnotationWriter(pool, tmp).writeAnnotation(annotation);
    }
  }
  byte[] data = tmp.toByteArray();
  output.writeInt(data.length);
  output.write(data);
}
 
Example 8
Source File: AttributeWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
public void writeAnnotationDefault(Attribute.AnnotationDefault attribute) {
  output.writeShort(pool.utf8(attribute.kind().signature()));
  ByteArrayDataOutput tmp = ByteStreams.newDataOutput();
  new AnnotationWriter(pool, tmp).writeElementValue(attribute.value());
  byte[] data = tmp.toByteArray();
  output.writeInt(data.length);
  output.write(data);
}
 
Example 9
Source File: AttributeWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
private void writeTypeAnnotation(TypeAnnotations attribute) {
  output.writeShort(pool.utf8(attribute.kind().signature()));
  ByteArrayDataOutput tmp = ByteStreams.newDataOutput();
  tmp.writeShort(attribute.annotations().size());
  for (TypeAnnotationInfo annotation : attribute.annotations()) {
    new AnnotationWriter(pool, tmp).writeTypeAnnotation(annotation);
  }
  byte[] data = tmp.toByteArray();
  output.writeInt(data.length);
  output.write(data);
}
 
Example 10
Source File: ResourceFile.java    From android-arscblamer with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] toByteArray(int options) throws IOException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  for (Chunk chunk : chunks) {
    output.write(chunk.toByteArray(options));
  }
  return output.toByteArray();
}
 
Example 11
Source File: TypedByteArrayComparator.java    From Eagle with Apache License 2.0 5 votes vote down vote up
/**
 * For hbase 0.98
 *
 * @return serialized byte array
 */
@Override
public byte[] toByteArray() {
    ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
    try {
        this.write(byteArrayDataOutput);
        return byteArrayDataOutput.toByteArray();
    } catch (IOException e) {
        LOG.error("Failed to serialize due to: "+e.getMessage(),e);
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: BukkitBridge.java    From BungeeTabListPlus with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected <T> void addActiveKey(DataKey<T> key) {
    super.addActiveKey(key);

    try {
        synchronized (this) {
            Server connection = getConnection();
            if (connection != null) {
                ByteArrayDataOutput data = ByteStreams.newDataOutput();
                data.writeByte(this instanceof PlayerBridgeDataCache ? BridgeProtocolConstants.MESSAGE_ID_REQUEST_DATA : BridgeProtocolConstants.MESSAGE_ID_REQUEST_DATA_SERVER);
                data.writeInt(connectionId);
                data.writeInt(nextOutgoingMessageId++);
                data.writeInt(1);
                DataStreamUtils.writeDataKey(data, key);
                data.writeInt(idMap.getNetId(key));
                byte[] message = data.toByteArray();
                messagesPendingConfirmation.add(message);
                lastMessageSent = System.currentTimeMillis();
                connection.sendData(BridgeProtocolConstants.CHANNEL, message);
            } else {
                requestAll = true;
            }
        }
    } catch (Throwable th) {
        rlExecutor.execute(() -> {
            logger.log(Level.SEVERE, "Unexpected exception", th);
        });
        requestAll = true;
    }
}
 
Example 13
Source File: BungeeSender.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
private void sendForwardedBungeecordMessage(final String subChannel, final String... data) {
    final ByteArrayDataOutput out = ByteStreams.newDataOutput();
    out.writeUTF("Forward");
    out.writeUTF("ONLINE");
    out.writeUTF(subChannel);
    final ByteArrayDataOutput dataOut = ByteStreams.newDataOutput();
    for (final String element : data) {
        dataOut.writeUTF(element);
    }
    final byte[] dataBytes = dataOut.toByteArray();
    out.writeShort(dataBytes.length);
    out.write(dataBytes);
    bukkitService.sendBungeeMessage(out.toByteArray());
}
 
Example 14
Source File: TypedByteArrayComparator.java    From eagle with Apache License 2.0 5 votes vote down vote up
/**
 * For hbase 0.98
 *
 * @return serialized byte array
 */
@Override
public byte[] toByteArray() {
    ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
    try {
        this.write(byteArrayDataOutput);
        return byteArrayDataOutput.toByteArray();
    } catch (IOException e) {
        LOG.error("Failed to serialize due to: " + e.getMessage(), e);
        throw new RuntimeException(e);
    }
}
 
Example 15
Source File: Response.java    From redis-mock with MIT License 5 votes vote down vote up
public static Slice bulkString(Slice s) {
    if (s == null) {
        return NULL;
    }
    ByteArrayDataOutput bo = ByteStreams.newDataOutput();
    bo.write(String.format("$%d\r\n", s.length()).getBytes());
    bo.write(s.data());
    bo.write("\r\n".getBytes());
    return new Slice(bo.toByteArray());
}
 
Example 16
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeserializeTooLargeByteArrayThrowException()
    throws TagContextDeserializationException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.write(BinarySerializationUtils.VERSION_ID);
  for (int i = 0; i < BinarySerializationUtils.TAGCONTEXT_SERIALIZED_SIZE_LIMIT / 8 - 1; i++) {
    // Each tag will be with format {key : "0123", value : "0123"}, so the length of it is 8.
    String str;
    if (i < 10) {
      str = "000" + i;
    } else if (i < 100) {
      str = "00" + i;
    } else if (i < 1000) {
      str = "0" + i;
    } else {
      str = String.valueOf(i);
    }
    encodeTagToOutput(str, str, output);
  }
  // The last tag will be of size 9, so the total size of the TagContext (8193) will be one byte
  // more than limit.
  encodeTagToOutput("last", "last1", output);

  byte[] bytes = output.toByteArray();
  thrown.expect(TagContextDeserializationException.class);
  thrown.expectMessage("Size of TagContext exceeds the maximum serialized size ");
  serializer.fromByteArray(bytes);
}
 
Example 17
Source File: ResourceFile.java    From apkfile with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] toByteArray(boolean shrink) throws IOException {
    ByteArrayDataOutput output = ByteStreams.newDataOutput();
    for (Chunk chunk : chunks) {
        output.write(chunk.toByteArray(shrink));
    }
    return output.toByteArray();
}
 
Example 18
Source File: TagContextDeserializationTest.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeserializeInvalidTagKey() throws TagContextDeserializationException {
  ByteArrayDataOutput output = ByteStreams.newDataOutput();
  output.write(BinarySerializationUtils.VERSION_ID);

  // Encode an invalid tag key and a valid tag value:
  encodeTagToOutput("\2key", "value", output);
  final byte[] bytes = output.toByteArray();

  thrown.expect(TagContextDeserializationException.class);
  thrown.expectMessage("Invalid tag key: \2key");
  serializer.fromByteArray(bytes);
}
 
Example 19
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 20
Source File: RowValueFilter.java    From eagle with Apache License 2.0 3 votes vote down vote up
/**
 * TODO: Currently still use older serialization method from hbase-0.94, need to migrate into ProtoBuff
 * based
 *
 * @return
 * @throws IOException
 */
@Override
public byte[] toByteArray() throws IOException {
    ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
    this.comparator.write(byteArrayDataOutput);
    return byteArrayDataOutput.toByteArray();
}