2
* Braille Utils (C) 2010-2011 Daisy Consortium
4
* This library is free software; you can redistribute it and/or modify it under
5
* the terms of the GNU Lesser General Public License as published by the Free
6
* Software Foundation; either version 2.1 of the License, or (at your option)
9
* This library is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this library; if not, write to the Free Software Foundation, Inc.,
16
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
package org.daisy.printing;
21
import java.io.FileInputStream;
22
import java.io.FileNotFoundException;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.io.Reader;
27
import javax.print.Doc;
28
import javax.print.DocFlavor;
29
import javax.print.DocPrintJob;
30
import javax.print.PrintException;
31
import javax.print.PrintService;
32
import javax.print.PrintServiceLookup;
33
import javax.print.attribute.DocAttributeSet;
36
* Printer device class of type DocFlavor.INPUT_STREAM.AUTOSENSE
38
* This class can be used when sending a file to a printer.
40
* @author Joel Håkansson
43
public class PrinterDevice implements Device {
44
//private final static DocFlavor FLAVOR = DocFlavor.BYTE_ARRAY.AUTOSENSE;
45
private final static DocFlavor FLAVOR = DocFlavor.INPUT_STREAM.AUTOSENSE;
46
private PrintService service;
49
* Create a device with the provided name.
50
* @param deviceName the name of the device
51
* @param fuzzyLookup If true, the returned device is any device whose name contains the
52
* supplied deviceName. If false, the returned device name equals the supplied deviceName.
53
* @throws IllegalArgumentException if no device is found.
55
public PrinterDevice(String deviceName, boolean fuzzyLookup) {
56
PrintService[] printers = PrintServiceLookup.lookupPrintServices(FLAVOR, null);
57
for (PrintService p : printers) {
58
if (p.getName().equals(deviceName)) {
64
PrintService match = null;
65
double currentMatch = 0;
66
for (PrintService p : printers) {
67
if (p.getName().contains(deviceName)) {
68
double thisMatch = deviceName.length() / (double)p.getName().length();
69
if (thisMatch > currentMatch) {
70
currentMatch = thisMatch;
80
throw new IllegalArgumentException("Could not find embosser.");
84
* List available devices
85
* @return returns a list of available devices that accepts DocFlavor.INPUT_STREAM.AUTOSENSE
87
public static PrintService[] getDevices() {
88
PrintService[] printers = PrintServiceLookup.lookupPrintServices(FLAVOR, null);
93
* Transmit a file to the device
94
* @param file the file to transmit
95
* @throws FileNotFoundException
96
* @throws PrintException
98
public void transmit(File file) throws PrintException {
100
transmit(new FileInputStream(file));
101
} catch (FileNotFoundException e) {
102
throw new PrintException(e);
106
private void transmit(InputStream is) throws PrintException {
107
InputStreamDoc doc = new InputStreamDoc(is);
108
DocPrintJob dpj = service.createPrintJob();
109
dpj.print(doc, null);
112
private class InputStreamDoc implements Doc {
113
private InputStream stream;
116
* Default constructor
118
* @throws FileNotFoundException
120
public InputStreamDoc(InputStream stream) {
121
this.stream = stream;
124
public DocAttributeSet getAttributes() {
128
public DocFlavor getDocFlavor() {
132
public Object getPrintData() throws IOException {
133
return getStreamForBytes();
136
public Reader getReaderForText() throws IOException {
140
public InputStream getStreamForBytes() throws IOException {