Search in sources :

Example 6 with DocFlavor

use of javax.print.DocFlavor in project adempiere by adempiere.

the class POSClientSide method printOtherOS.

/**
	 * Print Other S.O
	 * @param fis
	 * @return void
	 */
private void printOtherOS(FileInputStream fis) {
    DocFlavor docFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc document = new SimpleDoc(fis, docFormat, null);
    PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
    PrintService[] listsPrintService = PrintServiceLookup.lookupPrintServices(docFormat, attributeSet);
    try {
        DocPrintJob printJob = null;
        for (int x = 0; x < listsPrintService.length; x++) {
            if (listsPrintService[x].getName().equals(m_Print)) {
                printJob = listsPrintService[x].createPrintJob();
            }
        }
        printJob.print(document, attributeSet);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SimpleDoc(javax.print.SimpleDoc) SimpleDoc(javax.print.SimpleDoc) Doc(javax.print.Doc) DocPrintJob(javax.print.DocPrintJob) DocFlavor(javax.print.DocFlavor) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) IOException(java.io.IOException) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintService(javax.print.PrintService)

Example 7 with DocFlavor

use of javax.print.DocFlavor in project jdk8u_jdk by JetBrains.

the class PSStreamPrintJob method print.

public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException {
    synchronized (this) {
        if (printing) {
            throw new PrintException("already printing");
        } else {
            printing = true;
        }
    }
    this.doc = doc;
    /* check if the parameters are valid before doing much processing */
    DocFlavor flavor = doc.getDocFlavor();
    Object data;
    try {
        data = doc.getPrintData();
    } catch (IOException e) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("can't get print data: " + e.toString());
    }
    if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintJobFlavorException("invalid flavor", flavor);
    }
    initializeAttributeSets(doc, attributes);
    getAttributeValues(flavor);
    String repClassName = flavor.getRepresentationClassName();
    if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
        try {
            instream = doc.getStreamForBytes();
            printableJob(new ImagePrinter(instream), reqAttrSet);
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.URL.JPEG) || flavor.equals(DocFlavor.URL.PNG)) {
        try {
            printableJob(new ImagePrinter((URL) data), reqAttrSet);
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        }
    } else if (repClassName.equals("java.awt.print.Pageable")) {
        try {
            pageableJob((Pageable) doc.getPrintData(), reqAttrSet);
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (repClassName.equals("java.awt.print.Printable")) {
        try {
            printableJob((Printable) doc.getPrintData(), reqAttrSet);
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("unrecognized class: " + repClassName);
    }
}
Also used : PrintException(javax.print.PrintException) IOException(java.io.IOException) DocFlavor(javax.print.DocFlavor)

Example 8 with DocFlavor

use of javax.print.DocFlavor in project jdk8u_jdk by JetBrains.

the class UnixPrintJob method print.

public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException {
    synchronized (this) {
        if (printing) {
            throw new PrintException("already printing");
        } else {
            printing = true;
        }
    }
    if ((PrinterIsAcceptingJobs) (service.getAttribute(PrinterIsAcceptingJobs.class)) == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
        throw new PrintException("Printer is not accepting job.");
    }
    this.doc = doc;
    /* check if the parameters are valid before doing much processing */
    DocFlavor flavor = doc.getDocFlavor();
    Object data;
    try {
        data = doc.getPrintData();
    } catch (IOException e) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("can't get print data: " + e.toString());
    }
    if (data == null) {
        throw new PrintException("Null print data.");
    }
    if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintJobFlavorException("invalid flavor", flavor);
    }
    initializeAttributeSets(doc, attributes);
    getAttributeValues(flavor);
    // set up mOptions
    if ((service instanceof IPPPrintService) && CUPSPrinter.isCupsRunning()) {
        IPPPrintService.debug_println(debugPrefix + "instanceof IPPPrintService");
        if (mediaName != null) {
            CustomMediaSizeName customMedia = ((IPPPrintService) service).findCustomMedia(mediaName);
            if (customMedia != null) {
                mOptions = " media=" + customMedia.getChoiceName();
            }
        }
        if (customTray != null && customTray instanceof CustomMediaTray) {
            String choice = customTray.getChoiceName();
            if (choice != null) {
                mOptions += " media=" + choice;
            }
        }
        if (nUp != null) {
            mOptions += " number-up=" + nUp.getValue();
        }
        if (orient != OrientationRequested.PORTRAIT && (flavor != null) && !flavor.equals(DocFlavor.SERVICE_FORMATTED.PAGEABLE)) {
            mOptions += " orientation-requested=" + orient.getValue();
        }
        if (sides != null) {
            mOptions += " sides=" + sides;
        }
    }
    IPPPrintService.debug_println(debugPrefix + "mOptions " + mOptions);
    String repClassName = flavor.getRepresentationClassName();
    String val = flavor.getParameter("charset");
    String encoding = "us-ascii";
    if (val != null && !val.equals("")) {
        encoding = val;
    }
    if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
        try {
            instream = doc.getStreamForBytes();
            if (instream == null) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("No stream for data");
            }
            if (!(service instanceof IPPPrintService && ((IPPPrintService) service).isIPPSupportedImages(flavor.getMimeType()))) {
                printableJob(new ImagePrinter(instream));
                ((UnixPrintService) service).wakeNotifier();
                return;
            }
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.URL.JPEG) || flavor.equals(DocFlavor.URL.PNG)) {
        try {
            URL url = (URL) data;
            if ((service instanceof IPPPrintService) && ((IPPPrintService) service).isIPPSupportedImages(flavor.getMimeType())) {
                instream = url.openStream();
            } else {
                printableJob(new ImagePrinter(url));
                ((UnixPrintService) service).wakeNotifier();
                return;
            }
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e.toString());
        }
    } else if (flavor.equals(DocFlavor.CHAR_ARRAY.TEXT_PLAIN) || flavor.equals(DocFlavor.READER.TEXT_PLAIN) || flavor.equals(DocFlavor.STRING.TEXT_PLAIN)) {
        try {
            reader = doc.getReaderForText();
            if (reader == null) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("No reader for data");
            }
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe.toString());
        }
    } else if (repClassName.equals("[B") || repClassName.equals("java.io.InputStream")) {
        try {
            instream = doc.getStreamForBytes();
            if (instream == null) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("No stream for data");
            }
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe.toString());
        }
    } else if (repClassName.equals("java.net.URL")) {
        /*
             * This extracts the data from the URL and passes it the content
             * directly to the print service as a file.
             * This is appropriate for the current implementation where lp or
             * lpr is always used to spool the data. We expect to revise the
             * implementation to provide more complete IPP support (ie not just
             * CUPS) and at that time the job will be spooled via IPP
             * and the URL
             * itself should be sent to the IPP print service not the content.
             */
        URL url = (URL) data;
        try {
            instream = url.openStream();
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e.toString());
        }
    } else if (repClassName.equals("java.awt.print.Pageable")) {
        try {
            pageableJob((Pageable) doc.getPrintData());
            if (service instanceof IPPPrintService) {
                ((IPPPrintService) service).wakeNotifier();
            } else {
                ((UnixPrintService) service).wakeNotifier();
            }
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (repClassName.equals("java.awt.print.Printable")) {
        try {
            printableJob((Printable) doc.getPrintData());
            if (service instanceof IPPPrintService) {
                ((IPPPrintService) service).wakeNotifier();
            } else {
                ((UnixPrintService) service).wakeNotifier();
            }
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("unrecognized class: " + repClassName);
    }
    // now spool the print data.
    PrinterOpener po = new PrinterOpener();
    java.security.AccessController.doPrivileged(po);
    if (po.pex != null) {
        throw po.pex;
    }
    OutputStream output = po.result;
    /* There are three cases:
         * 1) Text data from a Reader, just pass through.
         * 2) Text data from an input stream which we must read using the
         *    correct encoding
         * 3) Raw byte data from an InputStream we don't interpret as text,
         *    just pass through: eg postscript.
         */
    BufferedWriter bw = null;
    if ((instream == null && reader != null)) {
        BufferedReader br = new BufferedReader(reader);
        OutputStreamWriter osw = new OutputStreamWriter(output);
        bw = new BufferedWriter(osw);
        char[] buffer = new char[1024];
        int cread;
        try {
            while ((cread = br.read(buffer, 0, buffer.length)) >= 0) {
                bw.write(buffer, 0, cread);
            }
            br.close();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e);
        }
    } else if (instream != null && flavor.getMediaType().equalsIgnoreCase("text")) {
        try {
            InputStreamReader isr = new InputStreamReader(instream, encoding);
            BufferedReader br = new BufferedReader(isr);
            OutputStreamWriter osw = new OutputStreamWriter(output);
            bw = new BufferedWriter(osw);
            char[] buffer = new char[1024];
            int cread;
            while ((cread = br.read(buffer, 0, buffer.length)) >= 0) {
                bw.write(buffer, 0, cread);
            }
            bw.flush();
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e);
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
            }
        }
    } else if (instream != null) {
        BufferedInputStream bin = new BufferedInputStream(instream);
        BufferedOutputStream bout = new BufferedOutputStream(output);
        byte[] buffer = new byte[1024];
        int bread = 0;
        try {
            while ((bread = bin.read(buffer)) >= 0) {
                bout.write(buffer, 0, bread);
            }
            bin.close();
            bout.flush();
            bout.close();
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e);
        }
    }
    notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
    if (mDestType == UnixPrintJob.DESTPRINTER) {
        PrinterSpooler spooler = new PrinterSpooler();
        java.security.AccessController.doPrivileged(spooler);
        if (spooler.pex != null) {
            throw spooler.pex;
        }
    }
    notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
    if (service instanceof IPPPrintService) {
        ((IPPPrintService) service).wakeNotifier();
    } else {
        ((UnixPrintService) service).wakeNotifier();
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) PrintException(javax.print.PrintException) BufferedInputStream(java.io.BufferedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) java.awt.print(java.awt.print) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) DocFlavor(javax.print.DocFlavor) PrinterIsAcceptingJobs(javax.print.attribute.standard.PrinterIsAcceptingJobs)

