changed converters to static

This commit is contained in:
Marcin Kostrzewski 2020-10-29 11:36:15 +01:00
parent 5a767c8cac
commit 609e39c771

View File

@ -1,13 +1,23 @@
package Main; package Main;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class ProtocolMessage { public class ProtocolMessage {
private static final Logger logger = LogManager.getLogger(App.class);
private int length; private int length;
private char[] message; private char[] message;
private char[] raw; private char[] raw;
/** Convert an integer into 4-byte array */ /** Convert an integer into 4-byte array */
private byte[] intToByte(int value) { public static byte[] intToByte(int value) {
return new byte[] { return new byte[] {
(byte)(value >>> 24), (byte)(value >>> 24),
(byte)(value >>> 16), (byte)(value >>> 16),
@ -16,7 +26,7 @@ public class ProtocolMessage {
} }
/** Convert a byte array into 4-byte integer */ /** Convert a byte array into 4-byte integer */
private int byteToInt(byte[] bytes) { public static int byteToInt(byte[] bytes) {
return bytes[0] << 24 | return bytes[0] << 24 |
(bytes[1] & 0xFF) << 16 | (bytes[1] & 0xFF) << 16 |
(bytes[2] & 0xFF) << 8 | (bytes[2] & 0xFF) << 8 |
@ -69,6 +79,27 @@ public class ProtocolMessage {
} }
public void sendToStream(Writer writer) throws IOException {
writer.write(this.raw);
writer.flush();
}
public void readFromStream(Reader reader) throws IOException {
int totalBytesRead = 0;
// Read first 4 bytes containing the length of the incoming message
while(totalBytesRead != 4){
int bytesRead = reader.read(this.raw, totalBytesRead, 4 - totalBytesRead);
if (bytesRead == -1) {
logger.log(Level.ERROR, "Invalid packet.");
return;
} else
totalBytesRead += bytesRead;
}
}
public int getLength() { public int getLength() {
return length; return length;
} }