StoredCacheFile.java 3.9 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletOutputStream
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.io.IOUtils
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.httpcache.file;

import com.adobe.granite.httpcache.api.CacheFile;
import com.adobe.granite.httpcache.api.Headers;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class StoredCacheFile
implements CacheFile {
    private static Logger logger = LoggerFactory.getLogger(StoredCacheFile.class);
    private final String key;
    private final File file;
    private Headers headers;
    private String encoding;

    public StoredCacheFile(String key, File file) {
        this.key = key;
        this.file = file;
    }

    public String getKey() {
        return this.key;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public Headers getHeaders() {
        if (this.headers == null) {
            File headersFile = new File(this.file.getPath() + ".headers");
            FileInputStream in = null;
            try {
                in = new FileInputStream(headersFile);
                this.headers = new Headers();
                this.headers.load(in);
            }
            catch (IOException e) {
                logger.error("Unable to load headers.", (Throwable)e);
            }
            finally {
                IOUtils.closeQuietly((InputStream)in);
            }
        }
        return this.headers;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void spool(HttpServletResponse response) throws IOException {
        InputStream in = this.openInputStream();
        if (this.encoding != null) {
            InputStreamReader reader = new InputStreamReader(in, this.encoding);
            try {
                IOUtils.copy((Reader)new InputStreamReader(in, this.encoding), (Writer)response.getWriter());
            }
            finally {
                IOUtils.closeQuietly((Reader)reader);
            }
        }
        try {
            IOUtils.copy((InputStream)in, (OutputStream)response.getOutputStream());
        }
        finally {
            IOUtils.closeQuietly((InputStream)in);
        }
    }

    private InputStream openInputStream() throws IOException {
        FileInputStream in = new FileInputStream(this.file);
        try {
            int len = in.read();
            if (len != 0 && len < 255) {
                int n;
                byte[] b = new byte[len];
                for (int off = 0; off < len; off += n) {
                    n = in.read(b, off, len - off);
                    if (n >= 0) continue;
                    throw new EOFException("Not enough bytes for encoding (" + len + " expected)");
                }
                this.encoding = new String(b, 0, b.length, "8859_1");
            }
        }
        catch (IOException e) {
            IOUtils.closeQuietly((InputStream)in);
            throw e;
        }
        return in;
    }

    public ServletOutputStream getOutputStream(ServletOutputStream base) throws IOException {
        throw new IllegalStateException("Read only");
    }

    public PrintWriter getWriter(PrintWriter base, String encoding) throws IOException {
        throw new IllegalStateException("Read only");
    }

    public boolean save() {
        throw new IllegalStateException("Read only");
    }

    public void discard() {
        throw new IllegalStateException("Read only");
    }
}