Path: blob/master/test/jdk/javax/print/PrintSubInputStream/Example.java
41149 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4700712 470777726* @summary Should submit only 1 job in Windows and print only 1 page.27* @author jgodinez28* @run main/manual Example29*/30import java.awt.*;31import java.awt.event.*;32import javax.swing.*;33import java.io.*;34import javax.print.*;35import javax.print.attribute.*;36import javax.print.event.*;3738public class Example39{40public static void main(String [] args)41{42if(args.length != 1)43{44System.err.println("Usage: Example num_sections");45return;46}4748try{49int stream_sections = Integer.parseInt(args[0]);50DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;51PrintService [] services = PrintServiceLookup.lookupPrintServices52(flavor, null);5354if(services.length > 0)55{56PrintRequestAttributeSet attbs = new57HashPrintRequestAttributeSet();58PrintService service = ServiceUI.printDialog(null, 100, 100,59services, null, flavor, attbs);6061if(service != null)62{63InputStream stream = createInputStream(stream_sections);64Doc doc = new SimpleDoc(stream, flavor, null);65DocPrintJob job = service.createPrintJob();66job.addPrintJobListener(new PrintJobListener(){67public void printJobCanceled(PrintJobEvent e)68{69finish("Canceled");70}7172public void printJobCompleted(PrintJobEvent e)73{74finish("Complete");75}7677public void printJobFailed(PrintJobEvent e)78{79finish("Failed");80}8182public void printDataTransferCompleted(PrintJobEvent83pje)84{85System.out.println("data transfered");86}8788public void printJobNoMoreEvents(PrintJobEvent pje)89{90finish("Complete");91}92public void printJobRequiresAttention(PrintJobEvent pje)93{}9495});96System.out.println("Printing...");97job.print(doc, attbs);98}99100}else101{102System.out.println("no printers found");103}104105}catch(Exception e)106{107e.printStackTrace();108}109}110111private static void finish(String message)112{113System.out.println("Printing " + message);114System.out.flush();115}116117private static InputStream createInputStream(int num_sections)118{119byte [] 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();120121return new TestInputStream(bytes, num_sections);122}123124private static class TestInputStream extends ByteArrayInputStream125{126public TestInputStream(byte [] bytes, int sections)127{128super(bytes);129int avail = super.available();130block_size = avail / sections;131}132133public int available()134{135int true_avail = super.available();136return true_avail == 0 ? 0 : block_size;137}138139private int block_size;140}141}142143144