title: Don't quite understand how InputStream.read( byte[], int, int ) works
body: <p>I know this is trivial, but it doesn't make sense to me. Java can't pass pointers/references as parameters, yet the read() function is passed a <code>buffer into which the data is read</code>, and only returns an int as <code>the total number of bytes read into the buffer</code>. </p>
  
  <p>I am expecting to read five separate bytes from this device, but when I pass the function a buffer, and try to access it afterwards it continues to be <code>null</code>. If I print out the return value from the function it gives me the <code>int 5</code>, which is expected. But how can I access the data which was actually put into the buffer?</p>
  
  <p>Here is a link to the <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html" rel="nofollow">JavaDocs</a>....</p>
  
  <p>EDIT: </p>
  
  <p>This is the original call to the read function. </p>
  
  <pre><code>public boolean onOptionsItemSelected( MenuItem item ) {
      switch( item.getItemId() ) {
      case R.id.connect:
          startActivityForResult( new Intent( this, DeviceList.class ), 1 );
          return true;
      case R.id.readTest:
          Log.i(TAG,  "Before write." );
          byte[] b = {'$'};
          for( int i = 0 ; i &lt; 3 ; i++ ) {
              mService.write( b );
          }
          Log.i(TAG, "After write." );
          return true;
  
      case R.id.readData:
          byte[] c = mService.read( 5 );
  
          Toast.makeText(this, Integer.toString( mService.bytes ), Toast.LENGTH_LONG).show();
      default:
          return super.onContextItemSelected( item );
      }
  }
  </code></pre>
  
  <p>Note, this read function is a function declared in my class called BluetoothService. This class contains another class called ConnectedThread, which calls the InputStream read...</p>
  
  <p>Here is MY read function....</p>
  
  <pre><code>public byte[] read( int length ) {
      Log.i( TAG, "Inside read." );
      ConnectedThread r;
      buffer = null;
      synchronized( this ) {
          if( mState != STATE_CONNECTED ) return null;
          r = mConnectedThread;
      }
      Log.i(TAG,  "Before run." );
      r.run( length );
      Log.i( TAG, "After run." );
      Log.i( TAG, Integer.toString( bytes ) );
      return buffer;
  
  }
  </code></pre>
  
  <p>And here is the ConnectedThread class, which calls read itself....</p>
  
  <pre><code>/**
   * This thread runs during a connection with a remote device.
   * It handles all incoming and outgoing transmissions.
   */
  private class ConnectedThread extends Thread {
      private final BluetoothSocket mmSocket;
      private final InputStream mmInStream;
      private final OutputStream mmOutStream;
  
      public ConnectedThread(BluetoothSocket socket, String socketType) {
          Log.d(TAG, "create ConnectedThread: " + socketType);
          mmSocket = socket;
          InputStream tmpIn = null;
          OutputStream tmpOut = null;
  
          // Get the BluetoothSocket input and output streams
          try {
              tmpIn = socket.getInputStream();
              tmpOut = socket.getOutputStream();
          } catch (IOException e) {
              Log.e(TAG, "temp sockets not created", e);
          }
  
          mmInStream = tmpIn;
          mmOutStream = tmpOut;
      }
  
      public void run(int length) {
          Log.i(TAG, "BEGIN mConnectedThread");
          byte[] buffer = new byte[1024];
  
  
          // Keep listening to the InputStream while connected
          //while (true) {
              try {
                  // Read from the InputStream
                  bytes = mmInStream.read(buffer, 0, length);
  
                  // Send the obtained bytes to the UI Activity
                  mHandler.obtainMessage(MainMenu.MESSAGE_READ, bytes, -1, buffer)
                          .sendToTarget();
              } catch (IOException e) {
                  Log.e(TAG, "disconnected", e);
                  connectionLost();
                  // Start the service over to restart listening mode
                  BluetoothService.this.start();
                  //break;
              }
         // }
              Log.i(TAG, "MADE IT HERE" );
      }
  
      /**
       * Write to the connected OutStream.
       * @param buffer  The bytes to write
       */
      public void write(byte[] buffer) {
          try {
              mmOutStream.write(buffer);
  
              // Share the sent message back to the UI Activity
              mHandler.obtainMessage(MainMenu.MESSAGE_WRITE, -1, -1, buffer)
                      .sendToTarget();
          } catch (IOException e) {
              Log.e(TAG, "Exception during write", e);
          }
      }
  
      public void cancel() {
          try {
              mmSocket.close();
          } catch (IOException e) {
              Log.e(TAG, "close() of connect socket failed", e);
          }
      }
  }
  </code></pre>