From 6b0fe35c8f288792dad90cce0b95d951cb816362 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 16 Oct 2017 22:12:35 -0700 Subject: [PATCH] use readFully() rather than read() readFully() blocks; read() can return with less than you expect. --- .../org/eehouse/android/xw4/BTService.java | 32 ++++++++----------- .../org/eehouse/android/xw4/BiDiSockWrap.java | 6 ++-- .../org/eehouse/android/xw4/NetUtils.java | 2 +- .../eehouse/android/xw4/RefreshNamesTask.java | 2 +- .../org/eehouse/android/xw4/RelayService.java | 12 +++---- .../org/eehouse/android/xw4/SMSService.java | 4 +-- 6 files changed, 24 insertions(+), 34 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java index 70df610ca..1fb0a9c3f 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BTService.java @@ -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 ); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BiDiSockWrap.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BiDiSockWrap.java index 9b97432d5..e2744c9f5 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BiDiSockWrap.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BiDiSockWrap.java @@ -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 ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NetUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NetUtils.java index 62ff478da..78c43b68e 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NetUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NetUtils.java @@ -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; } } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RefreshNamesTask.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RefreshNamesTask.java index f94f04725..9844be6d8 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RefreshNamesTask.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RefreshNamesTask.java @@ -94,7 +94,7 @@ public class RefreshNamesTask extends AsyncTask { // 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 ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java index 740aa945d..89c24ebb4 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/RelayService.java @@ -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; } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java index 38c1bef61..c5a226eec 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java @@ -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; }