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

The following examples show how to use com.google.common.io.ByteArrayDataOutput#writeByte() . 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: Packet.java    From FishingBot with GNU General Public License v3.0 6 votes vote down vote up
public static void writeVarInt(int value, ByteArrayDataOutput output) {
    int part;
    while (true) {
        part = value & 0x7F;

        value >>>= 7;
        if (value != 0) {
            part |= 0x80;
        }

        output.writeByte(part);

        if (value == 0) {
            break;
        }
    }
}
 
Example 2
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 3
Source File: BukkitBridge.java    From BungeeTabListPlus with GNU General Public License v3.0 6 votes vote down vote up
void requestMissingData() throws IOException {
    synchronized (this) {
        if (requestAll) {
            Server connection = getConnection();
            if (connection != null) {
                List<DataKey<?>> keys = new ArrayList<>(getActiveKeys());

                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(keys.size());
                for (DataKey<?> key : keys) {
                    DataStreamUtils.writeDataKey(data, key);
                    data.writeInt(idMap.getNetId(key));
                }
                byte[] message = data.toByteArray();
                messagesPendingConfirmation.add(message);
                lastMessageSent = System.currentTimeMillis();
                connection.sendData(BridgeProtocolConstants.CHANNEL, message);
            }
            requestAll = false;
        }
    }
}
 
Example 4
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 5
Source File: PacketOutUseItem.java    From FishingBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void write(ByteArrayDataOutput out, int protocolId) {
    switch (protocolId) {
        case ProtocolConstants.MINECRAFT_1_8: {
            out.writeLong(-1);      //Position
            out.writeByte(255);     //Face
            out.write(FishingBot.getInstance().getPlayer().getSlotData().toByteArray());  //Slot
            out.writeByte(0);       //Cursor X
            out.writeByte(0);       //Cursor Y
            out.writeByte(0);       //Cursor Z
            new Thread(() -> {
                try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }
                networkHandler.sendPacket(new PacketOutArmAnimation());
            }).start();
            break;
        }
        case ProtocolConstants.MINECRAFT_1_13_2:
        case ProtocolConstants.MINECRAFT_1_13_1:
        case ProtocolConstants.MINECRAFT_1_13:
        case ProtocolConstants.MINECRAFT_1_12_2:
        case ProtocolConstants.MINECRAFT_1_12_1:
        case ProtocolConstants.MINECRAFT_1_12:
        case ProtocolConstants.MINECRAFT_1_11_1:
        case ProtocolConstants.MINECRAFT_1_11:
        case ProtocolConstants.MINECRAFT_1_10:
        case ProtocolConstants.MINECRAFT_1_9_4:
        case ProtocolConstants.MINECRAFT_1_9_2:
        case ProtocolConstants.MINECRAFT_1_9_1:
        case ProtocolConstants.MINECRAFT_1_9:
        case ProtocolConstants.MINECRAFT_1_14:
        case ProtocolConstants.MINECRAFT_1_14_1:
        case ProtocolConstants.MINECRAFT_1_14_2:
        case ProtocolConstants.MINECRAFT_1_14_3:
        case ProtocolConstants.MINECRAFT_1_14_4:
        default: {
            out.writeByte(0);       //main hand
            break;
        }
    }
}
 
Example 6
Source File: PacketOutClientSettings.java    From FishingBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void write(ByteArrayDataOutput out, int protocolId) {
    switch (protocolId) {
        case ProtocolConstants.MINECRAFT_1_8: {
            writeString("en_7s", out);  //use speach "Pirate Speak", arrr
            out.writeByte(1);           //render-distance
            out.writeByte(0);           //chat enabled
            out.writeBoolean(true);     //support colors
            out.writeByte(128);         //skin bitmask
            break;
        }
        case ProtocolConstants.MINECRAFT_1_13_2:
        case ProtocolConstants.MINECRAFT_1_13_1:
        case ProtocolConstants.MINECRAFT_1_13:
        case ProtocolConstants.MINECRAFT_1_12_2:
        case ProtocolConstants.MINECRAFT_1_12_1:
        case ProtocolConstants.MINECRAFT_1_12:
        case ProtocolConstants.MINECRAFT_1_11_1:
        case ProtocolConstants.MINECRAFT_1_11:
        case ProtocolConstants.MINECRAFT_1_10:
        case ProtocolConstants.MINECRAFT_1_9_4:
        case ProtocolConstants.MINECRAFT_1_9_2:
        case ProtocolConstants.MINECRAFT_1_9_1:
        case ProtocolConstants.MINECRAFT_1_9:
        case ProtocolConstants.MINECRAFT_1_14:
        case ProtocolConstants.MINECRAFT_1_14_1:
        case ProtocolConstants.MINECRAFT_1_14_2:
        case ProtocolConstants.MINECRAFT_1_14_3:
        case ProtocolConstants.MINECRAFT_1_14_4:
        default: {
            writeString("lol_aa", out); //use speach "LOLCAT", lol
            out.writeByte(1);           //render-distance
            writeVarInt(0, out);        //chat enabled
            out.writeBoolean(true);     //support colors
            out.writeByte(128);         //skin bitmask
            writeVarInt(1, out);        //right = main hand
            break;
        }
    }
}
 
