LPDPrintJob.java 5.08 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.fd.stp.internal.print.jobs;

import com.adobe.fd.stp.api.PrinterProtocol;
import com.adobe.fd.stp.internal.exception.STPServiceException;
import com.adobe.fd.stp.internal.logging.STPServiceLogger;
import com.adobe.fd.stp.internal.print.jobs.TCPPrintJob;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Random;

public class LPDPrintJob
extends TCPPrintJob {
    private static STPServiceLogger logger = new STPServiceLogger(LPDPrintJob.class);
    private static final int mnDefaultPortNo = 515;
    private String msUserName = "dummy";
    private final int ASCII_SP = 32;
    private final int ASCII_LF = 10;
    private static final String LPD = PrinterProtocol.LPD.toString();

    public LPDPrintJob(String sPrintServerUri, String sPrinterName) throws STPServiceException {
        super(sPrintServerUri, sPrinterName, 515);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public boolean isAccessible() {
        boolean bSuccess;
        Socket printer = null;
        bSuccess = true;
        DataInputStream in = null;
        DataOutputStream out = null;
        String sPrinterName = this.getPrinterName();
        try {
            printer = this.connect(LPD);
            in = new DataInputStream(printer.getInputStream());
            out = new DataOutputStream(printer.getOutputStream());
            out.write(2);
            out.writeBytes(sPrinterName);
            out.write(10);
            out.flush();
            if (in.readByte() != 0) {
                bSuccess = false;
            }
        }
        catch (Exception e) {
            logger.error("AEM_STP_001_004", new String[]{sPrinterName + ":" + e.getMessage()});
            bSuccess = false;
        }
        finally {
            this.closeAll(printer, in, out);
        }
        return bSuccess;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void print(InputStream inStream) throws STPServiceException {
        boolean bSuccess;
        String sPrinterName;
        Socket printer = null;
        bSuccess = true;
        DataInputStream in = null;
        DataOutputStream out = null;
        String document = "dummy doc";
        Random jobIdGen = new Random();
        String jobid = String.valueOf(jobIdGen.nextInt(999) + 1);
        String sLocalHostName = "dummy";
        try {
            sLocalHostName = InetAddress.getLocalHost().getHostName();
        }
        catch (Exception var10_10) {
            // empty catch block
        }
        sPrinterName = this.getPrinterName();
        if (sPrinterName == null) {
            throw new STPServiceException("AEM_STP_001_001", new String[]{"printerName"});
        }
        try {
            printer = this.connect(LPD);
            in = new DataInputStream(printer.getInputStream());
            out = new DataOutputStream(printer.getOutputStream());
            String dname = "dfA" + jobid + sLocalHostName;
            String cname = "cfA" + jobid + sLocalHostName;
            String controlString = "H" + sLocalHostName + "\n" + "P" + this.msUserName + "\n" + "f" + dname + "\n" + "U" + dname + "\n" + "N" + document + "\n";
            out.write(2);
            out.writeBytes(sPrinterName);
            out.write(10);
            out.flush();
            if (in.readByte() != 0) {
                bSuccess = false;
            }
            out.write(2);
            out.writeBytes(String.valueOf(controlString.length()));
            out.write(32);
            out.writeBytes(cname);
            out.write(10);
            out.flush();
            byte by = in.readByte();
            if (by != 0) {
                bSuccess = false;
            }
            out.writeBytes(controlString);
            out.write(0);
            out.flush();
            by = in.readByte();
            if (by != 0) {
                bSuccess = false;
            }
            out.write(3);
            out.writeBytes(String.valueOf(inStream.available()));
            out.write(32);
            out.writeBytes(dname);
            out.write(10);
            out.flush();
            by = in.readByte();
            if (by != 0) {
                bSuccess = false;
            }
            int bytes = 0;
            byte[] buffer = new byte[8192];
            while ((bytes = inStream.read(buffer)) > 0) {
                out.write(buffer, 0, bytes);
            }
            out.write(0);
            out.flush();
            by = in.readByte();
            if (by != 0) {
                bSuccess = false;
            }
        }
        catch (Exception e) {
            logger.error("AEM_STP_001_005", new String[]{LPD, sPrinterName + ":" + e.getMessage()});
            bSuccess = false;
        }
        finally {
            this.closeAll(printer, in, out);
        }
        if (!bSuccess) {
            throw new STPServiceException("AEM_STP_001_005", new String[]{LPD, sPrinterName});
        }
    }
}