diff options
author | Zhihai Xu <zhihaixu@google.com> | 2013-12-17 11:39:20 -0800 |
---|---|---|
committer | Matthew Xie <mattx@google.com> | 2014-01-13 17:52:13 -0800 |
commit | 85c4f02c03e459009051ad706db3babecccfa5c2 (patch) | |
tree | aa2f39cba34f10e137ef689e9c17a1f971d61f49 /framework/java/android/bluetooth/BluetoothSocket.java | |
parent | b4422d237f39921deaa51f410653658d2e3dc262 (diff) |
NPE in BluetoothSocket.write()
If calling connect succeed, we should not see this NPE.
An IOException may happen after call BluetoothSocket.connect.
If you still call write after the IOException,
you will get this NPE.
add NPE protection for possible wrong calling sequence from application,
To make bluetoothSocket more error-tolerant.
bug:12104154
Change-Id: I7fa4e847b500ca9b9d2a43df432f31a1bb016c0a
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothSocket.java')
-rw-r--r-- | framework/java/android/bluetooth/BluetoothSocket.java | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/framework/java/android/bluetooth/BluetoothSocket.java b/framework/java/android/bluetooth/BluetoothSocket.java index d10eaea2fb..1e75fc2a1c 100644 --- a/framework/java/android/bluetooth/BluetoothSocket.java +++ b/framework/java/android/bluetooth/BluetoothSocket.java @@ -417,27 +417,28 @@ public final class BluetoothSocket implements Closeable { * if an i/o error occurs. */ /*package*/ void flush() throws IOException { + if (mSocketOS == null) throw new IOException("flush is called on null OutputStream"); if (VDBG) Log.d(TAG, "flush: " + mSocketOS); mSocketOS.flush(); } /*package*/ int read(byte[] b, int offset, int length) throws IOException { - - if (VDBG) Log.d(TAG, "read in: " + mSocketIS + " len: " + length); - int ret = mSocketIS.read(b, offset, length); - if(ret < 0) - throw new IOException("bt socket closed, read return: " + ret); - if (VDBG) Log.d(TAG, "read out: " + mSocketIS + " ret: " + ret); - return ret; + if (mSocketIS == null) throw new IOException("read is called on null InputStream"); + if (VDBG) Log.d(TAG, "read in: " + mSocketIS + " len: " + length); + int ret = mSocketIS.read(b, offset, length); + if(ret < 0) + throw new IOException("bt socket closed, read return: " + ret); + if (VDBG) Log.d(TAG, "read out: " + mSocketIS + " ret: " + ret); + return ret; } /*package*/ int write(byte[] b, int offset, int length) throws IOException { - - if (VDBG) Log.d(TAG, "write: " + mSocketOS + " length: " + length); - mSocketOS.write(b, offset, length); - // There is no good way to confirm since the entire process is asynchronous anyway - if (VDBG) Log.d(TAG, "write out: " + mSocketOS + " length: " + length); - return length; + if (mSocketOS == null) throw new IOException("write is called on null OutputStream"); + if (VDBG) Log.d(TAG, "write: " + mSocketOS + " length: " + length); + mSocketOS.write(b, offset, length); + // There is no good way to confirm since the entire process is asynchronous anyway + if (VDBG) Log.d(TAG, "write out: " + mSocketOS + " length: " + length); + return length; } @Override |