下面列出了com.google.common.io.ByteArrayDataOutput#writeByte ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
}
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;
}
}
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;
}
}
}
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;
}
@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;
}
}
}
@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;
}
}
}
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]);
}
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;
}
}
}
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);
}
private static byte[] getSchematicaPayload() {
final ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.writeByte(0);
output.writeBoolean(false);
output.writeBoolean(false);
output.writeBoolean(false);
return output.toByteArray();
}
@SneakyThrows
public byte[] encode() {
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeByte(protocol.ordinal());
write(buf);
return buf.toByteArray();
}
@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;
}
}
@Override
public void writeSpawnData(ByteArrayDataOutput data)
{
for (int i = 0; i < 23; i++)
{
data.writeByte(golemCubes[i]);
}
}
@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[]{};
}
}
public final Packet makePacket()
{
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeByte(getPacketId());
write(out);
return PacketDispatcher.getPacket(CHANNEL, out.toByteArray());
}
@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);
}