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

Huge print files generated by jdk1.2 rc1

XMLWordPrintable

    • 2d
    • x86
    • windows_nt



      Name: tb29552 Date: 10/29/98


      /*
       This program produces clipped output under jdk1.1.7, and is
       the subject of bug 4179886. The program simply formats
       and prints a file specified on the command line.

       Compiling and running the same program under jdk1.2 rc1
       generates such a huge print file that I have never been
       able to wait around long enough to see whether anything
       comes out. For example, using a 15 line text file as follows:

       012345678901234567890line1
       012345678901234567890line2
       012345678901234567890line3
       012345678901234567890line4
       012345678901234567890line5
       012345678901234567890line6
       012345678901234567890line7
       012345678901234567890line8
       012345678901234567890line9
       012345678901234567890line10
       012345678901234567890line11
       012345678901234567890line12
       012345678901234567890line13
       012345678901234567890line14
       012345678901234567890line15

       produces an output file of 446 KB!!

       System: Windows NT 4 service pack 3
       Printers: HP LaserJet 4Si MX

       What is going on??
       */
       /*
        * This example simply formats the contents of a file to a printer (N.B. does
        * not expand tabs..)
        *
        * Usage: java PrintBug nameOfFile e.g. java PrintBug c:\temp\myjunk.text
        *
        * When the program is compiled and run under jdk1.1.6, the text is printed
        * only into the region of the page that is effectively printed by the
        * printer. Under jdk1.1.7, everything seems to be translated up and to the
        * left, with the effect that the part of text is clipped.
        *
        * System: Windows NT 4 service pack 3 Printers: HP LaserJet 4Si MX
        *
        * Program PrintBug.java follows:
        *
        */
      import java.awt.*;
      import java.io.*;

      public class PrintBug {
          private PrintJob _pJob;
          private Frame _aFrame;

          public static void main(String[] argv) {
              if (argv.length < 1) {
                  System.out.println("Usage: java PrintBug someFile ");
                  System.out.println();
                  System.out.println(" e.g. java PrintBug c:\\temp\\myjunk.txt ");
                  System.exit(1);
              }
              FileReader fr;
              try {
                  fr = new FileReader(argv[0]);
                  LineNumberReader lnr = new LineNumberReader(fr);
                  (new PrintBug ()).printIt(lnr);
                  System.exit(0);
              } catch(FileNotFoundException e) {
                  System.out.println("No such file: " + argv[0]);
              }
          }

          public PrintBug () {
              Frame _aFrame = new Frame();

              /*
               * Reference Bug IDs 4136947 and 4093775: Even though the PrinterBug
               * class has a Frame, it is never displayed (ie. setVisible(true) is
               * never called) so the "default" font for the frame is never
               * initialized since that doesn't happen until addNotify which isn't
               * done until the peer is created and no peer is ever created. Btw,
               * this happens in both jdk 1.1.x and jdk 1.2.
               */
              _aFrame.pack();
              _aFrame.setVisible(true);

              _pJob = _aFrame.getToolkit().getPrintJob(_aFrame, "Printed output ",
                                                       null);
              if (_pJob == null) {
                  System.out.println("No print job..");
                  System.exit(0);
              }
          }

          protected void printIt(LineNumberReader lnr) {
              Graphics pg = _pJob.getGraphics();

              int leadingChars = 0;
              int leadingLines = 0;
              int trailingLines = 0;

              int pageNum = 1;
              int linesForThisPage = 0;
              int linesForThisJob = 0;

              String nextLine;

              int pageHeight = _pJob.getPageDimension().height;
              int pageWidth = _pJob.getPageDimension().width;
              // System.out.println("_pJob.getPageDimension().height returns
              // + pageHeight);
              // System.out.println("_pJob.getPageDimension().width returns "+
              // pageWidth);
              int fontSize = 12;
              Font fixed = new Font("Monospaced", Font.PLAIN, fontSize);
              FontMetrics fm = pg.getFontMetrics(fixed);
              // System.out.println("fm.charWidth(' ') returns " +fm.charWidth('
              // '));
              while (fm.charWidth(' ') * (132 + leadingChars) > pageWidth ||
                     fm.getHeight() * (66 + leadingLines +
                                       trailingLines) > pageHeight) {
                  fontSize--;
                  fixed = new Font("Monospaced", Font.PLAIN,
                                   fontSize);
                  fm = pg.getFontMetrics(fixed);
                  // System.out.println("fm.charWidth(' ') returns "
                  // +fm.charWidth(' '));
              }

              // Have to set the font to get any output!
              pg.setFont(fixed);
              fm = pg.getFontMetrics(fixed);
              int fontHeight = fm.getHeight();
              int fontDescent = fm.getDescent();
              int curHeight = leadingLines * fontHeight;
              int inPixels = leadingChars * fm.charWidth(' ');
              try {
                  do {
                      nextLine = lnr.readLine();
                      if (nextLine != null) {
                          if (curHeight + fontHeight *
                              (trailingLines + 1) > pageHeight) {
                              // New Page
                              // System.out.println ("" + linesForThisPage + "
                              // lines printed for page " + pageNum);
                              pageNum++;
                              linesForThisPage = 0;
                              pg.dispose();
                              pg = _pJob.getGraphics();
                              if (pg != null) {
                                  pg.setFont(fixed);
                              }
                              curHeight = leadingLines *
                                  fontHeight;
                          }
                          curHeight += fontHeight;
                          if (pg != null) {
                              pg.drawString(nextLine,
                                            inPixels, curHeight - fontDescent);
                              linesForThisPage++;
                              linesForThisJob++;
                          } else {
                              System.out.println("pg null ");
                          }
                      }
                  } while (nextLine != null);
              } catch(EOFException eof) {
                  // Fine, ignore
              } catch(Throwable t) { // Anything else
                  t.printStackTrace();
              }
              // Send last page to the printer;
              pg.dispose();
              System.out.println("" + linesForThisPage + " lines printed for page " +
                                 pageNum);
              System.out.println("pages printed: " + pageNum);
              System.out.println("total lines printed: " +
                                 linesForThisJob);
              // Clean-up
              _pJob.end();
          }
      }

      (Review ID: 41519)
      ======================================================================

            mpeircesunw Michael Peirce (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: