在此期间,我开发了一个android应用程序,使用Object [Input / Output] Stream发送/接收消息。以下代码为send方法:
public void sendMsg(Message msg) {
try {
if(connectedFalg) {
outputStream.reset();
outputStream.writeObject(msg);
outputStream.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
同时,有一个线程一直接收消息,代码如下:
while(true) {
try {
Message msg = (Message) inputStream.readObject();
BufferManager.INSTANCE.addReceiveQueue(msg);
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在我的项目中,我有三个球,一个球是目标球,另一个是由加速传感器的手机控制。首先我发送1号球的状态,然后发送2号球或目标球,然后会有一些例外。喜欢:
java.io.StreamCorruptedException:Wrong format:56
05-15 21:53:02.220: W/System.err(5923): at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:676)
05-15 21:53:02.230: W/System.err(5923): at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:789)
有时它会遇到这个问题:
05-16 15:42:28.380: W/System.err(14690): java.io.UTFDataFormatException: bad byte at 122
05-16 15:42:28.380:W / System.err(14690):at java.nio.charset.ModifiedUtf8.decode(ModifiedUtf8.java:60) 05-16 15:42:28.380:W / System.err(14690):at java.io.DataInputStream.decodeUTF(DataInputStream.java:179) 05-16 15:42:28.380:W / System.err(14690):at java.io.DataInputStream.decodeUTF(DataInputStream.java:173) 05-16 15:42:28.380:W / System.err(14690):at java.io.DataInputStream.readUTF(DataInputStream.java:169) 05-16 15:42:28.380:W / System.err(14690):at java.io.ObjectInputStream.readFieldDescriptors(ObjectInputStream.java:895) 05-16 15:42:28.380:W / System.err(14690):at java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:1719) 05-16 15:42:28.380:W / System.err(14690):at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1631) 05-16 15:42:28.380:W / System.err(14690):at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:658) 05-16 15:42:28.380:W / System.err(14690):at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1781) 05-16 15:42:28.380:W / System.err(14690):at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762) 05-16 15:42:28.380:W / System.err(14690):at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981) 05-16 15:42:28.380:W / System.err(14690):at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
这是项目https://github.com/Aresxiao/DataStreamEngine/tree/dev
如果两个球碰撞,那么会调用collisionCalculate()方法,方法如下:
public boolean collisionCalculate(float ballaTempXY[],AbstractBall balla,AbstractBall ballb)
{
Value bvalue = KVStoreInMemory.INSTANCE.getVersionValue(new Key(ballb.getBallId())).getValue();
float[] bloc = bvalue.getLoc();
float BAx = ballaTempXY[0] - bloc[0];
float BAy = ballaTempXY[1] - bloc[1];
float mvBA = mould(new float[]{BAx, BAy});
if(mvBA > (balla.getRadius() + ballb.getRadius())){
return false;
}
float[] bv = bvalue.getV();
float vB = (float)Math.sqrt(bv[0] * bv[0] + bv[1] * bv[1]);
float vbCollX = 0;
float vbCollY = 0;
//垂直方向的Xy分速度
float vbVerticalX=0;
float vbVerticalY=0;
if(balla.getVMin() < vB)
{
float bAngle=angle
(
new float[]{bv[0],bv[1]},
new float[]{BAx,BAy}
);
float vbColl=vB * (float)Math.cos(bAngle);
vbCollX = (vbColl/mvBA) * BAx;
vbCollY = (vbColl/mvBA) * BAy;
vbVerticalX = bv[0] - vbCollX;
vbVerticalY = bv[1] - vbCollY;
}
Value avalue = KVStoreInMemory.INSTANCE.getVersionValue(new Key(balla.getBallId())).getValue();
float[] av = avalue.getV();
float vA = (float)Math.sqrt(av[0] * av[0] + av[1] * av[1]);
//平行方向的Xy分速度
float vaCollX = 0;
float vaCollY = 0;
//垂直方向的Xy分速度
float vaVerticalX = 0;
float vaVerticalY = 0;
if(balla.getVMin() < vA)
{
float aAngle=angle
(
new float[]{av[0],av[1]},
new float[]{BAx,BAy}
);
float vaColl=vA*(float)Math.cos(aAngle);
vaCollX=(vaColl/mvBA)*BAx;
vaCollY=(vaColl/mvBA)*BAy;
vaVerticalX = av[0]-vaCollX;
vaVerticalY = av[1]-vaCollY;
}
av[0] = vaVerticalX + vbCollX;
av[1] = vaVerticalY + vbCollY;
avalue.setV(av[0], av[1]);
RegisterControllerFactory.INSTANCE.getRegisterController().write(new Key(balla.getBallId()), avalue);
System.out.println("write balla "+balla.getBallId());
bv[0] = vbVerticalX + vaCollX;
bv[1] = vbVerticalY + vaCollY;
bvalue.setV(bv[0], bv[1]);
RegisterControllerFactory.INSTANCE.getRegisterController().write(new Key(ballb.getBallId()), bvalue);
System.out.println("write ballb "+ballb.getBallId());
return true;
}