rendered paste bodyimport java.io.DataInputStream;
import java.io.InputStream;
import java.util.Random;
import javax.microedition.io.SocketConnection;
public class ChatInThread extends Thread {
InputStream is;
DataInputStream dis;
ChatSession session;
private byte[] pkg;
public ChatInThread(InputStream is, ChatSession session) {
this.session = session;
try {
dis = new DataInputStream(is);
} catch (Exception e) {
}
}
public void run() {
while(true){
// System.out.println("PACKAGE");
session.inPackage(readPacket());
try{Thread.sleep(100);}catch(Exception e){}
}
}
public byte[] readPkg() {
byte[] pkg = null;
try {
pkg = new byte[is.read()&0xff];
pkg[0] = (byte) pkg.length;
for (int i = 1; i < pkg.length; i++) {
pkg[i] = (byte) is.read();
}
} catch (Exception e) {
}
return pkg;
}
public byte[] readPacket() {
byte[] packet=null;
try {
while (dis.available() < 2);
int packetLen = getPacketSize(dis.readByte(),dis.readByte());
while (dis.available() < packetLen-2);
// System.out.println("LEN "+packetLen);
packet = new byte[packetLen];
packet[0] = (byte)packetLen;
for (int i = 2; i < packetLen; i++) {
packet[i] = dis.readByte();
}
} catch (Exception e) {
System.out.println("CzatIn "+ e);
}
return packet;
}
public int getPacketSize(byte a, byte b){
String sa = Integer.toHexString((int)(a&0xff));
String sb = Integer.toHexString((int)(b&0xff));
if(sa.length()==1)
sa="0"+sa;
if(sb.length()==1)
sb="0"+sb;
return Integer.parseInt(sb+sa,16);
}
}