Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/print/SimpleDoc.java
38829 views
/*1* Copyright (c) 2001, 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.StringReader;30import java.io.InputStream;31import java.io.IOException;32import java.io.Reader;33import javax.print.attribute.AttributeSetUtilities;34import javax.print.attribute.DocAttributeSet;3536/**37* This class is an implementation of interface <code>Doc</code> that can38* be used in many common printing requests.39* It can handle all of the presently defined "pre-defined" doc flavors40* defined as static variables in the DocFlavor class.41* <p>42* In particular this class implements certain required semantics of the43* Doc specification as follows:44* <ul>45* <li>constructs a stream for the service if requested and appropriate.46* <li>ensures the same object is returned for each call on a method.47* <li>ensures multiple threads can access the Doc48* <li>performs some validation of that the data matches the doc flavor.49* </ul>50* Clients who want to re-use the doc object in other jobs,51* or need a MultiDoc will not want to use this class.52* <p>53* If the print data is a stream, or a print job requests data as a54* stream, then <code>SimpleDoc</code> does not monitor if the service55* properly closes the stream after data transfer completion or job56* termination.57* Clients may prefer to use provide their own implementation of doc that58* adds a listener to monitor job completion and to validate that59* resources such as streams are freed (ie closed).60*/6162public final class SimpleDoc implements Doc {6364private DocFlavor flavor;65private DocAttributeSet attributes;66private Object printData;67private Reader reader;68private InputStream inStream;6970/**71* Constructs a <code>SimpleDoc</code> with the specified72* print data, doc flavor and doc attribute set.73* @param printData the print data object74* @param flavor the <code>DocFlavor</code> object75* @param attributes a <code>DocAttributeSet</code>, which can76* be <code>null</code>77* @throws IllegalArgumentException if <code>flavor</code> or78* <code>printData</code> is <code>null</code>, or the79* <code>printData</code> does not correspond80* to the specified doc flavor--for example, the data is81* not of the type specified as the representation in the82* <code>DocFlavor</code>.83*/84public SimpleDoc(Object printData,85DocFlavor flavor, DocAttributeSet attributes) {8687if (flavor == null || printData == null) {88throw new IllegalArgumentException("null argument(s)");89}9091Class repClass = null;92try {93String className = flavor.getRepresentationClassName();94sun.reflect.misc.ReflectUtil.checkPackageAccess(className);95repClass = Class.forName(className, false,96Thread.currentThread().getContextClassLoader());97} catch (Throwable e) {98throw new IllegalArgumentException("unknown representation class");99}100101if (!repClass.isInstance(printData)) {102throw new IllegalArgumentException("data is not of declared type");103}104105this.flavor = flavor;106if (attributes != null) {107this.attributes = AttributeSetUtilities.unmodifiableView(attributes);108}109this.printData = printData;110}111112/**113* Determines the doc flavor in which this doc object will supply its114* piece of print data.115*116* @return Doc flavor.117*/118public DocFlavor getDocFlavor() {119return flavor;120}121122/**123* Obtains the set of printing attributes for this doc object. If the124* returned attribute set includes an instance of a particular attribute125* <I>X,</I> the printer must use that attribute value for this doc,126* overriding any value of attribute <I>X</I> in the job's attribute set.127* If the returned attribute set does not include an instance128* of a particular attribute <I>X</I> or if null is returned, the printer129* must consult the job's attribute set to obtain the value for130* attribute <I>X,</I> and if not found there, the printer must use an131* implementation-dependent default value. The returned attribute set is132* unmodifiable.133*134* @return Unmodifiable set of printing attributes for this doc, or null135* to obtain all attribute values from the job's attribute136* set.137*/138public DocAttributeSet getAttributes() {139return attributes;140}141142/*143* Obtains the print data representation object that contains this doc144* object's piece of print data in the format corresponding to the145* supported doc flavor.146* The <CODE>getPrintData()</CODE> method returns an instance of147* the representation class whose name is given by148* {@link DocFlavor#getRepresentationClassName() getRepresentationClassName},149* and the return value can be cast150* from class Object to that representation class.151*152* @return Print data representation object.153*154* @exception IOException if the representation class is a stream and155* there was an I/O error while constructing the stream.156*/157public Object getPrintData() throws IOException {158return printData;159}160161/**162* Obtains a reader for extracting character print data from this doc.163* The <code>Doc</code> implementation is required to support this164* method if the <code>DocFlavor</code> has one of the following print165* data representation classes, and return <code>null</code>166* otherwise:167* <UL>168* <LI> <code>char[]</code>169* <LI> <code>java.lang.String</code>170* <LI> <code>java.io.Reader</code>171* </UL>172* The doc's print data representation object is used to construct and173* return a <code>Reader</code> for reading the print data as a stream174* of characters from the print data representation object.175* However, if the print data representation object is itself a176* <code>Reader</code> then the print data representation object is177* simply returned.178* <P>179* @return a <code>Reader</code> for reading the print data180* characters from this doc.181* If a reader cannot be provided because this doc does not meet182* the criteria stated above, <code>null</code> is returned.183*184* @exception IOException if there was an I/O error while creating185* the reader.186*/187public Reader getReaderForText() throws IOException {188189if (printData instanceof Reader) {190return (Reader)printData;191}192193synchronized (this) {194if (reader != null) {195return reader;196}197198if (printData instanceof char[]) {199reader = new CharArrayReader((char[])printData);200}201else if (printData instanceof String) {202reader = new StringReader((String)printData);203}204}205return reader;206}207208/**209* Obtains an input stream for extracting byte print data from210* this doc.211* The <code>Doc</code> implementation is required to support this212* method if the <code>DocFlavor</code> has one of the following print213* data representation classes; otherwise this method214* returns <code>null</code>:215* <UL>216* <LI> <code>byte[]</code>217* <LI> <code>java.io.InputStream</code>218* </UL>219* The doc's print data representation object is obtained. Then, an220* input stream for reading the print data221* from the print data representation object as a stream of bytes is222* created and returned.223* However, if the print data representation object is itself an224* input stream then the print data representation object is simply225* returned.226* <P>227* @return an <code>InputStream</code> for reading the print data228* bytes from this doc. If an input stream cannot be229* provided because this doc does not meet230* the criteria stated above, <code>null</code> is returned.231*232* @exception IOException233* if there was an I/O error while creating the input stream.234*/235public InputStream getStreamForBytes() throws IOException {236237if (printData instanceof InputStream) {238return (InputStream)printData;239}240241synchronized (this) {242if (inStream != null) {243return inStream;244}245246if (printData instanceof byte[]) {247inStream = new ByteArrayInputStream((byte[])printData);248}249}250return inStream;251}252253}254255256