// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.est;

import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class CTEChunkedInputStream extends InputStream
{
    private InputStream src;
    int chunkLen;
    
    public CTEChunkedInputStream(final InputStream src) {
        this.chunkLen = 0;
        this.src = src;
    }
    
    private String readEOL() throws IOException {
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int i;
        do {
            i = this.src.read();
            if (i == -1) {
                if (byteArrayOutputStream.size() == 0) {
                    return null;
                }
                return byteArrayOutputStream.toString().trim();
            }
            else {
                byteArrayOutputStream.write(i & 0xFF);
            }
        } while (i != 10);
        return byteArrayOutputStream.toString().trim();
    }
    
    @Override
    public int read() throws IOException {
        if (this.chunkLen == Integer.MIN_VALUE) {
            return -1;
        }
        if (this.chunkLen == 0) {
            String eol;
            do {
                eol = this.readEOL();
            } while (eol != null && eol.length() == 0);
            if (eol == null) {
                return -1;
            }
            this.chunkLen = Integer.parseInt(eol.trim(), 16);
            if (this.chunkLen == 0) {
                this.readEOL();
                this.chunkLen = Integer.MIN_VALUE;
                return -1;
            }
        }
        final int read = this.src.read();
        --this.chunkLen;
        return read;
    }
}
