If you think the Android project Freebloks-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package de.saschahlusiak.freebloks.network;
/*fromwww.java2s.com*/import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.Socket;
publicclass NET_HEADER implements Serializable {
privatestaticfinallong serialVersionUID = 1L;
/* int check1; *//* uint8 */publicint data_length; /* uint16 */publicint msg_type; /* uint8 *//* int check2; *//* uint8 */publicstaticfinalint HEADER_SIZE = 5;
byte buffer[];
public NET_HEADER(int msg_type, int data_length) {
this.msg_type = msg_type;
this.data_length = data_length;
}
public NET_HEADER(NET_HEADER from) {
this.data_length = from.data_length;
this.msg_type = from.msg_type;
this.buffer = from.buffer;
}
/**
* a java byte is always signed, being -128..127
* casting (byte)-1 to int will result in (int)-1
*
*/publicstaticfinalint unsigned(byte b) {
return (b & 0xFF);
}
boolean read(Socket socket, boolean block) throws Exception {
InputStream is;
int r;
int check1, check2;
if (socket == null)
return false;
if (socket.isInputShutdown())
return false;
buffer = newbyte[HEADER_SIZE];
is = socket.getInputStream();
if (!block && (is.available() < HEADER_SIZE))
return false;
r = is.read(buffer, 0, HEADER_SIZE);
if (r < HEADER_SIZE)
thrownew Exception("read error");
check1 = unsigned(buffer[0]);
data_length = unsigned(buffer[1]) << 8 | unsigned(buffer[2]);
msg_type = buffer[3];
check2 = unsigned(buffer[4]);
if (data_length < HEADER_SIZE)
thrownew Exception("invalid header data length");
/* Beiden Checksums erneut berechnen */int c1 = (byte) (data_length & 0x0055) ^ msg_type;
int c2 = (c1 ^ 0xD6) + msg_type;
/* Bei Ungleichheit Fehler, sonst Nachricht ok */if (c1 != check1 || c2 != check2)
thrownew Exception("header checksum failed");
data_length -= HEADER_SIZE;
if (is.available() < data_length)
thrownew Exception("short read for package payload");
buffer = newbyte[data_length];
r = is.read(buffer, 0, data_length);
if (r < data_length)
thrownew Exception("short read for package payload");
return true;
}
void prepare(ByteArrayOutputStream bos) {
int l = data_length + HEADER_SIZE;
int check1, check2;
check1 = (l & 0x0055) ^ msg_type;
check2 = (check1 ^ 0xD6) + msg_type;
bos.write(check1);
bos.write((l >> 8) & 0xff);
bos.write(l & 0xff);
bos.write(msg_type);
bos.write(check2);
}
publicboolean send(Socket socket) {
if (socket == null)
return false;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
prepare(bos);
try {
socket.getOutputStream().write(bos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}