*/
@Validate
public static byte[] readUntil(@NotNull final InputStream stream, @NotNull final byte[] terminator)
throws IOException
{
val result = new ByteArrayOutputStream(128);
// buffer holding the latest bytes
val tempBuffer = new byte[terminator.length];
// read the first batch
for (int i = 0; i < tempBuffer.length; i++)
{
int read = stream.read();
// check status
if (read >= 0)
tempBuffer[i] = (byte) read;
else
throw new IOException(String.format(EOF_EXCEPTION_MESSAGE_BYTES, tempBuffer.length - i));
}
result.write(tempBuffer, 0, tempBuffer.length);
// compare and read next until arrays hold equal values
while (!(ByteArrayUtils.sequenceEqual(tempBuffer, terminator)))
{
// read next byte
val read = stream.read();
// check status
if (read >= 0)
{
val bt = (byte) read;
// shift elements in array to the left, throw away the first read
for (int i = 0; i < tempBuffer.length - 1; i++)
tempBuffer[i] = tempBuffer[i + 1];