Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8349350

Unable to print using InputSlot and OutputBin print attributes at the same time

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 25
    • 11, 21, 23, 24, 25
    • client-libs
    • None
    • 2d
    • b15

      the printExecCmd method of the PSPrinterJob throws an ArrayIndexOutOfBoundsException when a printer job contains more than one print option (InputSlot and OutputBin at the same time).

      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
      at java.desktop/sun.print.PSPrinterJob.printExecCmd(PSPrinterJob.java:1687)
      at java.desktop/sun.print.PSPrinterJob$PrinterSpooler.run(PSPrinterJob.java:748)
      at java.base/java.security.AccessController.doPrivileged(AccessController.java:319)
      at java.desktop/sun.print.PSPrinterJob.endDoc(PSPrinterJob.java:820)
      at java.desktop/sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1683)

      The reason is that the execCmd array uses one index per print flag, but 'OPTIONS' flag can use more.

      To reproduce the bug you need a printer with available InputSlot and OutputBin options.

      import javax.print.PrintService;
      import javax.print.PrintServiceLookup;
      import javax.print.attribute.HashPrintRequestAttributeSet;
      import javax.print.attribute.PrintRequestAttributeSet;
      import javax.print.attribute.standard.Media;
      import javax.print.attribute.standard.MediaTray;
      import javax.print.attribute.standard.OutputBin;
      import java.awt.Graphics;
      import java.awt.print.PageFormat;
      import java.awt.print.Printable;
      import java.awt.print.PrinterException;
      import java.awt.print.PrinterJob;

      public class PrintArrIdxOut {

          public static void main(String[] args) throws PrinterException {
              PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
              if (printServices.length == 0) {
                  System.out.println("Print services not found");
                  return;
              }

              PrintService testPrintService = null;
              OutputBin outputBin = null;
              MediaTray mediaTray = null;
              for (PrintService ps : printServices) {
                  Media[] medias = (Media[]) ps.
                          getSupportedAttributeValues(Media.class, null, null);
                  if (medias != null) {
                      for (Media m : medias) {
                          if (m instanceof MediaTray) {
                              mediaTray = (MediaTray) m;
                              break;
                          }
                      }
                  }
                  if (mediaTray == null) {
                      continue;
                  }
                  OutputBin[] outputBins = (OutputBin[]) ps.
                          getSupportedAttributeValues(OutputBin.class, null, null);
                  if (outputBins != null && outputBins.length > 0) {
                      outputBin = outputBins[0];
                      testPrintService = ps;
                      break;
                  }
              }
              if (testPrintService == null) {
                  System.out.println("There is no print service with available InputTray and OutputBin attributes");
                  return;
              }

              PrinterJob printerJob = PrinterJob.getPrinterJob();
              printerJob.setPrintService(testPrintService);

              PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
              attributeSet.add(outputBin);
              attributeSet.add(mediaTray);

              printerJob.setPrintable(new Printable() {
                  @Override
                  public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
                      return NO_SUCH_PAGE;
                  }
              });
              printerJob.print(attributeSet);
          }
      }

      Tentative fix is attached, a PR will be submitted soon.

            Unassigned Unassigned
            avoitylov Aleksei Voitylov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: