title: ArrayIndexOutOfBoundsException when read from buffer
body: <p>My app send data (string or bytes) to another app copy via Bluetooth.
  I try to send big text or bytes[] and get this error:</p>
  
  <pre><code>FATAL EXCEPTION: Thread-8297
          java.lang.ArrayIndexOutOfBoundsException: length=1024; index=1024
              at com.ramimartin.multibluetooth.bluetooth.BluetoothRunnable.run(BluetoothRunnable.java:147)
              at java.lang.Thread.run(Thread.java:841)
  </code></pre>
  
  <p>So, <strong>why?</strong>
  Code from line </p>
  
  <blockquote>
    <p>BluetoothRunnable.java:147</p>
  </blockquote>
  
  <p>is:</p>
  
  <pre><code>while(bytesRead == bufferSize &amp;&amp; buffer[bufferSize] != 0) {
  </code></pre>
  
  <p>Full code:</p>
  
  <pre><code>        int bufferSize = 1024;
          int bytesRead;
          byte[] buffer = new byte[bufferSize];
                      try {
                          if(this.mInputStream != null) {
                              bytesRead = this.mInputStream.read(buffer);
                              ByteBuffer bbuf = ByteBuffer.allocate(bytesRead);
                              if(bytesRead != -1) {
                                  while(bytesRead == bufferSize &amp;&amp; buffer[bufferSize] != 0) {
                                      bbuf.put(buffer, 0, bytesRead);
                                      if(this.mInputStream == null) {
                                          return;
                                      }
  
                                      bytesRead = this.mInputStream.read(buffer);
                                  }
  
                                  bbuf.put(buffer, 0, bytesRead);
                              }
  
                              EventBus.getDefault().post(new BluetoothCommunicatorBytes(bbuf.array()));
                              continue;
                          }
                      } catch (IOException var8) {
                          Log.e(TAG, "===&gt; Error Received Bytes IOException  : " + var8.getMessage());
                          continue;
                      }
  </code></pre>