JDK-4809530 : Setting printing attribute MediaSize.ISO.A4 fails with ClassCastException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2003-01-28
  • Updated: 2003-01-29
  • Resolved: 2003-01-29
Description

Name: jk109818			Date: 01/28/2003


FULL PRODUCT VERSION :

java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

FULL OPERATING SYSTEM VERSION :
The problem is independent of operating system.
I have tested under:
Linux gun 2.2.16-22 #1 Tue Aug 22 16:49:06 EDT 2000 i686
unknown
glibc-2.1.92-14
Red Hat Linux release 7.0 (Guinness)

A DESCRIPTION OF THE PROBLEM :
Firstly, the example given in the API documentation of
javax.print package fails to compile, on line:
   aset.add(MediaSize.A4);
MediaSize.A4 does not exist.  It should be MediaSize.ISO.A4

With this change, the example program compiles but fails to
execute, giving a ClassCastException on the line
   aset.add(MediaSize.ISO.A4);
This is caused by an inappropriate declaration in
javax/print/attribute/standard/MediaSize.java, where
MediaSize should be declared to implement
PrintRequestAttribute rather than Attribute.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.  javac PrintEx3.java -deprecation
2.  java PrintEx3

EXPECTED VERSUS ACTUAL BEHAVIOR :
The program (below) should list the available printers and
send the small PostScript job to the first printer.  Instead
it fails with the ClassCastException on the line setting the
MediaSize.ISO.A4 attribute.

If this line is commented out, the program executes
correctly.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.ClassCastException
        at
javax.print.attribute.AttributeSetUtilities.verifyAttributeValue(AttributeSetUtilities.java:534)
        at javax.print.attribute.HashAttributeSet.add(HashAttributeSet.java:281)
        at PrintEx3.<init>(PrintEx3.java:39)
        at PrintEx3.main(PrintEx3.java:29)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.Sides;
import javax.print.Doc;
import javax.print.SimpleDoc;
import javax.print.DocPrintJob;
import javax.print.PrintServiceLookup;
import javax.print.PrintException;
import java.io.ByteArrayInputStream;
//import java.io.StringBufferInputStream;

public class PrintEx3
{
   String ps = "%!\n"+
      "30 30 moveto 565 30 lineto 565 812 lineto 30 812 lineto closepath\n"+
      "565 30 moveto 30 812 lineto .5 setgray stroke 0 setgray\n"+
      "/Times-Bold findfont 20 scalefont setfont\n"+
      "60 782 moveto (Big bold heading at top of page) show\n"+
      "/Times-Roman findfont 15 scalefont setfont\n"+
      "60 700 moveto (We can print any PostScript file from Java now !)show\n"+
      "showpage\n";
      
   public static void main(String s[])
   {
      PrintEx3 pe = new PrintEx3();
   }

   public PrintEx3()
   {
         DocFlavor psInFormat = DocFlavor.INPUT_STREAM.POSTSCRIPT;
         Doc myDoc = new SimpleDoc(new ByteArrayInputStream(ps.getBytes()),
psInFormat, null);
         //Doc myDoc = new SimpleDoc(new StringBufferInputStream(ps),
psInFormat, null);
         PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
         aset.add(new Copies(1));
         aset.add(MediaSize.ISO.A4);
         PrintService[] services =
            PrintServiceLookup.lookupPrintServices(psInFormat, aset);
         for (int i=0; i<services.length; ++i)
            System.out.println(services[i].getName());
         if (services.length > 0)
         {
            DocPrintJob job = services[0].createPrintJob();
            try { job.print(myDoc, aset); }
            catch (PrintException ex) { ex.printStackTrace(); }
         }
   }
}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
MediaSize cannot be used in its present form.
(Review ID: 167212) 
======================================================================

Comments
EVALUATION This is not a bug. None of the MediaSize's implement PrintRequestAttribute The class description explains that " MediaSize is not yet used to specify media. Its current role is as a mapping for named media (see MediaSizeName). ..." You instead specify MediaSizeName.ISO_A4 ###@###.### 2003-01-28 ============================
28-01-2003