use readFully() rather than read()

readFully() blocks; read() can return with less than you expect.
This commit is contained in:
Eric House 2017-10-16 22:12:35 -07:00
parent 927c4f12a0
commit 6b0fe35c8f
6 changed files with 24 additions and 34 deletions

View file

@ -550,7 +550,7 @@ public class BTService extends XWService {
} else {
short len = is.readShort();
byte[] nliData = new byte[len];
is.read( nliData );
is.readFully( nliData );
nli = XwJNI.nliFromStream( nliData );
}
@ -573,26 +573,20 @@ public class BTService extends XWService {
int gameID = dis.readInt();
switch ( cmd ) {
case MESG_SEND:
short len = dis.readShort();
byte[] buffer = new byte[len];
int nRead = dis.read( buffer, 0, len );
if ( nRead == len ) {
BluetoothDevice host = socket.getRemoteDevice();
addAddr( host );
byte[] buffer = new byte[dis.readShort()];
dis.readFully( buffer );
BluetoothDevice host = socket.getRemoteDevice();
addAddr( host );
CommsAddrRec addr = new CommsAddrRec( host.getName(),
host.getAddress() );
ReceiveResult rslt
= BTService.this.receiveMessage( BTService.this,
gameID, m_btMsgSink,
buffer, addr );
CommsAddrRec addr = new CommsAddrRec( host.getName(),
host.getAddress() );
ReceiveResult rslt
= BTService.this.receiveMessage( BTService.this,
gameID, m_btMsgSink,
buffer, addr );
result = rslt == ReceiveResult.GAME_GONE ?
BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT;
} else {
Log.e( TAG, "receiveMessage(): read only %d of %d bytes",
nRead, len );
}
result = rslt == ReceiveResult.GAME_GONE ?
BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT;
break;
case MESG_GAMEGONE:
postEvent( MultiEvent.MESSAGE_NOGAME, gameID );

View file

@ -188,10 +188,8 @@ public class BiDiSockWrap {
DataInputStream inStream
= new DataInputStream( mSocket.getInputStream() );
while ( mRunThreads ) {
short len = inStream.readShort();
Log.d( TAG, "got len: %d", len );
byte[] packet = new byte[len];
inStream.read( packet );
byte[] packet = new byte[inStream.readShort()];
inStream.readFully( packet );
mIface.gotPacket( BiDiSockWrap.this, packet );
}
} catch( IOException ioe ) {

View file

@ -184,7 +184,7 @@ public class NetUtils {
short len = dis.readShort();
if ( len > 0 ) {
byte[] packet = new byte[len];
dis.read( packet );
dis.readFully( packet );
msgs[ii][jj] = packet;
}
}

View file

@ -94,7 +94,7 @@ public class RefreshNamesTask extends AsyncTask<Void, Void, String[]> {
// Can't figure out how to read a null-terminated string
// from DataInputStream so parse it myself.
byte[] bytes = new byte[len];
dis.read( bytes );
dis.readFully( bytes );
int index = -1;
for ( int ii = 0; ii < nRooms; ++ii ) {

View file

@ -736,7 +736,7 @@ public class RelayService extends XWService
case XWPDEV_MSG:
int token = dis.readInt();
byte[] msg = new byte[dis.available()];
dis.read( msg );
dis.readFully( msg );
postData( this, token, msg );
// game-related packets only count
@ -756,9 +756,8 @@ public class RelayService extends XWService
resetBackoff = true;
intent = getIntentTo( this, MsgCmds.GOT_INVITE );
int srcDevID = dis.readInt();
short len = dis.readShort();
byte[] nliData = new byte[len];
dis.read( nliData );
byte[] nliData = new byte[dis.readShort()];
dis.readFully( nliData );
NetLaunchInfo nli = XwJNI.nliFromStream( nliData );
intent.putExtra( INVITE_FROM, srcDevID );
String asStr = nli.toString();
@ -995,9 +994,8 @@ public class RelayService extends XWService
private String getVLIString( DataInputStream dis )
throws java.io.IOException
{
int len = vli2un( dis );
byte[] tmp = new byte[len];
dis.read( tmp );
byte[] tmp = new byte[vli2un( dis )];
dis.readFully( tmp );
String result = new String( tmp );
return result;
}

View file

@ -522,7 +522,7 @@ public class SMSService extends XWService {
case DATA:
int gameID = dis.readInt();
byte[] rest = new byte[dis.available()];
dis.read( rest );
dis.readFully( rest );
if ( feedMessage( gameID, rest, new CommsAddrRec( phone ) ) ) {
SMSResendReceiver.resetTimer( this );
}
@ -618,7 +618,7 @@ public class SMSService extends XWService {
} else {
SMS_CMD cmd = SMS_CMD.values()[dis.readByte()];
byte[] rest = new byte[dis.available()];
dis.read( rest );
dis.readFully( rest );
receive( cmd, rest, senderPhone );
success = true;
}