Example 7
Source File: VectorIterator.java    From Indra with MIT License 5 votes vote down vote up
private void parseHeadline(LittleEndianDataInputStream input) throws IOException {
    ByteArrayDataOutput out = ByteStreams.newDataOutput();
    byte c;

    while((c = input.readByte()) != '\n') {
        out.writeByte(c);
    }

    String[] headline = new String(out.toByteArray(), StandardCharsets.UTF_8).split(" ");
    this.numberOfVectors = Integer.parseInt(headline[0]);
    this.dimensions = Integer.parseInt(headline[1]);
}
 
Example 8
Source File: ClassWriter.java    From turbine with Apache License 2.0 5 votes vote down vote up
static void writeConstantPool(ConstantPool constantPool, ByteArrayDataOutput output) {
  output.writeShort(constantPool.nextEntry);
  for (ConstantPool.Entry e : constantPool.constants()) {
    output.writeByte(e.kind().tag());
    Value value = e.value();
    switch (e.kind()) {
      case CLASS_INFO:
      case STRING:
      case MODULE:
      case PACKAGE:
        output.writeShort(((IntValue) value).value());
        break;
      case INTEGER:
        output.writeInt(((IntValue) value).value());
        break;
      case DOUBLE:
        output.writeDouble(((DoubleValue) value).value());
        break;
      case FLOAT:
        output.writeFloat(((FloatValue) value).value());
        break;
      case LONG:
        output.writeLong(((LongValue) value).value());
        break;
      case UTF8:
        output.writeUTF(((StringValue) value).value());
        break;
    }
  }
}
 
Example 9
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 10
Source File: ModListener.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
private static byte[] getSchematicaPayload() {
    final ByteArrayDataOutput output = ByteStreams.newDataOutput();
    output.writeByte(0);
    output.writeBoolean(false);
    output.writeBoolean(false);
    output.writeBoolean(false);
    return output.toByteArray();
}
 
Example 11
Source File: PlayerSqlProtocol.java    From PlayerSQL with GNU General Public License v2.0 5 votes vote down vote up
@SneakyThrows
public byte[] encode() {
    ByteArrayDataOutput buf = ByteStreams.newDataOutput();
    buf.writeByte(protocol.ordinal());
    write(buf);
    return buf.toByteArray();
}
 
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: MoCEntityGolem.java    From mocreaturesdev with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeSpawnData(ByteArrayDataOutput data)
{
    for (int i = 0; i < 23; i++)
    {
        data.writeByte(golemCubes[i]);
    }
}
 
Example 14
Source File: MessageSerDe.java    From suro with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(Message payload) {
    try {
        ByteArrayDataOutput out = new ByteArrayDataOutputStream(outputStream.get());
        out.writeByte(Message.classMap.inverse().get(payload.getClass()));
        payload.write(out);
        return out.toByteArray();
    } catch (IOException e) {
        log.error("Exception on serialize: " + e.getMessage(), e);
        return new byte[]{};
    }
}
 
Example 15
Source File: AbstractPacket.java    From ExtraCells1 with MIT License 5 votes vote down vote up
public final Packet makePacket()
{
	ByteArrayDataOutput out = ByteStreams.newDataOutput();
	out.writeByte(getPacketId());
	write(out);
	return PacketDispatcher.getPacket(CHANNEL, out.toByteArray());
}
 
Example 16
Source File: PacketTerminalFluid.java    From ExtraCells1 with MIT License 5 votes vote down vote up
@Override
public void write(ByteArrayDataOutput out)
{
	out.writeByte(type);
	out.writeInt(world.provider.dimensionId);
	out.writeInt(x);
	out.writeInt(y);
	out.writeInt(z);
	out.writeInt(fluidID);
	out.writeInt(amount);
}