/* Listing3503.java */

import java.io.*;

import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;

public class Listing3503
{
  final static DocFlavor FLAVOR = DocFlavor.INPUT_STREAM.AUTOSENSE;
  
  public static void main(String[] args) throws IOException, PrintException
  {
    System.out.println("Gedruckt wird: " + args[0]);

    File file = new File(args[0]);
    if (!file.exists()) {
      throw new IllegalArgumentException(args[0] + " existiert nicht"); 
    }
    InputStream stream = new FileInputStream(file);
    Doc document = new SimpleDoc(stream, FLAVOR, null);
    
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    System.out.println("Standarddrucker ist: " + service.getName());
    
    if (!service.isDocFlavorSupported(FLAVOR)) {
      throw new IllegalStateException(
        service.getName() + 
        " unterstützt " + FLAVOR + " nicht"
      );
    }
    System.out.println(service.getName() + " unterstützt " + FLAVOR);
    DocPrintJob job = service.createPrintJob();
    
    PrintRequestAttributeSet params = new HashPrintRequestAttributeSet();
    params.add(new Copies(1)); 
    
    job.print(document, params);
    stream.close();
  }
}