title: Reading binary stream until "\r\n" is encountered
body: <p>I'm working on a Java application which will stream video from an IP Camera. The video streams from the IP Camera in MJPEG format. The protocol is the following...</p>
  
  <pre><code>--ipcamera (\r\n)
  Content-Type: image/jpeg (\r\n)
  Content-Length: {length of frame} (\r\n)
  (\r\n)
  {frame}
  (\r\n)
  --ipcamera (\r\n)
  etc.
  </code></pre>
  
  <p>I've tried using classes such as BufferedReader and Scanner to read until the "\r\n", however those are meant for text and not binary data, so it becomes corrupt. Is there any way to read the binary stream until it encounters a "\r\n"? Here is my current (broken) code.</p>
  
  <p>EDIT: I've gotten it to work. I updated the code below. However, it's really slow in doing so. I'm not sure if it has anything to do with the ArrayList or not, but it could be the culprit. Any pointers to speed up the code? It's currently taking 500ms to 900ms for a single frame.</p>
  
  <pre><code>public void run() {
      long startTime = System.currentTimeMillis();
      try {
          URLConnection urlConn = url.openConnection();
          urlConn.setReadTimeout(15000);
          urlConn.connect();
          urlStream = urlConn.getInputStream();
          DataInputStream dis = new DataInputStream(urlStream);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          ArrayList&lt;Byte&gt; bytes = new ArrayList&lt;Byte&gt;();
          byte cur;
          int curi;
          byte[] curBytes;
          int length = 0;
          while ((curi = dis.read()) != -1) {
              cur = (byte) curi;
              bytes.add(cur);
              curBytes = getPrimativeArray(bytes);
              String curBytesString = new String(curBytes, "UTF-8");
              if (curBytesString.equals("--ipcamera\r\n")) {
                  bytes.clear();
                  continue;
              } else if (curBytesString.equals("Content-Type: image/jpeg\r\n")) {
                  bytes.clear();
                  continue;
              } else if (curBytesString.matches("^Content-Length: ([0-9]+)\r\n$")) {
                  length = Integer.parseInt(curBytesString.replace("Content-Length: ", "").trim());
                  bytes.clear();
                  continue;
              } else if (curBytesString.equals("\r\n")) {
                  if (length == 0) {
                      continue;
                  }
                  byte[] frame = new byte[length];
                  dis.readFully(frame, 0, length);
                  writeFrame(frame);
                  bytes.clear();
                  break;
              }
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
      long curTime = System.currentTimeMillis() - startTime;
      System.out.println(curTime);
  }
  
  private byte[] getPrimativeArray(ArrayList&lt;Byte&gt; array) {
      byte[] bytes = new byte[array.size()];
      for (int i = 0; i &lt; array.size(); i++) {
          bytes[i] = array.get(i).byteValue();
      }
      return bytes;
  }
  
  private void writeFrame(byte[] bytes) throws IOException {
      File file = new File("C:\\test.jpg");
      FileOutputStream fos = new FileOutputStream(file);
      fos.write(bytes);
      fos.close();
      System.out.println("done");
  }
  </code></pre>