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

package org.bouncycastle.tsp.ers;

import java.io.FileInputStream;
import org.bouncycastle.operator.DigestCalculator;
import java.io.IOException;
import org.bouncycastle.util.io.Streams;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.File;

public class ERSInputStreamData extends ERSCachingData
{
    private final File contentFile;
    private final byte[] contentBytes;
    
    public ERSInputStreamData(final File file) throws FileNotFoundException {
        if (file.isDirectory()) {
            throw new IllegalArgumentException("directory not allowed");
        }
        if (!file.exists()) {
            throw new FileNotFoundException(file + " not found");
        }
        this.contentBytes = null;
        this.contentFile = file;
    }
    
    public ERSInputStreamData(final InputStream inputStream) {
        try {
            this.contentBytes = Streams.readAll(inputStream);
        }
        catch (final IOException ex) {
            throw ExpUtil.createIllegalState("unable to open content: " + ex.getMessage(), ex);
        }
        this.contentFile = null;
    }
    
    @Override
    protected byte[] calculateHash(final DigestCalculator digestCalculator, final byte[] array) {
        byte[] array2;
        if (this.contentBytes != null) {
            array2 = ERSUtil.calculateDigest(digestCalculator, this.contentBytes);
        }
        else {
            try {
                final FileInputStream fileInputStream = new FileInputStream(this.contentFile);
                array2 = ERSUtil.calculateDigest(digestCalculator, fileInputStream);
                fileInputStream.close();
            }
            catch (final IOException ex) {
                throw ExpUtil.createIllegalState("unable to open content: " + ex.getMessage(), ex);
            }
        }
        if (array != null) {
            return ERSUtil.concatPreviousHashes(digestCalculator, array, array2);
        }
        return array2;
    }
}
