
package com.fsck.k9.mail.filter;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

/**
 * A filtering InputStream that allows single byte "peeks" without consuming the byte. The
 * client of this stream can call peek() to see the next available byte in the stream
 * and a subsequent read will still return the peeked byte.
 */
public class PeekableInputStream extends FilterInputStream {
    private boolean mPeeked;
    private int mPeekedByte;

    public PeekableInputStream(InputStream in) {
        super(in);
    }

    @Override
    public int read() throws IOException {
        if (!mPeeked) {
            return in.read();
        } else {
            mPeeked = false;
            return mPeekedByte;
        }
    }

    public int peek() throws IOException {
        if (!mPeeked) {
            mPeekedByte = in.read();
            mPeeked = true;
        }
        return mPeekedByte;
    }

    @Override
    public int read(byte[] b, int offset, int length) throws IOException {
        if (!mPeeked) {
            return in.read(b, offset, length);
        } else {
            b[offset] = (byte)mPeekedByte;
            mPeeked = false;
            int r = in.read(b, offset + 1, length - 1);
            if (r == -1) {
                return 1;
            } else {
                return r + 1;
            }
        }
    }

    @Override
    public int read(byte[] b) throws IOException {
        return read(b, 0, b.length);
    }

    @Override
    public String toString() {
        return String.format(Locale.US, "PeekableInputStream(in=%s, peeked=%b, peekedByte=%d)",
                             in.toString(), mPeeked, mPeekedByte);
    }
}

@@ -1,9 +1,8 @@
-%0A
 package 
@@ -27,16 +27,17 @@
 ilter;%0A%0A
+%0A
 import j
@@ -144,16 +144,17 @@
 ocale;%0A%0A
+%0A
 /**%0A * A
@@ -471,18 +471,17 @@
 boolean 
-mP
+p
 eeked;%0A 
@@ -495,18 +495,17 @@
 ate int 
-mP
+p
 eekedByt
@@ -508,16 +508,17 @@
 dByte;%0A%0A
+%0A
     publ
@@ -646,34 +646,33 @@
  %7B%0A        if (!
-mP
+p
 eeked) %7B%0A       
@@ -715,34 +715,33 @@
 e %7B%0A            
-mP
+p
 eeked = false;%0A 
@@ -750,34 +750,33 @@
          return 
-mP
+p
 eekedByte;%0A     
@@ -835,34 +835,33 @@
  %7B%0A        if (!
-mP
+p
 eeked) %7B%0A       
@@ -861,26 +861,25 @@
             
-mP
+p
 eekedByte = 
@@ -893,34 +893,33 @@
 ();%0A            
-mP
+p
 eeked = true;%0A  
@@ -941,18 +941,17 @@
  return 
-mP
+p
 eekedByt
@@ -1002,16 +1002,21 @@
 byte%5B%5D b
+uffer
 , int of
@@ -1067,18 +1067,17 @@
    if (!
-mP
+p
 eeked) %7B
@@ -1097,32 +1097,37 @@
 return in.read(b
+uffer
 , offset, length
@@ -1159,16 +1159,21 @@
        b
+uffer
 %5Boffset%5D
@@ -1181,18 +1181,18 @@
 = (byte)
-mP
+ p
 eekedByt
@@ -1206,18 +1206,17 @@
         
-mP
+p
 eeked = 
@@ -1251,16 +1251,21 @@
 n.read(b
+uffer
 , offset
@@ -1460,16 +1460,21 @@
 byte%5B%5D b
+uffer
 ) throws
@@ -1513,14 +1513,24 @@
 ad(b
+uffer
 , 0, b
+uffer
 .len
@@ -1699,29 +1699,16 @@
         
-             
 in.toStr
@@ -1718,19 +1718,17 @@
 (), 
-mP
+p
 eeked, 
-mP
+p
 eeke

Apply current code style