Path: blob/master/src/java.desktop/share/classes/javax/print/SimpleDoc.java
41153 views
/*1* Copyright (c) 2001, 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.ByteArrayInputStream;28import java.io.CharArrayReader;29import java.io.IOException;30import java.io.InputStream;31import java.io.Reader;32import java.io.StringReader;3334import javax.print.attribute.AttributeSetUtilities;35import javax.print.attribute.DocAttributeSet;3637/**38* This class is an implementation of interface {@code Doc} that can be used in39* many common printing requests. It can handle all of the presently defined40* "pre-defined" doc flavors defined as static variables in the41* {@code DocFlavor} class.42* <p>43* In particular this class implements certain required semantics of the44* {@code Doc} specification as follows:45* <ul>46* <li>constructs a stream for the service if requested and appropriate.47* <li>ensures the same object is returned for each call on a method.48* <li>ensures multiple threads can access the {@code Doc}49* <li>performs some validation of that the data matches the doc flavor.50* </ul>51* Clients who want to re-use the doc object in other jobs, or need a52* {@code MultiDoc} will not want to use this class.53* <p>54* If the print data is a stream, or a print job requests data as a stream, then55* {@code SimpleDoc} does not monitor if the service properly closes the stream56* after data transfer completion or job termination. Clients may prefer to use57* provide their own implementation of doc that adds a listener to monitor job58* completion and to validate that resources such as streams are freed (ie59* closed).60*/61public final class SimpleDoc implements Doc {6263/**64* The doc flavor in which this doc will supply its piece of print data.65*/66private DocFlavor flavor;6768/**69* The set of printing attributes for this doc.70*/71private DocAttributeSet attributes;7273/**74* The print data.75*/76private Object printData;7778/**79* The reader for extracting character print data from this doc.80*/81private Reader reader;8283/**84* The input stream for extracting byte print data from this doc.85*/86private InputStream inStream;8788/**89* Constructs a {@code SimpleDoc} with the specified print data, doc flavor90* and doc attribute set.91*92* @param printData the print data object93* @param flavor the {@code DocFlavor} object94* @param attributes a {@code DocAttributeSet}, which can be {@code null}95* @throws IllegalArgumentException if {@code flavor} or {@code printData}96* is {@code null}, or the {@code printData} does not correspond to97* the specified doc flavor--for example, the data is not of the98* type specified as the representation in the {@code DocFlavor}99*/100public SimpleDoc(Object printData,101DocFlavor flavor, DocAttributeSet attributes) {102103if (flavor == null || printData == null) {104throw new IllegalArgumentException("null argument(s)");105}106107Class<?> repClass = null;108try {109String className = flavor.getRepresentationClassName();110sun.reflect.misc.ReflectUtil.checkPackageAccess(className);111repClass = Class.forName(className, false,112Thread.currentThread().getContextClassLoader());113} catch (Throwable e) {114throw new IllegalArgumentException("unknown representation class");115}116117if (!repClass.isInstance(printData)) {118throw new IllegalArgumentException("data is not of declared type");119}120121this.flavor = flavor;122if (attributes != null) {123this.attributes = AttributeSetUtilities.unmodifiableView(attributes);124}125this.printData = printData;126}127128/**129* Determines the doc flavor in which this doc object will supply its piece130* of print data.131*132* @return doc flavor133*/134public DocFlavor getDocFlavor() {135return flavor;136}137138/**139* Obtains the set of printing attributes for this doc object. If the140* returned attribute set includes an instance of a particular attribute141* <i>X,</i> the printer must use that attribute value for this doc,142* overriding any value of attribute <i>X</i> in the job's attribute set. If143* the returned attribute set does not include an instance of a particular144* attribute <i>X</i> or if {@code null} is returned, the printer must145* consult the job's attribute set to obtain the value for attribute146* <i>X,</i> and if not found there, the printer must use an147* implementation-dependent default value. The returned attribute set is148* unmodifiable.149*150* @return unmodifiable set of printing attributes for this doc, or151* {@code null} to obtain all attribute values from the job's152* attribute set153*/154public DocAttributeSet getAttributes() {155return attributes;156}157158/**159* Obtains the print data representation object that contains this doc160* object's piece of print data in the format corresponding to the supported161* doc flavor. The {@code getPrintData()} method returns an instance of the162* representation class whose name is given by {@link #getDocFlavor()163* getDocFlavor()}.{@link DocFlavor#getRepresentationClassName()164* getRepresentationClassName()}, and the return value can be cast from165* class {@code Object} to that representation class.166*167* @return print data representation object168* @throws IOException if the representation class is a stream and there was169* an I/O error while constructing the stream170*/171public Object getPrintData() throws IOException {172return printData;173}174175/**176* Obtains a reader for extracting character print data from this doc. The177* {@code Doc} implementation is required to support this method if the178* {@code DocFlavor} has one of the following print data representation179* classes, and return {@code null} otherwise:180* <ul>181* <li>{@code char[]}182* <li>{@code java.lang.String}183* <li>{@code java.io.Reader}184* </ul>185* The doc's print data representation object is used to construct and186* return a {@code Reader} for reading the print data as a stream of187* characters from the print data representation object. However, if the188* print data representation object is itself a {@code Reader} then the189* print data representation object is simply returned.190*191* @return a {@code Reader} for reading the print data characters from this192* doc. If a reader cannot be provided because this doc does not193* meet the criteria stated above, {@code null} is returned.194* @throws IOException if there was an I/O error while creating the reader195*/196public Reader getReaderForText() throws IOException {197198if (printData instanceof Reader) {199return (Reader)printData;200}201202synchronized (this) {203if (reader != null) {204return reader;205}206207if (printData instanceof char[]) {208reader = new CharArrayReader((char[])printData);209}210else if (printData instanceof String) {211reader = new StringReader((String)printData);212}213}214return reader;215}216217/**218* Obtains an input stream for extracting byte print data from this doc. The219* {@code Doc} implementation is required to support this method if the220* {@code DocFlavor} has one of the following print data representation221* classes; otherwise this method returns {@code null}:222* <ul>223* <li>{@code byte[]}224* <li>{@code java.io.InputStream}225* </ul>226* The doc's print data representation object is obtained. Then, an input227* stream for reading the print data from the print data representation228* object as a stream of bytes is created and returned. However, if the229* print data representation object is itself an input stream then the print230* data representation object is simply returned.231*232* @return an {@code InputStream} for reading the print data bytes from this233* doc. If an input stream cannot be provided because this doc does234* not meet the criteria stated above, {@code null} is returned.235* @throws IOException if there was an I/O error while creating the input236* stream237*/238public InputStream getStreamForBytes() throws IOException {239240if (printData instanceof InputStream) {241return (InputStream)printData;242}243244synchronized (this) {245if (inStream != null) {246return inStream;247}248249if (printData instanceof byte[]) {250inStream = new ByteArrayInputStream((byte[])printData);251}252}253return inStream;254}255}256257258