Esempio n. 1
0
  /**
   * Checks if this doc flavor object is equal to the given object.
   *
   * <p>Two doc flavor objects are considered equal if the provided object is not <code>null</code>
   * and an instance of <code>DocFlavor</code>. The MIME types has to be equal in their media type,
   * media subtype, their paramter/value combinations and the representation classname.
   *
   * @param obj the object to test.
   * @return <code>true</code> if equal, <code>false</code> otherwise.
   */
  public boolean equals(Object obj) {
    if (!(obj instanceof DocFlavor)) return false;

    DocFlavor tmp = (DocFlavor) obj;

    return (getMimeType().equals(tmp.getMimeType())
        && getRepresentationClassName().equals(tmp.getRepresentationClassName()));
  }
Esempio n. 2
0
  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();
  }