title: How to split a byte array that contains multiple "lines" in Java?
body: <p>Say we have a file like so:</p>
  
  <pre><code>one 
  two 
  three
  </code></pre>
  
  <p>(but this file got encrypted)</p>
  
  <p>My crypto method returns the whole file in memory, as a byte[] type.<BR>
  I know byte arrays don't have a concept of "lines", that's something a Scanner (for example) could have.</p>
  
  <p>I would like to traverse each line, convert it to string and perform my operation on it but I don't know
  how to:</p>
  
  <ol>
  <li>Find lines in a byte array</li>
  <li>Slice the original byte array to "lines" (I would convert those slices to String, to send to my other methods)</li>
  <li>Correctly traverse a byte array, where each iteration is a new "line"</li>
  </ol>
  
  <p>Also: do I need to consider the different OS the file might have been composed in? I know that there is some difference between new lines in Windows and Linux and I don't want my method to work only with one format.</p>
  
  <p>Edit: Following some tips from answers here, I was able to write some code that gets the job done. I still wonder if this code is worthy of keeping or I am doing something that can fail in the future:</p>
  
  <pre><code>byte[] decryptedBytes = doMyCrypto(fileName, accessKey);
  ByteArrayInputStream byteArrInStrm = new ByteArrayInputStream(decryptedBytes);
  InputStreamReader inStrmReader = new InputStreamReader(byteArrInStrm);
  BufferedReader buffReader = new BufferedReader(inStrmReader);
  
  String delimRegex = ",";
  String line;
  String[] values = null;
  
  while ((line = buffReader.readLine()) != null) {
      values = line.split(delimRegex);
      if (Objects.equals(values[0], tableKey)) {
          return values;
      }
  }
  System.out.println(String.format("No entry with key %s in %s", tableKey, fileName));
  return values;
  </code></pre>
  
  <p>In particular, I was advised to explicitly set the encoding but I was unable to see exactly where?</p>