private void getAttributeValues(DocFlavor flavor) throws PrintException { if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) { fidelity = true; } else { fidelity = false; } Class category; Attribute[] attrs = reqAttrSet.toArray(); for (int i = 0; i < attrs.length; i++) { Attribute attr = attrs[i]; category = attr.getCategory(); if (fidelity == true) { if (!service.isAttributeCategorySupported(category)) { notifyEvent(PrintJobEvent.JOB_FAILED); throw new PrintJobAttributeException("unsupported category: " + category, category, null); } else if (!service.isAttributeValueSupported(attr, flavor, null)) { notifyEvent(PrintJobEvent.JOB_FAILED); throw new PrintJobAttributeException("unsupported attribute: " + attr, null, attr); } } if (category == Destination.class) { URI uri = ((Destination) attr).getURI(); if (!"file".equals(uri.getScheme())) { notifyEvent(PrintJobEvent.JOB_FAILED); throw new PrintException("Not a file: URI"); } else { try { mDestination = (new File(uri)).getPath(); } catch (Exception e) { throw new PrintException(e); } // check write access SecurityManager security = System.getSecurityManager(); if (security != null) { try { security.checkWrite(mDestination); } catch (SecurityException se) { notifyEvent(PrintJobEvent.JOB_FAILED); throw new PrintException(se); } } } } else if (category == JobName.class) { jobName = ((JobName) attr).getValue(); } else if (category == Copies.class) { copies = ((Copies) attr).getValue(); } else if (category == Media.class) { if (attr instanceof MediaSizeName) { mediaName = (MediaSizeName) attr; // If requested MediaSizeName is not supported, // get the corresponding media size - this will // be used to create a new PageFormat. if (!service.isAttributeValueSupported(attr, null, null)) { mediaSize = MediaSize.getMediaSizeForName(mediaName); } } } else if (category == OrientationRequested.class) { orient = (OrientationRequested) attr; } } }
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 (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(); }