title: How to receive only one packet from tcp socket quickly java
body: <p>The remote Tcp Server device sends out 8 bytes of data at a time and does so at relatively fast pace(but not horribly fast). Anyways, i want to receive the 8 bytes and enact on the 8 bytes given. However, i appear to be receiving bytearrays with multiples of these 8 bytes back to back to where my count(in code) is several multiples of 8 bytes, varying in length from 16 to 120+ bytes.</p>
  
  <p>Checking Wireshark, the remote Tcp Server appears to be sending out the 8 bytes correctly and not combining them. This shows me that the reading of the socket is too slow and thus combining the 8 bytes intervals back to back for me, which i don't want.? An example being:</p>
  
  <p>8bytes-8bytes-16bytes-8bytes-8bytes-56bytes.</p>
  
  <p>here is a timestamping of how soon data streams in. with as little as 10 ms.</p>
  
  <pre><code>(13:44:23.210) 00h 10h 01h 06h FFh 3Ah FFh FFh
  (13:44:23.290) 00h 10h 01h 06h FFh 3Fh FFh FFh
  (13:44:23.300) 00h 10h 01h 06h FFh 44h FFh FFh
  </code></pre>
  
  <p>How do i efficiently read from the port to be as fast as possible while ensuring no multiples of the 8 byte data? </p>
  
  <p>The code below is a segment that reads data of unknown size and posts it back to the NetworkManager for further processing. I have tried using different streams in hopes of getting the desired effect, but neither stream ensures that.</p>
  
  <pre><code>    private class Receive extends Thread {
  
          private InputStream inputStream;
          private DataInputStream dataInputStream;
          private BufferedInputStream bufferedInputStream;
          private ByteArrayOutputStream byteArrayOutputStream;
  
          public void run() {
              if (DEBUG) Log.d(TAG + " \"" + name + "\"", String.valueOf(getId()) + ":receive run.");
              bundle = new Bundle();
              buffer = new byte[1024];
              try {
                  bufferedInputStream = new BufferedInputStream(socket.getInputStream());
              } catch (IOException ioException) {
                  Log.d(TAG + " \"" + name + "\"", String.valueOf(getId()) + ":IOException", ioException);
              }
              while (connected &amp;&amp; !closed) {
                  if (DEBUG) Log.d(TAG + " \"" + name + "\"", String.valueOf(getId()) + ":retry read.");
                  try {
  //                    socket.setSoTimeout(timeout);
                      int count = bufferedInputStream.read(buffer);
                      byteArrayOutputStream = new ByteArrayOutputStream(count);
                      if (count == -1) {
                          networkManager.stopTcpNetwork(name);
                          closed = true;
                          Log.d(TAG + " \"" + name + "\"", String.valueOf(getId()) + "Connection Lost?!?!");
                      } else {
                          if (DEBUG) Log.d(TAG + " \"" + name + "\"", String.valueOf(getId()) + ":call back." + " Count: " + String.valueOf(count));
                          byteArrayOutputStream.write(buffer, 0, count);
                          bundle.putByteArray(name, byteArrayOutputStream.toByteArray());
  //                        bundle.putByteArray(name, buffer);
                          message = networkManager.obtainMessage();
                          message.what = Messages.MESSAGES_TCP_DATA.getInt();
                          message.setData(bundle);
                          networkManager.sendMessage(message);
                      }
                  } catch (SocketTimeoutException socketTimeoutException) {
                      if (DEBUG) Log.d(TAG + " \"" + name + "\"", String.valueOf(getId()) + ":no data yet.");
                  } catch (Exception exception) {
                      Log.d(TAG + " \"" + name + "\"", String.valueOf(getId()) + ":Exception", exception);
                  }
              }
          }
      }
  </code></pre>