Example 9 with DocFlavor

use of javax.print.DocFlavor in project camel by apache.

the class PrinterConfiguration method assignDocFlavor.

private DocFlavor assignDocFlavor(String flavor, String mimeType) throws Exception {
    // defaults
    if (mimeType == null) {
        mimeType = "AUTOSENSE";
    }
    if (flavor == null) {
        flavor = "DocFlavor.BYTE_ARRAY";
    }
    DocFlavor d = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    DocFlavorAssigner docFlavorAssigner = new DocFlavorAssigner();
    if (mimeType.equalsIgnoreCase("AUTOSENSE")) {
        d = docFlavorAssigner.forMimeTypeAUTOSENSE(flavor);
    } else if (mimeType.equalsIgnoreCase("GIF")) {
        d = docFlavorAssigner.forMimeTypeGIF(flavor);
    } else if (mimeType.equalsIgnoreCase("JPEG")) {
        d = docFlavorAssigner.forMimeTypeJPEG(flavor);
    } else if (mimeType.equalsIgnoreCase("PDF")) {
        d = docFlavorAssigner.forMimeTypePDF(flavor);
    } else if (mimeType.equalsIgnoreCase("PCL")) {
        d = docFlavorAssigner.forMimeTypePCL(flavor);
    } else if (mimeType.equalsIgnoreCase("POSTSCRIPT")) {
        d = docFlavorAssigner.forMimeTypePOSTSCRIPT(flavor);
    } else if (mimeType.equalsIgnoreCase("TEXT_HTML_HOST")) {
        d = docFlavorAssigner.forMimeTypeHOST(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_HTML_US_ASCII")) {
        d = docFlavorAssigner.forMimeTypeUSASCII(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_HTML_UTF_16")) {
        d = docFlavorAssigner.forMimeTypeUTF16(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_HTML_UTF_16LE")) {
        d = docFlavorAssigner.forMimeTypeUTF16LE(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_HTML_UTF_16BE")) {
        d = docFlavorAssigner.forMimeTypeUTF16BE(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_HTML_UTF_8")) {
        d = docFlavorAssigner.forMimeTypeUTF8(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_PLAIN_HOST")) {
        d = docFlavorAssigner.forMimeTypeHOST(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_PLAIN_US_ASCII")) {
        d = docFlavorAssigner.forMimeTypeUSASCII(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_PLAIN_UTF_16")) {
        d = docFlavorAssigner.forMimeTypeUTF16(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_PLAIN_UTF_16LE")) {
        d = docFlavorAssigner.forMimeTypeUTF16LE(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_PLAIN_UTF_16BE")) {
        d = docFlavorAssigner.forMimeTypeUTF16BE(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_PLAIN_UTF_8")) {
        d = docFlavorAssigner.forMimeTypeUTF8(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_HTML")) {
        d = docFlavorAssigner.forMimeTypeBasic(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("TEXT_PLAIN")) {
        d = docFlavorAssigner.forMimeTypeBasic(flavor, mimeType);
    } else if (mimeType.equalsIgnoreCase("PAGEABLE")) {
        d = docFlavorAssigner.forMimeTypePAGEABLE(flavor);
    } else if (mimeType.equalsIgnoreCase("PRINTABLE")) {
        d = docFlavorAssigner.forMimeTypePRINTABLE(flavor);
    } else if (mimeType.equalsIgnoreCase("RENDERABLE_IMAGE")) {
        d = docFlavorAssigner.forMimeTypeRENDERABLEIMAGE(flavor);
    }
    return d;
}
Also used : DocFlavor(javax.print.DocFlavor)

Example 10 with DocFlavor

use of javax.print.DocFlavor in project jdk8u_jdk by JetBrains.

the class Win32PrintJob method print.

public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException {
    synchronized (this) {
        if (printing) {
            throw new PrintException("already printing");
        } else {
            printing = true;
        }
    }
    PrinterState prnState = (PrinterState) service.getAttribute(PrinterState.class);
    if (prnState == PrinterState.STOPPED) {
        PrinterStateReasons prnStateReasons = (PrinterStateReasons) service.getAttribute(PrinterStateReasons.class);
        if ((prnStateReasons != null) && (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))) {
            throw new PrintException("PrintService is no longer available.");
        }
    }
    if ((PrinterIsAcceptingJobs) (service.getAttribute(PrinterIsAcceptingJobs.class)) == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS) {
        throw new PrintException("Printer is not accepting job.");
    }
    this.doc = doc;
    /* check if the parameters are valid before doing much processing */
    DocFlavor flavor = doc.getDocFlavor();
    Object data;
    try {
        data = doc.getPrintData();
    } catch (IOException e) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("can't get print data: " + e.toString());
    }
    if (data == null) {
        throw new PrintException("Null print data.");
    }
    if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintJobFlavorException("invalid flavor", flavor);
    }
    initializeAttributeSets(doc, attributes);
    getAttributeValues(flavor);
    String repClassName = flavor.getRepresentationClassName();
    if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
        try {
            instream = doc.getStreamForBytes();
            if (instream == null) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("No stream for data");
            }
            printableJob(new ImagePrinter(instream));
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.URL.JPEG) || flavor.equals(DocFlavor.URL.PNG)) {
        try {
            printableJob(new ImagePrinter((URL) data));
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        }
    } else if (repClassName.equals("java.awt.print.Pageable")) {
        try {
            pageableJob((Pageable) doc.getPrintData());
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (repClassName.equals("java.awt.print.Printable")) {
        try {
            printableJob((Printable) doc.getPrintData());
            service.wakeNotifier();
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (repClassName.equals("[B") || repClassName.equals("java.io.InputStream") || repClassName.equals("java.net.URL")) {
        if (repClassName.equals("java.net.URL")) {
            URL url = (URL) data;
            try {
                instream = url.openStream();
            } catch (IOException e) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(e.toString());
            }
        } else {
            try {
                instream = doc.getStreamForBytes();
            } catch (IOException ioe) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(ioe.toString());
            }
        }
        if (instream == null) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("No stream for data");
        }
        if (mDestination != null) {
            // if destination attribute is set
            try {
                FileOutputStream fos = new FileOutputStream(mDestination);
                byte[] buffer = new byte[1024];
                int cread;
                while ((cread = instream.read(buffer, 0, buffer.length)) >= 0) {
                    fos.write(buffer, 0, cread);
                }
                fos.flush();
                fos.close();
            } catch (FileNotFoundException fnfe) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(fnfe.toString());
            } catch (IOException ioe) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(ioe.toString());
            }
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
            notifyEvent(PrintJobEvent.JOB_COMPLETE);
            service.wakeNotifier();
            return;
        }
        if (!startPrintRawData(service.getName(), jobName)) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("Print job failed to start.");
        }
        BufferedInputStream bin = new BufferedInputStream(instream);
        int bread = 0;
        try {
            byte[] buffer = new byte[PRINTBUFFERLEN];
            while ((bread = bin.read(buffer, 0, PRINTBUFFERLEN)) >= 0) {
                if (!printRawData(buffer, bread)) {
                    bin.close();
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException("Problem while spooling data");
                }
            }
            bin.close();
            if (!endPrintRawData()) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("Print job failed to close properly.");
            }
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
        } catch (IOException e) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(e.toString());
        } finally {
            notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
        }
    } else {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("unrecognized class: " + repClassName);
    }
    service.wakeNotifier();
}
Also used : PrinterState(javax.print.attribute.standard.PrinterState) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URL(java.net.URL) java.awt.print(java.awt.print) PrintException(javax.print.PrintException) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) PrinterStateReasons(javax.print.attribute.standard.PrinterStateReasons) DocFlavor(javax.print.DocFlavor) PrinterIsAcceptingJobs(javax.print.attribute.standard.PrinterIsAcceptingJobs)

Aggregations

DocFlavor (javax.print.DocFlavor)11 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)6 FileOutputStream (java.io.FileOutputStream)5 IOException (java.io.IOException)5 PrintService (javax.print.PrintService)4 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)4 DocPrintJob (javax.print.DocPrintJob)3 PrintException (javax.print.PrintException)3 SimpleDoc (javax.print.SimpleDoc)3 StreamPrintServiceFactory (javax.print.StreamPrintServiceFactory)3 java.awt.print (java.awt.print)2 BufferedInputStream (java.io.BufferedInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 URL (java.net.URL)2 StreamPrintService (javax.print.StreamPrintService)2 PrinterIsAcceptingJobs (javax.print.attribute.standard.PrinterIsAcceptingJobs)2 BufferedImage (java.awt.image.BufferedImage)1 PageFormat (java.awt.print.PageFormat)1 Printable (java.awt.print.Printable)1 PrinterException (java.awt.print.PrinterException)1