mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-08 05:24:39 +01:00
use readFully() rather than read()
readFully() blocks; read() can return with less than you expect.
This commit is contained in:
parent
927c4f12a0
commit
6b0fe35c8f
6 changed files with 24 additions and 34 deletions
|
@ -550,7 +550,7 @@ public class BTService extends XWService {
|
||||||
} else {
|
} else {
|
||||||
short len = is.readShort();
|
short len = is.readShort();
|
||||||
byte[] nliData = new byte[len];
|
byte[] nliData = new byte[len];
|
||||||
is.read( nliData );
|
is.readFully( nliData );
|
||||||
nli = XwJNI.nliFromStream( nliData );
|
nli = XwJNI.nliFromStream( nliData );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -573,10 +573,8 @@ public class BTService extends XWService {
|
||||||
int gameID = dis.readInt();
|
int gameID = dis.readInt();
|
||||||
switch ( cmd ) {
|
switch ( cmd ) {
|
||||||
case MESG_SEND:
|
case MESG_SEND:
|
||||||
short len = dis.readShort();
|
byte[] buffer = new byte[dis.readShort()];
|
||||||
byte[] buffer = new byte[len];
|
dis.readFully( buffer );
|
||||||
int nRead = dis.read( buffer, 0, len );
|
|
||||||
if ( nRead == len ) {
|
|
||||||
BluetoothDevice host = socket.getRemoteDevice();
|
BluetoothDevice host = socket.getRemoteDevice();
|
||||||
addAddr( host );
|
addAddr( host );
|
||||||
|
|
||||||
|
@ -589,10 +587,6 @@ public class BTService extends XWService {
|
||||||
|
|
||||||
result = rslt == ReceiveResult.GAME_GONE ?
|
result = rslt == ReceiveResult.GAME_GONE ?
|
||||||
BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT;
|
BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT;
|
||||||
} else {
|
|
||||||
Log.e( TAG, "receiveMessage(): read only %d of %d bytes",
|
|
||||||
nRead, len );
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case MESG_GAMEGONE:
|
case MESG_GAMEGONE:
|
||||||
postEvent( MultiEvent.MESSAGE_NOGAME, gameID );
|
postEvent( MultiEvent.MESSAGE_NOGAME, gameID );
|
||||||
|
|
|
@ -188,10 +188,8 @@ public class BiDiSockWrap {
|
||||||
DataInputStream inStream
|
DataInputStream inStream
|
||||||
= new DataInputStream( mSocket.getInputStream() );
|
= new DataInputStream( mSocket.getInputStream() );
|
||||||
while ( mRunThreads ) {
|
while ( mRunThreads ) {
|
||||||
short len = inStream.readShort();
|
byte[] packet = new byte[inStream.readShort()];
|
||||||
Log.d( TAG, "got len: %d", len );
|
inStream.readFully( packet );
|
||||||
byte[] packet = new byte[len];
|
|
||||||
inStream.read( packet );
|
|
||||||
mIface.gotPacket( BiDiSockWrap.this, packet );
|
mIface.gotPacket( BiDiSockWrap.this, packet );
|
||||||
}
|
}
|
||||||
} catch( IOException ioe ) {
|
} catch( IOException ioe ) {
|
||||||
|
|
|
@ -184,7 +184,7 @@ public class NetUtils {
|
||||||
short len = dis.readShort();
|
short len = dis.readShort();
|
||||||
if ( len > 0 ) {
|
if ( len > 0 ) {
|
||||||
byte[] packet = new byte[len];
|
byte[] packet = new byte[len];
|
||||||
dis.read( packet );
|
dis.readFully( packet );
|
||||||
msgs[ii][jj] = packet;
|
msgs[ii][jj] = packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,7 +94,7 @@ public class RefreshNamesTask extends AsyncTask<Void, Void, String[]> {
|
||||||
// Can't figure out how to read a null-terminated string
|
// Can't figure out how to read a null-terminated string
|
||||||
// from DataInputStream so parse it myself.
|
// from DataInputStream so parse it myself.
|
||||||
byte[] bytes = new byte[len];
|
byte[] bytes = new byte[len];
|
||||||
dis.read( bytes );
|
dis.readFully( bytes );
|
||||||
|
|
||||||
int index = -1;
|
int index = -1;
|
||||||
for ( int ii = 0; ii < nRooms; ++ii ) {
|
for ( int ii = 0; ii < nRooms; ++ii ) {
|
||||||
|
|
|
@ -736,7 +736,7 @@ public class RelayService extends XWService
|
||||||
case XWPDEV_MSG:
|
case XWPDEV_MSG:
|
||||||
int token = dis.readInt();
|
int token = dis.readInt();
|
||||||
byte[] msg = new byte[dis.available()];
|
byte[] msg = new byte[dis.available()];
|
||||||
dis.read( msg );
|
dis.readFully( msg );
|
||||||
postData( this, token, msg );
|
postData( this, token, msg );
|
||||||
|
|
||||||
// game-related packets only count
|
// game-related packets only count
|
||||||
|
@ -756,9 +756,8 @@ public class RelayService extends XWService
|
||||||
resetBackoff = true;
|
resetBackoff = true;
|
||||||
intent = getIntentTo( this, MsgCmds.GOT_INVITE );
|
intent = getIntentTo( this, MsgCmds.GOT_INVITE );
|
||||||
int srcDevID = dis.readInt();
|
int srcDevID = dis.readInt();
|
||||||
short len = dis.readShort();
|
byte[] nliData = new byte[dis.readShort()];
|
||||||
byte[] nliData = new byte[len];
|
dis.readFully( nliData );
|
||||||
dis.read( nliData );
|
|
||||||
NetLaunchInfo nli = XwJNI.nliFromStream( nliData );
|
NetLaunchInfo nli = XwJNI.nliFromStream( nliData );
|
||||||
intent.putExtra( INVITE_FROM, srcDevID );
|
intent.putExtra( INVITE_FROM, srcDevID );
|
||||||
String asStr = nli.toString();
|
String asStr = nli.toString();
|
||||||
|
@ -995,9 +994,8 @@ public class RelayService extends XWService
|
||||||
private String getVLIString( DataInputStream dis )
|
private String getVLIString( DataInputStream dis )
|
||||||
throws java.io.IOException
|
throws java.io.IOException
|
||||||
{
|
{
|
||||||
int len = vli2un( dis );
|
byte[] tmp = new byte[vli2un( dis )];
|
||||||
byte[] tmp = new byte[len];
|
dis.readFully( tmp );
|
||||||
dis.read( tmp );
|
|
||||||
String result = new String( tmp );
|
String result = new String( tmp );
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -522,7 +522,7 @@ public class SMSService extends XWService {
|
||||||
case DATA:
|
case DATA:
|
||||||
int gameID = dis.readInt();
|
int gameID = dis.readInt();
|
||||||
byte[] rest = new byte[dis.available()];
|
byte[] rest = new byte[dis.available()];
|
||||||
dis.read( rest );
|
dis.readFully( rest );
|
||||||
if ( feedMessage( gameID, rest, new CommsAddrRec( phone ) ) ) {
|
if ( feedMessage( gameID, rest, new CommsAddrRec( phone ) ) ) {
|
||||||
SMSResendReceiver.resetTimer( this );
|
SMSResendReceiver.resetTimer( this );
|
||||||
}
|
}
|
||||||
|
@ -618,7 +618,7 @@ public class SMSService extends XWService {
|
||||||
} else {
|
} else {
|
||||||
SMS_CMD cmd = SMS_CMD.values()[dis.readByte()];
|
SMS_CMD cmd = SMS_CMD.values()[dis.readByte()];
|
||||||
byte[] rest = new byte[dis.available()];
|
byte[] rest = new byte[dis.available()];
|
||||||
dis.read( rest );
|
dis.readFully( rest );
|
||||||
receive( cmd, rest, senderPhone );
|
receive( cmd, rest, senderPhone );
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue