Path: blob/master/src/java.desktop/share/classes/javax/print/Doc.java
41153 views
/*1* Copyright (c) 2000, 2017, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.print;2627import java.io.IOException;28import java.io.InputStream;29import java.io.Reader;3031import javax.print.attribute.DocAttributeSet;3233/**34* Interface {@code Doc} specifies the interface for an object that supplies one35* piece of print data for a Print Job. "Doc" is a short, easy-to-pronounce term36* that means "a piece of print data." The client passes to the Print Job an37* object that implements interface {@code Doc}, and the Print Job calls methods38* on that object to obtain the print data. The {@code Doc} interface lets a39* Print Job:40* <ul>41* <li>Determine the format, or "doc flavor" (class42* {@link DocFlavor DocFlavor}), in which the print data is available. A doc43* flavor designates the print data format (a MIME type) and the44* representation class of the object from which the print data comes.45* <li>Obtain the print data representation object, which is an instance of46* the doc flavor's representation class. The Print Job can then obtain the47* actual print data from the representation object.48* <li>Obtain the printing attributes that specify additional characteristics49* of the doc or that specify processing instructions to be applied to the50* doc. Printing attributes are defined in package51* {@link javax.print.attribute}. The doc returns its printing attributes52* stored in an {@link DocAttributeSet javax.print.attribute.DocAttributeSet}.53* </ul>54* Each method in an implementation of interface {@code Doc} is permitted always55* to return the same object each time the method is called. This has56* implications for a Print Job or other caller of a doc object whose print data57* representation object "consumes" the print data as the caller obtains the58* print data, such as a print data representation object which is a stream.59* Once the Print Job has called {@link #getPrintData() getPrintData()} and60* obtained the stream, any further calls to {@link #getPrintData()61* getPrintData()} will return the same stream object upon which reading may62* already be in progress, <i>not</i> a new stream object that will re-read the63* print data from the beginning. Specifying a doc object to behave this way64* simplifies the implementation of doc objects, and is justified on the grounds65* that a particular doc is intended to convey print data only to one Print Job,66* not to several different Print Jobs. (To convey the same print data to67* several different Print Jobs, you have to create several different doc68* objects on top of the same print data source.)69* <p>70* Interface {@code Doc} affords considerable implementation flexibility. The71* print data might already be in existence when the doc object is constructed.72* In this case the objects returned by the doc's methods can be supplied to the73* doc's constructor, be stored in the doc ahead of time, and simply be returned74* when called for. Alternatively, the print data might not exist yet when the75* doc object is constructed. In this case the doc object might provide a "lazy"76* implementation that generates the print data representation object (and/or77* the print data) only when the Print Job calls for it (when the Print Job78* calls the {@link #getPrintData() getPrintData()} method).79* <p>80* There is no restriction on the number of client threads that may be81* simultaneously accessing the same doc. Therefore, all implementations of82* interface {@code Doc} must be designed to be multiple thread safe.83* <p>84* However there can only be one consumer of the print data obtained from a85* {@code Doc}.86* <p>87* If print data is obtained from the client as a stream, by calling88* {@code Doc}'s {@code getReaderForText()} or {@code getStreamForBytes()}89* methods, or because the print data source is already an {@code InputStream}90* or {@code Reader}, then the print service should always close these streams91* for the client on all job completion conditions. With the following caveat.92* If the print data is itself a stream, the service will always close it. If93* the print data is otherwise something that can be requested as a stream, the94* service will only close the stream if it has obtained the stream before95* terminating. That is, just because a print service might request data as a96* stream does not mean that it will, with the implications that {@code Doc}97* implementors which rely on the service to close them should create such98* streams only in response to a request from the service.99*/100public interface Doc {101102/**103* Determines the doc flavor in which this doc object will supply its piece104* of print data.105*106* @return doc flavor107*/108public DocFlavor getDocFlavor();109110/**111* Obtains the print data representation object that contains this doc112* object's piece of print data in the format corresponding to the supported113* doc flavor. The {@code getPrintData()} method returns an instance of the114* representation class whose name is given by{@link #getDocFlavor()115* getDocFlavor()}.{@link DocFlavor#getRepresentationClassName()116* getRepresentationClassName()}, and the return value can be cast from117* class {@code Object} to that representation class.118*119* @return print data representation object120* @throws IOException if the representation class is a stream and there was121* an I/O error while constructing the stream122*/123public Object getPrintData() throws IOException;124125/**126* Obtains the set of printing attributes for this doc object. If the127* returned attribute set includes an instance of a particular attribute128* <i>X,</i> the printer must use that attribute value for this doc,129* overriding any value of attribute <i>X</i> in the job's attribute set. If130* the returned attribute set does not include an instance of a particular131* attribute <i>X</i> or if {@code null} is returned, the printer must132* consult the job's attribute set to obtain the value for attribute133* <i>X,</i> and if not found there, the printer must use an134* implementation-dependent default value. The returned attribute set is135* unmodifiable.136*137* @return unmodifiable set of printing attributes for this doc, or138* {@code null} to obtain all attribute values from the job's139* attribute set140*/141public DocAttributeSet getAttributes();142143/**144* Obtains a reader for extracting character print data from this doc. The145* {@code Doc} implementation is required to support this method if the146* {@code DocFlavor} has one of the following print data representation147* classes, and return {@code null} otherwise:148* <ul>149* <li>char[]150* <li>java.lang.String151* <li>java.io.Reader152* </ul>153* The doc's print data representation object is used to construct and154* return a {@code Reader} for reading the print data as a stream of155* characters from the print data representation object. However, if the156* print data representation object is itself a {@code Reader}, then the157* print data representation object is simply returned.158*159* @return reader for reading the print data characters from this doc. If a160* reader cannot be provided because this doc does not meet the161* criteria stated above, {@code null} is returned.162* @throws IOException if there was an I/O error while creating the reader163*/164public Reader getReaderForText() throws IOException;165166/**167* Obtains an input stream for extracting byte print data from this doc. The168* {@code Doc} implementation is required to support this method if the169* {@code DocFlavor} has one of the following print data representation170* classes, and return {@code null} otherwise:171* <ul>172* <li>byte[]173* <li>java.io.InputStream174* </ul>175* This doc's print data representation object is obtained, then an input176* stream for reading the print data from the print data representation177* object as a stream of bytes is created and returned. However, if the178* print data representation object is itself an input stream, then the179* print data representation object is simply returned.180*181* @return input stream for reading the print data bytes from this doc. If182* an input stream cannot be provided because this doc does not meet183* the criteria stated above, {@code null} is returned.184* @throws IOException if there was an I/O error while creating the input185* stream186*/187public InputStream getStreamForBytes() throws IOException;188}189190191