PsToPdfUtils.java 9.24 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.pdfg.logging.PDFGLogger
 */
package com.adobe.pdfg.impl.utils;

import com.adobe.pdfg.logging.PDFGLogger;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

public class PsToPdfUtils {
    private static PDFGLogger pdfgLogger = PDFGLogger.getPDFGLogger(PsToPdfUtils.class);
    private static final byte[] PREVIEW_IMAGE_HEADER = new byte[]{-59, -48, -45, -58};
    private static final byte[] END_OF_PREVIEW_IMAGE = new byte[]{37, 33, 80, 83};
    private static final byte[] END_OF_FILE = new byte[]{37, 37, 69, 79, 70};
    private static byte[] WATERMARK_POSTSCRIPT;
    private static final String WATERMARK_RESOURCE_NAME = "/watermark.ps";

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Converted monitor instructions to comments
     * Lifted jumps to return sites
     */
    private static void injectWatermark(OutputStream out) throws IOException {
        if (PsToPdfUtils.WATERMARK_POSTSCRIPT == null) {
            var1_1 = PsToPdfUtils.class;
            // MONITORENTER : com.adobe.pdfg.impl.utils.PsToPdfUtils.class
            if (PsToPdfUtils.WATERMARK_POSTSCRIPT == null) {
                outBytes = new ByteArrayOutputStream();
                in = null;
                try {
                    in = PsToPdfUtils.class.getResourceAsStream("/watermark.ps");
                    buffer = new byte[4096];
                    nBytesRead = in.read(buffer);
                    while (nBytesRead != -1) {
                        outBytes.write(buffer, 0, nBytesRead);
                        nBytesRead = in.read(buffer);
                    }
                    PsToPdfUtils.WATERMARK_POSTSCRIPT = outBytes.toByteArray();
                    var7_6 = null;
                    if (in != null) {
                        try {
                            in.close();
                        }
                        catch (Exception e) {
                            PsToPdfUtils.pdfgLogger.trace("PsToPdfUtils.injectWatermark exception: " + e.getMessage(), null, (Throwable)e);
                        }
                    }
                }
                catch (Throwable var6_10) {
                    var7_7 = null;
                    if (in == null) throw var6_10;
                    ** try [egrp 2[TRYBLOCK] [2 : 105->112)] { 
lbl30: // 1 sources:
                    in.close();
                    throw var6_10;
lbl32: // 1 sources:
                    catch (Exception e) {
                        PsToPdfUtils.pdfgLogger.trace("PsToPdfUtils.injectWatermark exception: " + e.getMessage(), null, (Throwable)e);
                    }
                    throw var6_10;
                }
            }
            // MONITOREXIT : var1_1
        }
        out.write(PsToPdfUtils.WATERMARK_POSTSCRIPT);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static String preProcessEPS(String dataFileName, File modifiedFile) throws Exception {
        String string;
        Exception e2;
        OutputStream out;
        block13 : {
            BufferedInputStream in = null;
            out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(dataFileName));
                out = new BufferedOutputStream(new FileOutputStream(modifiedFile));
                byte[] header = new byte[4];
                int headerLength = in.read(header);
                if (headerLength == 4 && Arrays.equals(header, PREVIEW_IMAGE_HEADER)) {
                    PsToPdfUtils.copyBytesBetween(in, out, END_OF_PREVIEW_IMAGE, END_OF_FILE);
                } else {
                    out.write(header, 0, headerLength);
                    PsToPdfUtils.copyStreams(in, out);
                }
                string = modifiedFile.getCanonicalPath();
                Object var8_7 = null;
                if (in == null) break block13;
            }
            catch (Throwable var7_11) {
                Exception e2;
                Object var8_8 = null;
                if (in != null) {
                    try {
                        in.close();
                    }
                    catch (Exception e2) {
                        pdfgLogger.trace("PsToPdfUtils.preProcessEPS exception: " + e2.getMessage(), null, (Throwable)e2);
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    }
                    catch (Exception e2) {
                        pdfgLogger.trace("PsToPdfUtils.preProcessEPS exception: " + e2.getMessage(), null, (Throwable)e2);
                    }
                }
                throw var7_11;
            }
            try {
                in.close();
            }
            catch (Exception e2) {
                pdfgLogger.trace("PsToPdfUtils.preProcessEPS exception: " + e2.getMessage(), null, (Throwable)e2);
            }
        }
        if (out != null) {
            try {
                out.close();
            }
            catch (Exception e2) {
                pdfgLogger.trace("PsToPdfUtils.preProcessEPS exception: " + e2.getMessage(), null, (Throwable)e2);
            }
        }
        return string;
    }

    private static void copyBytesBetween(InputStream in, OutputStream out, byte[] begin, byte[] end) throws IOException {
        boolean STAGE_SKIPPING = false;
        boolean STAGE_COPYING = true;
        int STAGE_DONE = 2;
        int stage = 0;
        int posBegin = 0;
        int posEnd = 0;
        ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
        while (stage != 2) {
            int byteRead = in.read();
            if (byteRead == -1) {
                stage = 2;
                break;
            }
            if (stage == 0) {
                if (byteRead == begin[posBegin]) {
                    outBytes.write(byteRead);
                    if (++posBegin != begin.length) continue;
                    out.write(outBytes.toByteArray());
                    outBytes.reset();
                    stage = 1;
                    continue;
                }
                posBegin = 0;
                outBytes.reset();
                continue;
            }
            if (stage != 1) continue;
            out.write(byteRead);
            if (byteRead != end[posEnd]) {
                posEnd = 0;
            }
            if (byteRead != end[posEnd] || ++posEnd != end.length) continue;
            stage = 2;
            break;
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static String preProcessPS(String dataFileName, File modifiedFile) throws Exception {
        String string;
        Exception e2;
        OutputStream out;
        block11 : {
            BufferedInputStream in = null;
            out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(dataFileName));
                out = new BufferedOutputStream(new FileOutputStream(modifiedFile));
                PsToPdfUtils.injectWatermark(out);
                PsToPdfUtils.copyStreams(in, out);
                string = modifiedFile.getCanonicalPath();
                Object var6_5 = null;
                if (in == null) break block11;
            }
            catch (Throwable var5_9) {
                Exception e2;
                Object var6_6 = null;
                if (in != null) {
                    try {
                        in.close();
                    }
                    catch (Exception e2) {
                        pdfgLogger.trace("PsToPdfUtils.preProcessPS exception: " + e2.getMessage(), null, (Throwable)e2);
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    }
                    catch (Exception e2) {
                        pdfgLogger.trace("PsToPdfUtils.preProcessPS exception: " + e2.getMessage(), null, (Throwable)e2);
                    }
                }
                throw var5_9;
            }
            try {
                in.close();
            }
            catch (Exception e2) {
                pdfgLogger.trace("PsToPdfUtils.preProcessPS exception: " + e2.getMessage(), null, (Throwable)e2);
            }
        }
        if (out != null) {
            try {
                out.close();
            }
            catch (Exception e2) {
                pdfgLogger.trace("PsToPdfUtils.preProcessPS exception: " + e2.getMessage(), null, (Throwable)e2);
            }
        }
        return string;
    }

    private static void copyStreams(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[32678];
        int nBytesRead = in.read(buffer);
        while (nBytesRead != -1) {
            out.write(buffer, 0, nBytesRead);
            nBytesRead = in.read(buffer);
        }
    }
}