+ private byte[] readStreamingJpgData() throws IOException {
+ byte[] data;
+ byte[] batch = new byte[1024];
+ boolean lastByteIsXX = false;
+ loop:
+ while (true) {
+ int batchCount = fileStream.read(batch);
+ if (batchCount == -1) {
+ return null;
+ }
+ if (lastByteIsXX) {
+ if (batch[0] == (byte) 0xd8) {
+ data = bufferedBytes.toByteArray();
+ bufferedBytes.reset();
+ bufferedBytes.write(0xff);
+ bufferedBytes.write(batch, 0, batchCount);
+ break;
+ }
+ bufferedBytes.write(0xff);
+ }
+ for (int i = 0; i < batchCount - 1; i++) {
+ if (batch[i] == (byte) 0xff && batch[i + 1] == (byte) 0xd8) { // start of jpeg
+ if (i > 0) {
+ bufferedBytes.write(batch, 0, i);
+ }
+ data = bufferedBytes.toByteArray();
+ bufferedBytes.reset();
+ bufferedBytes.write(batch, i, batchCount - i);
+ break loop;
+ }
+ }
+ lastByteIsXX = batch[batchCount - 1] == (byte) 0xff;
+ bufferedBytes.write(batch, 0, batchCount - (lastByteIsXX ? 1 : 0));