public void testNewDataOutput_writeByte() { ByteArrayDataOutput out = ByteStreams.newDataOutput(); out.write(0x12); out.writeByte(0x34); assertEquals(new byte[] {0x12, 0x34}, out.toByteArray()); }
byte[] encode(byte[] states) { // calculate the number of differences int n = 0; for (int i = 0; i < states.length; i++) { byte state = states[i]; byte defaultState = this.defaultStates[i]; if (state == defaultState) { continue; } n++; } if (n == 0) { return new byte[0]; } ByteArrayDataOutput out = ByteStreams.newDataOutput(); // write the number of differences to "expect" out.writeByte((byte) n); for (int i = 0; i < states.length; i++) { byte state = states[i]; byte defaultState = this.defaultStates[i]; if (state == defaultState) { continue; } byte settingOrdinal = (byte) i; out.writeByte(settingOrdinal); out.writeByte(state); } return out.toByteArray(); }
@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 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; }
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]); }
public void delayedDig(long pos, byte face){ try { ByteArrayDataOutput buf = ByteStreams.newDataOutput(); Packet.writeVarInt(buf, 7); buf.writeByte(2); buf.writeLong(pos); buf.writeByte(face); c.net.sendPacket(buf, c.out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void closeInventory() { ByteArrayDataOutput buf = ByteStreams.newDataOutput(); try { Packet.writeVarInt(buf, 13); buf.writeByte(0); c.net.sendPacket(buf, c.out); } catch (IOException e) { e.printStackTrace(); } }
@Override public void runCommand(Client c, String command, String[] args, String username) { try { ByteArrayDataOutput buf = ByteStreams.newDataOutput(); Packet.writeVarInt(buf, 22); buf.writeByte(0); c.net.sendPacket(buf, c.out); //c.chat.sendMessage("Respawned!"); } catch (IOException e) { e.printStackTrace(); } }; });
public void placeBlock(int x, int y, int z, int id, int face){ ByteArrayDataOutput buf = ByteStreams.newDataOutput(); try { long pos = ((long)(x & 0x3FFFFFF) << 38) | ((long)(y & 0xFFF) << 26) | z & 0x3FFFFFF; Packet.writeVarInt(buf, 8); buf.writeLong(pos); buf.writeByte(face); new Slot(0,(short)id,(byte)1,(short)0,(byte)0).sendSlot(buf); buf.writeByte(0); buf.writeByte(0); buf.writeByte(0); c.net.sendPacket(buf, c.out); buf = ByteStreams.newDataOutput(); Packet.writeVarInt(buf, 10); c.net.sendPacket(buf, c.out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void click(byte windowID, short num, byte button, byte mode) { ByteArrayDataOutput buf = ByteStreams.newDataOutput(); Slot s; if (num>0&&num<=44) { s = inventory.get(num); } else { s = new Slot(-999,(short)-1,(byte)0,(short)0,(byte)0); } try { //write the clicking info Packet.writeVarInt(buf, 14); buf.writeByte(windowID); buf.writeShort(num); buf.writeByte(button); buf.writeShort(actionNumber); actionNumber++; buf.writeByte(mode); //write the slot structure we have s.sendSlot(buf); c.net.sendPacket(buf, c.out); } catch (IOException e) { e.printStackTrace(); } }
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<>(getQueriedKeys()); 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; } } } }
@Override public void read(Client c, int len, ByteArrayDataInputWrapper buf) throws IOException{ c.entityID = buf.readInt(); c.gamemode = buf.readByte(); c.dimension = buf.readByte(); c.difficulty = buf.readByte(); c.maxplayer = buf.readByte(); System.out.println("Level type is: " + getString(buf)); ByteArrayDataOutput buff = ByteStreams.newDataOutput(); try{ Packet.writeVarInt(buff, 4); Packet.writeString(buff, "en_GB"); buff.writeByte(0); Packet.writeVarInt(buff, 0); buff.writeBoolean(true); buff.writeByte(0); Packet.writeVarInt(buff, 1); c.net.sendPacket(buff, c.out); }catch(IOException e){ e.printStackTrace(); } }
public void testNewDataOutput_writeByte() { ByteArrayDataOutput out = ByteStreams.newDataOutput(); out.write(0x12); out.writeByte(0x34); assertEquals(new byte[] {0x12, 0x34}, out.toByteArray()); }
@Override protected <V> void onMissingData(DataKey<V> key) { super.onMissingData(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(() -> { plugin.getLogger().log(Level.SEVERE, "Unexpected exception", th); }); requestAll = true; } }