jdk/test/javax/print/PrintSubInputStream/Example.java
author mlapshin
Wed, 23 Apr 2008 18:06:34 +0400
changeset 414 05c1395dbe48
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6691503: Malicious applet can show always-on-top popup menu which has whole screen size Summary: The fix for 6675802 is replaced by a try-catch clause that catches SequrityExceptions for applets. Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4700712 4707777
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Should submit only 1 job in Windows and print only 1 page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author jgodinez
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main/manual Example
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.print.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.print.attribute.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.print.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Example
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static void main(String [] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        if(args.length != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            System.err.println("Usage: Example num_sections");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            int stream_sections      = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            DocFlavor flavor         = DocFlavor.INPUT_STREAM.AUTOSENSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            PrintService [] services = PrintServiceLookup.lookupPrintServices
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
(flavor, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            if(services.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                PrintRequestAttributeSet attbs = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
HashPrintRequestAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                PrintService service = ServiceUI.printDialog(null, 100, 100,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
services, null, flavor, attbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                if(service != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    InputStream stream = createInputStream(stream_sections);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    Doc doc            = new SimpleDoc(stream, flavor, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    DocPrintJob job    = service.createPrintJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    job.addPrintJobListener(new PrintJobListener(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                        public void printJobCanceled(PrintJobEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                            finish("Canceled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        public void printJobCompleted(PrintJobEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                            finish("Complete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        public void printJobFailed(PrintJobEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                            finish("Failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        public void printDataTransferCompleted(PrintJobEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
pje)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                            System.out.println("data transfered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        public void printJobNoMoreEvents(PrintJobEvent pje)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                            finish("Complete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        public void printJobRequiresAttention(PrintJobEvent pje)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
{}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    System.out.println("Printing...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    job.print(doc, attbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                System.out.println("no printers found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }catch(Exception e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static void finish(String message)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        System.out.println("Printing " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static InputStream createInputStream(int num_sections)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        byte [] bytes = "Returns the number of bytes that can be read (or skipped over)\nfrom this input stream without blocking by the next caller of\na method for this input stream. The next caller might be the same thread or\nanother thread. ".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return new TestInputStream(bytes, num_sections);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static class TestInputStream extends ByteArrayInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public TestInputStream(byte [] bytes, int sections)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            super(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            int avail  = super.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            block_size = avail / sections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public int available()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int true_avail = super.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return true_avail == 0 ? 0 : block_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        private int block_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}