123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- package javax.print;
- import java.io.ByteArrayInputStream;
- import java.io.CharArrayReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.Reader;
- import java.io.StringReader;
- import javax.print.attribute.AttributeSetUtilities;
- import javax.print.attribute.DocAttributeSet;
- public final class SimpleDoc implements Doc
- {
- private final Object printData;
- private final DocFlavor flavor;
- private final DocAttributeSet attributes;
- private InputStream stream;
- private Reader reader;
-
- public SimpleDoc(Object printData, DocFlavor flavor,
- DocAttributeSet attributes)
- {
- if (printData == null || flavor == null)
- throw new IllegalArgumentException("printData/flavor may not be null");
- if (! (printData.getClass().getName().equals(
- flavor.getRepresentationClassName())
- || flavor.getRepresentationClassName().equals("java.io.Reader")
- && printData instanceof Reader
- || flavor.getRepresentationClassName().equals("java.io.InputStream")
- && printData instanceof InputStream))
- {
- throw new IllegalArgumentException("data is not of declared flavor type");
- }
- this.printData = printData;
- this.flavor = flavor;
- if (attributes != null)
- this.attributes = AttributeSetUtilities.unmodifiableView(attributes);
- else
- this.attributes = null;
- stream = null;
- reader = null;
- }
-
- public DocAttributeSet getAttributes()
- {
- return attributes;
- }
-
- public DocFlavor getDocFlavor()
- {
- return flavor;
- }
-
- public Object getPrintData() throws IOException
- {
- return printData;
- }
-
- public Reader getReaderForText() throws IOException
- {
- synchronized (this)
- {
-
- if (reader == null)
- {
- if (flavor instanceof DocFlavor.CHAR_ARRAY)
- reader = new CharArrayReader((char[]) printData);
- else if (flavor instanceof DocFlavor.STRING)
- reader = new StringReader((String) printData);
- else if (flavor instanceof DocFlavor.READER)
- reader = (Reader) printData;
- }
- return reader;
- }
- }
-
- public InputStream getStreamForBytes() throws IOException
- {
- synchronized (this)
- {
-
- if (stream == null)
- {
- if (flavor instanceof DocFlavor.BYTE_ARRAY)
- stream = new ByteArrayInputStream((byte[]) printData);
- else if (flavor instanceof DocFlavor.INPUT_STREAM)
- stream = (InputStream) printData;
- }
- return stream;
- }
- }
- }
|