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

PrinterJob.print() generates IllegalArgumentException,NegativeArraySizeException

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 1.4.0
    • 1.2.0
    • client-libs
    • 2d
    • beta
    • generic
    • generic

    Description

      Name: mc57594 Date: 06/29/99

      Calling PrinterJob.print() with a small or negative paper width
      or height throws a NegativeArraySizeException or
      IllegalArgumentException. These conditions should probably be
      tested for or the exceptions caught within the method, and a
      PrinterException thrown, hopefully with a meaningful message.

      If the width and height are small, I guess the exception
      happens when they are rounded to zero at the initial 72dpi. But
      if the Graphics is scaled later, these small sizes might be
      perfectly acceptable, right?. For example, I could print on
      .00001 by .00001 inch paper at ten million dpi - so no positive
      paper dimensions should cause an exception.


      Here are the stack dumps (java full version "JDK-1.2-V") on Solaris:

      java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0
              at java.awt.image.SampleModel.<init>(SampleModel.java:93)
              at java.awt.image.SinglePixelPackedSampleModel.<init>(SinglePixelPackedSampleModel.java:107)
              at java.awt.image.Raster.createPackedRaster(Raster.java:598)
              at java.awt.image.Raster.createPackedRaster(Raster.java:412)
              at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:637)
              at java.awt.image.BufferedImage.<init>(BufferedImage.java:245)
              at sun.java2d.RasterPrinterJob.printPage(RasterPrinterJob.java:467)
              at sun.java2d.RasterPrinterJob.print(RasterPrinterJob.java:235)


      java.lang.NegativeArraySizeException:
              at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:48)
              at java.awt.image.Raster.createPackedRaster(Raster.java:404)
              at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:637)
              at java.awt.image.BufferedImage.<init>(BufferedImage.java:245)
              at sun.java2d.RasterPrinterJob.printPage(RasterPrinterJob.java:467)
              at sun.java2d.RasterPrinterJob.print(RasterPrinterJob.java:235)
      (Review ID: 84674)
      ======================================================================
          OK.

              java PrintTest will generate a divide by zero exception
              java PrintTest 1 will generate an IllegalArgumentException
              java PrintTest 2 will generate a NegativeArraySizeException

          all exceptions root from job.print().

          I guess negative values could be caught in Paper.setSize() and
      Paper.setImageableArea().


         import java.awt.*;
         import java.awt.print.*;

         public class PrintTest
         {
            public static void main(String args[])
            {
               PrinterJob job = PrinterJob.getPrinterJob();
               PageFormat format = job.defaultPage();
               Paper paper = format.getPaper();
            
               double w = 1, h = .0001; // Generates ArithmeticException: / by zero.
               if(args.length > 0 && args[0].equals("1")) {
                  w = .0001; h = 1; } // Generates IllegalArgumentException.
               else if(args.length > 0 && args[0].equals("2")) {
                  w = -1; h = 1; } // Generates NegativeArraySizeException.
               paper.setSize(w, h);
               paper.setImageableArea(0, 0, w, h);
               format.setPaper(paper);
               job.setPrintable(
                     new Printable() {
                        public int print(Graphics g, PageFormat page_format, int page) {
                           return NO_SUCH_PAGE;
                        }
                     }, format);
            
               try {
                  job.print(); }
                  catch(PrinterException e) {
                     System.err.println("Print error:\n" + e.getMessage());
                  }
            }
         }

      Attachments

        Issue Links

          Activity

            People

              jgodinez Jennifer Godinez (Inactive)
              mchamnessunw Mark Chamness (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: