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

every time invokes getSupportedAttributesValues() taking time

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • 8
    • 6
    • client-libs
    • 2d
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      Java(TM) SE Runtime Environment (build 1.6.0_03-b05) Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Windows XP

      A DESCRIPTION OF THE PROBLEM :
      When Java 1.4.2 invoked the method of PrintService.getSupportedAttributesValues() for the first time, it takes some time to retrieve the values from default printer.
      After that, the following method is executed, it takes no time to return from the execution.

      However, for Java6, every time the method is invoked, it takes quite some time to finish from that excution so that it dynamically degrade the perfomance of printing job.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      After the first execution of PrintService.getSupportedAttributesValues(), the following calls made is that takes no time as same as Java 1.4.2.
      ACTUAL -
      Every time getSupportedAttributesValues() invoked taking time to retrieve the values.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------


      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.awt.print.*;
      import javax.print.*;
      import javax.print.attribute.*;
      import javax.print.attribute.standard.*;

      public class Main extends JFrame implements Printable {

      /** Panel */
      private JPanel pnlMain = null;

      /** TextArea for Printing */
      private JTextArea taMain = null;

      /** row chars of TextArea */
      private final int ROW = 10;

      /** column chars of TextArea */
      private int COL = 30;

      /** Print Button */
      private JButton btPrint = null;

      /** Quit Button */
      private JButton btQuit = null;

      /** Flag whether any printable page exists or not */
      private boolean bPageExists = true;

      /** Printing text */
      private StringBuffer sbText = null;

      /** Font */
      private Font font = null;

      /** Old printing page */
      private int iOldIndex = -1;

      /**
      * Main
      * @param args
      */
      public static void main(String[] args) {
      Main mainWnd = new Main();

      mainWnd.init();
      mainWnd.disp();
      }

      /**
      * Initialize screen
      */
      private void init() {
      pnlMain = new JPanel();

      // TextArea
      font = new Font("Courier New", Font.PLAIN, 12);
      taMain = new JTextArea(ROW, COL);
      taMain.setFont(font);
      taMain.setLineWrap(true);

      int iWidth = taMain.getPreferredSize().width + font.getSize() * 2;
      int iHeight = taMain.getPreferredSize().height;

      // Print button
      btPrint = new JButton("Print");
      btPrint.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      onPrint();
      }
      });

      // Quit button
      btQuit = new JButton("Quit");
      btQuit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      onQuit();
      }
      });
      iHeight += btQuit.getPreferredSize().height;

      pnlMain.add(taMain);
      pnlMain.add(btPrint);
      pnlMain.add(btQuit);
      pnlMain.setPreferredSize(new Dimension(iWidth, iHeight));
      }

      /**
      * Display screen
      */
      private void disp() {
      Dimension frameSize = new Dimension(pnlMain.getPreferredSize().width,
      pnlMain.getPreferredSize().height + btQuit.getPreferredSize().height * 2);
      this.setSize(frameSize);
      this.add(pnlMain);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      this.setTitle("PrintTest");
      this.setVisible(true);
      }

      /**
      * Ending process
      */
      private void onQuit() {
      System.exit(0);
      }

      /**
      * Printing process
      */
      private void onPrint() {
      // Print Settings
      OrientationRequested or = OrientationRequested.PORTRAIT;
      MediaSizeName mediaSizeName = MediaSizeName.ISO_A4;

      // Search for print service
      PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
      if (null == printService) {
      this.setTitle("Print Test - Cannot find default print service");
      return;
      }
      this.setTitle("Print Test");

      // Get printing job
      DocPrintJob job = printService.createPrintJob();

      // Set print attributes
      PrintRequestAttributeSet reqAttrSet = new HashPrintRequestAttributeSet();
      reqAttrSet.add(new JobName("Test Printing", null));
      reqAttrSet.add(or);
      reqAttrSet.add(mediaSizeName);
      MediaPrintableArea mpa = Main.getMediaPrintableArea(printService, mediaSizeName);
      if (mpa != null) {
      reqAttrSet.add(mpa);
      }

      DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
      Doc doc = new SimpleDoc(this, flavor, null);

      // Set printing text
      sbText = new StringBuffer(taMain.getText());

      // Enables reprinting
      bPageExists = true;
      iOldIndex = -1;

      try {
      job.print(doc, reqAttrSet);
      } catch (PrintException e) {
      this.setTitle("Print Test - Print failure");
      return;
      }
      }

      /**
      * Get printable area
      * @param printService Print service name
      * @param mediaSizeName Media size name(e.g. ISO_A4)
      * @return
      */
      private static synchronized MediaPrintableArea getMediaPrintableArea(PrintService printService, MediaSizeName mediaSizeName) {
      PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
      attr.add(mediaSizeName);

      Object obj = printService.getSupportedAttributeValues(MediaPrintableArea.class,
      DocFlavor.SERVICE_FORMATTED.PRINTABLE, attr);
      MediaPrintableArea[] mpa0 = (MediaPrintableArea[])obj;

              return mpa0[0];
      }

      /**
      * Callback method when printing
      * @return PAGE_EXISTS or NO_SUCH_PAGE
      */
      public int print(Graphics g, PageFormat pf, int index) {
      if (bPageExists) {
      Graphics2D g2d = (Graphics2D)g;
      g2d.setFont(font);
      int x = 50;
      int y = 100;
      int dy = font.getSize() + 4;

      if (iOldIndex == index) {
      while (true) {
      String strLine = getLine();
      if (null == strLine || 0 == strLine.length()) {
      break;
      }
      g2d.drawString(strLine, x, y);
      y += dy;
      }
      bPageExists = false;
      }
      iOldIndex = index;
      return Printable.PAGE_EXISTS;
      }
      return Printable.NO_SUCH_PAGE;
      }

      /**
      * Get 1 line text for printing
      * @return 1 line text
      */
      private String getLine() {
      if (null == sbText) {
      return null;
      }
      int iLength = sbText.length();
      int iLfCrPoint = sbText.indexOf("\n");
      int iNewLinePoint = 0;
      String strLine = "";
      if (0 < iLfCrPoint) {
      iNewLinePoint = Math.min(iLfCrPoint, 30);
      strLine = sbText.substring(0, iNewLinePoint);
      sbText.delete(0, iNewLinePoint + 1);
      } else {
      iNewLinePoint = Math.min(iLength, 30);
      strLine = sbText.substring(0, iNewLinePoint);
      sbText.delete(0, iNewLinePoint);
      }

      return strLine;
      }
      }

      ---------- END SOURCE ----------

            jgodinez Jennifer Godinez (Inactive)
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: