Поскольку вы не знаете, где в потоке находятся эти 2 байта, вам придется просмотреть весь ввод.Это означает, что ваша производительность будет по крайней мере линейной.Два найти два байта линейно просто:
static long search(InputStream inputStream) throws IOException {
BufferedInputStream is = new BufferedInputStream(inputStream);
int previous = is.read(read);
long pos = 0;
int current;
while((current = is.read()) != -1) {
pos++;
if(previous == 0xff && current == 0xd8) {
return pos;
}
last = current;
}
throw new RuntimeException("There ain't no pic in here.");
}