1 7 8 package javax.print; 9 10 import java.io.ByteArrayInputStream ; 11 import java.io.CharArrayReader ; 12 import java.io.StringReader ; 13 import java.io.InputStream ; 14 import java.io.IOException ; 15 import java.io.Reader ; 16 import javax.print.attribute.AttributeSetUtilities ; 17 import javax.print.attribute.DocAttributeSet ; 18 19 44 45 public final class SimpleDoc implements Doc { 46 47 private DocFlavor flavor; 48 private DocAttributeSet attributes; 49 private Object printData; 50 private Reader reader; 51 private InputStream inStream; 52 53 67 public SimpleDoc(Object printData, 68 DocFlavor flavor, DocAttributeSet attributes) { 69 70 if (flavor == null || printData == null) { 71 throw new IllegalArgumentException ("null argument(s)"); 72 } 73 74 Class repClass = null; 75 try { 76 repClass = Class.forName(flavor.getRepresentationClassName()); 77 } catch (Throwable e) { 78 throw new IllegalArgumentException ("unknown representation class"); 79 } 80 81 if (!repClass.isInstance(printData)) { 82 throw new IllegalArgumentException ("data is not of declared type"); 83 } 84 85 this.flavor = flavor; 86 if (attributes != null) { 87 this.attributes = AttributeSetUtilities.unmodifiableView(attributes); 88 } 89 this.printData = printData; 90 } 91 92 98 public DocFlavor getDocFlavor() { 99 return flavor; 100 } 101 102 118 public DocAttributeSet getAttributes() { 119 return attributes; 120 } 121 122 137 public Object getPrintData() throws IOException { 138 return printData; 139 } 140 141 167 public Reader getReaderForText() throws IOException { 168 169 if (printData instanceof Reader ) { 170 return (Reader )printData; 171 } 172 173 synchronized (this) { 174 if (reader != null) { 175 return reader; 176 } 177 178 if (printData instanceof char[]) { 179 reader = new CharArrayReader ((char[])printData); 180 } 181 else if (printData instanceof String ) { 182 reader = new StringReader ((String )printData); 183 } 184 } 185 return reader; 186 } 187 188 215 public InputStream getStreamForBytes() throws IOException { 216 217 if (printData instanceof InputStream ) { 218 return (InputStream )printData; 219 } 220 221 synchronized (this) { 222 if (inStream != null) { 223 return inStream; 224 } 225 226 if (printData instanceof byte[]) { 227 inStream = new ByteArrayInputStream ((byte[])printData); 228 } 229 } 230 return inStream; 231 } 232 233 } 234 | Popular Tags |