Ajout d'un nouveau packet pour PandacubeCore + Gestion d'octet bit à bit

This commit is contained in:
Marc Baloup 2016-11-22 21:32:18 +01:00
parent 774fac4c64
commit cae491bc20
2 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package fr.pandacube.java.util.network.packet.bytebuffer;
import java.util.Arrays;
public class Array8Bit {
public static final int BIT_COUNT = 8;
private boolean[] values = new boolean[BIT_COUNT];
/**
*
* @param b unsigned integer value. Lowest significant bit will be used.
*/
public Array8Bit(int b) {
for (int i = 0; i<BIT_COUNT; i++) {
values[i] = (b % 2 == 1);
b >>= 1;
}
}
public Array8Bit(boolean[] bits) {
if (bits == null || bits.length != BIT_COUNT)
throw new IllegalArgumentException("bits is null or bits.length != "+BIT_COUNT);
values = Arrays.copyOf(bits, BIT_COUNT);
}
/**
* i = 0 is the lowest significant bit
* @param i
* @return
*/
public boolean getValue(int i) {
return values[i];
}
/**
* i = 0 is the lowest significant bit
* @param i
* @param b
*/
public void setValue(int i, boolean b) {
values[i] = b;
}
public byte getValuesAsByte() {
byte b = 0;
for (int i=BIT_COUNT-1; i>=0; i--) {
b <<= 1;
if (values[i]) b |= 1;
}
return b;
}
}

View File

@ -0,0 +1,30 @@
package fr.pandacube.java.util.network.packet.packets.global;
import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketServerCommand extends PacketServer {
private String command;
private boolean async;
private boolean returnResult;
public PacketServerCommand() {
super((byte)0xD2);
}
@Override
public void serializeToByteBuffer(ByteBuffer buffer) {
buffer.putString(command);
buffer.putByte((byte) (async ? 1 : 0));
buffer.putByte((byte) (returnResult ? 1 : 0));
}
@Override
public void deserializeFromByteBuffer(ByteBuffer buffer) {
command = buffer.getString();
async = buffer.getByte() != 0;
returnResult = buffer.getByte() != 0;
}
}