- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathDocFlavor.java
1314 lines (1201 loc) · 55 KB
/
DocFlavor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.print;
importjava.io.IOException;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.io.Serial;
importjava.io.Serializable;
/**
* Class {@code DocFlavor} encapsulates an object that specifies the format in
* which print data is supplied to a {@link DocPrintJob}. "Doc" is a short,
* easy-to-pronounce term that means "a piece of print data." The print data
* format, or "doc flavor", consists of two things:
* <ul>
* <li><b>MIME type.</b> This is a Multipurpose Internet Mail Extensions
* (MIME) media type (as defined in
* <a href="https://www.rfc-editor.org/info/rfc2045">RFC 2045</a> and
* <a href="https://www.rfc-editor.org/info/rfc2046">RFC 2046</a>) that specifies
* how the print data is to be interpreted. The charset of text data should be
* the IANA MIME-preferred name, or its canonical name if no preferred name is
* specified. Additionally a few historical names supported by earlier
* versions of the Java platform may be recognized. See
* <a href="../../../java.base/java/lang/package-summary.html#charenc">
* character encodings</a> for more information on the character encodings
* supported on the Java platform.
* <li><b>Representation class name.</b> This specifies the fully-qualified
* name of the class of the object from which the actual print data comes, as
* returned by the {@link Class#getName() Class.getName()} method. (Thus the
* class name for {@code byte[]} is {@code "[B"}, for {@code char[]} it is
* {@code "[C"}.)
* </ul>
* A {@code DocPrintJob} obtains its print data by means of interface
* {@link Doc Doc}. A {@code Doc} object lets the {@code DocPrintJob} determine
* the doc flavor the client can supply. A {@code Doc} object also lets the
* {@code DocPrintJob} obtain an instance of the doc flavor's representation
* class, from which the {@code DocPrintJob} then obtains the actual print data.
*
* <hr>
* <h2>Client Formatted Print Data</h2>
* There are two broad categories of print data, client formatted print data and
* service formatted print data.
* <p>
* For <b>client formatted print data</b>, the client determines or knows the
* print data format. For example the client may have a JPEG encoded image, a
* {@code URL} for HTML code, or a disk file containing plain text in some
* encoding, possibly obtained from an external source, and requires a way to
* describe the data format to the print service.
* <p>
* The doc flavor's representation class is a conduit for the JPS
* {@code DocPrintJob} to obtain a sequence of characters or bytes from the
* client. The doc flavor's MIME type is one of the standard media types telling
* how to interpret the sequence of characters or bytes. For a list of standard
* media types, see the Internet Assigned Numbers Authority's (IANA's)
* <a href="http://www.iana.org/assignments/media-types/">Media Types Directory
* </a>. Interface {@link Doc Doc} provides two utility operations,
* {@link Doc#getReaderForText() getReaderForText} and
* {@link Doc#getStreamForBytes() getStreamForBytes()}, to help a {@code Doc}
* object's client extract client formatted print data.
* <p>
* For client formatted print data, the print data representation class is
* typically one of the following (although other representation classes are
* permitted):
* <ul>
* <li>Character array ({@code char[]}) -- The print data consists of the
* Unicode characters in the array.
* <li>{@code String} -- The print data consists of the Unicode characters in
* the string.
* <li>Character stream ({@link java.io.Reader java.io.Reader}) -- The print
* data consists of the Unicode characters read from the stream up to the
* end-of-stream.
* <li>Byte array ({@code byte[]}) -- The print data consists of the bytes in
* the array. The bytes are encoded in the character set specified by the doc
* flavor's MIME type. If the MIME type does not specify a character set, the
* default character set is US-ASCII.
* <li>Byte stream ({@link java.io.InputStream java.io.InputStream}) -- The
* print data consists of the bytes read from the stream up to the
* end-of-stream. The bytes are encoded in the character set specified by the
* doc flavor's MIME type. If the MIME type does not specify a character set,
* the default character set is US-ASCII.
* <li>Uniform Resource Locator ({@link java.net.URL URL}) -- The print data
* consists of the bytes read from the URL location. The bytes are encoded in
* the character set specified by the doc flavor's MIME type. If the MIME type
* does not specify a character set, the default character set is US-ASCII.
* When the representation class is a {@code URL}, the print service itself
* accesses and downloads the document directly from its {@code URL} address,
* without involving the client. The service may be some form of network print
* service which is executing in a different environment. This means you
* should not use a {@code URL} print data flavor to print a document at a
* restricted {@code URL} that the client can see but the printer cannot see.
* This also means you should not use a {@code URL} print data flavor to print
* a document stored in a local file that is not available at a {@code URL}
* accessible independently of the client. For example, a file that is not
* served up by an HTTP server or FTP server. To print such documents, let the
* client open an input stream on the {@code URL} or file and use an input
* stream data flavor.
* </ul>
*
* <hr>
* <h2>Default and Platform Encodings</h2>
* For byte print data where the doc flavor's MIME type does not include a
* {@code charset} parameter, the Java Print Service instance assumes the
* US-ASCII character set by default. This is in accordance with
* <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a>, which says the
* default character set is US-ASCII. Note that US-ASCII is a subset of UTF-8,
* so in the future this may be widened if a future RFC endorses UTF-8 as the
* default in a compatible manner.
* <p>
* Also note that this is different than the behaviour of the Java runtime when
* interpreting a stream of bytes as text data. That assumes the default
* encoding for the user's locale. Thus, when spooling a file in local encoding
* to a Java Print Service it is important to correctly specify the encoding.
* Developers working in the English locales should be particularly conscious of
* this, as their platform encoding corresponds to the default mime charset. By
* this coincidence that particular case may work without specifying the
* encoding of platform data.
* <p>
* Every instance of the Java virtual machine has a default character encoding
* determined during virtual-machine startup and typically depends upon the
* locale and charset being used by the underlying operating system. In a
* distributed environment there is no guarantee that two VM share the same
* default encoding. Thus clients which want to stream platform encoded text
* data from the host platform to a Java Print Service instance must explicitly
* declare the charset and not rely on defaults.
* <p>
* The preferred form is the official IANA primary name for an encoding.
* Applications which stream text data should always specify the charset in the
* mime type, which necessitates obtaining the encoding of the host platform for
* data (eg files) stored in that platform's encoding. A {@code CharSet} which
* corresponds to this and is suitable for use in a mime-type for a
* {@code DocFlavor} can be obtained from
* {@link DocFlavor#hostEncoding DocFlavor.hostEncoding} This may not always be
* the primary IANA name but is guaranteed to be understood by this VM. For
* common flavors, the pre-defined *HOST {@code DocFlavors} may be used.
* <p>
* See <a href="../../../java.base/java/lang/package-summary.html#charenc">
* character encodings</a> for more information on the character encodings
* supported on the Java platform.
*
* <hr>
* <h2>Recommended DocFlavors</h2>
* The Java Print Service API does not define any mandatorily supported
* {@code DocFlavors}. However, here are some examples of MIME types that a Java
* Print Service instance might support for client formatted print data. Nested
* classes inside class {@code DocFlavor} declare predefined static constant
* {@code DocFlavor} objects for these example doc flavors; class
* {@code DocFlavor}'s constructor can be used to create an arbitrary doc
* flavor.
* <ul>
* <li>Preformatted text
* <table class="striped">
* <caption>MIME-Types and their descriptions</caption>
* <thead>
* <tr>
* <th scope="col">MIME-Type
* <th scope="col">Description
* </thead>
* <tbody>
* <tr>
* <th scope="row">{@code "text/plain"}
* <td>Plain text in the default character set (US-ASCII)
* <tr>
* <th scope="row"><code> "text/plain; charset=<i>xxx</i>"</code>
* <td>Plain text in character set <i>xxx</i>
* <tr>
* <th scope="row">{@code "text/html"}
* <td>HyperText Markup Language in the default character set (US-ASCII)
* <tr>
* <th scope="row"><code> "text/html; charset=<i>xxx</i>"</code>
* <td>HyperText Markup Language in character set <i>xxx</i>
* </tbody>
* </table>
* In general, preformatted text print data is provided either in a character
* oriented representation class (character array, String, Reader) or in a
* byte oriented representation class (byte array, InputStream, URL).
* <li>Preformatted page description language (PDL) documents
* <table class="striped">
* <caption>MIME-Types and their descriptions</caption>
* <thead>
* <tr>
* <th scope="col">MIME-Type
* <th scope="col">Description
* </thead>
* <tbody>
* <tr>
* <th scope="row">{@code "application/pdf"}
* <td>Portable Document Format document
* <tr>
* <th scope="row">{@code "application/postscript"}
* <td>PostScript document
* <tr>
* <th scope="row">{@code "application/vnd.hp-PCL"}
* <td>Printer Control Language document
* </tbody>
* </table>
* In general, preformatted PDL print data is provided in a byte oriented
* representation class (byte array, {@code InputStream}, {@code URL}).
* <li>Preformatted images
* <table class="striped">
* <caption>MIME-Types and their descriptions</caption>
* <thead>
* <tr>
* <th scope="col">MIME-Type
* <th scope="col">Description
* </thead>
* <tbody>
* <tr>
* <th scope="row">{@code "image/gif"}
* <td>Graphics Interchange Format image
* <tr>
* <th scope="row">{@code "image/jpeg"}
* <td>Joint Photographic Experts Group image
* <tr>
* <th scope="row">{@code "image/png"}
* <td>Portable Network Graphics image
* </tbody>
* </table>
* In general, preformatted image print data is provided in a byte oriented
* representation class (byte array, {@code InputStream}, {@code URL}).
* <li>Preformatted autosense print data
* <table class="striped">
* <caption>MIME-Types and their descriptions</caption>
* <thead>
* <tr>
* <th scope="col">MIME-Type
* <th scope="col">Description
* </thead>
* <tbody>
* <tr>
* <th scope="row">{@code "application/octet-stream"}
* <td>The print data format is unspecified (just an octet stream)
* </tbody>
* </table>
* The printer decides how to interpret the print data; the way this
* "autosensing" works is implementation dependent. In general, preformatted
* autosense print data is provided in a byte oriented representation class
* (byte array, {@code InputStream}, {@code URL}).
* </ul>
*
* <hr>
* <h2>Service Formatted Print Data</h2>
* For <b>service formatted print data</b>, the Java Print Service instance
* determines the print data format. The doc flavor's representation class
* denotes an interface whose methods the {@code DocPrintJob} invokes to
* determine the content to be printed -- such as a renderable image interface
* or a Java printable interface. The doc flavor's MIME type is the special
* value {@code "application/x-java-jvm-local-objectref"} indicating the client
* will supply a reference to a Java object that implements the interface named
* as the representation class. This MIME type is just a placeholder; what's
* important is the print data representation class.
* <p>
* For service formatted print data, the print data representation class is
* typically one of the following (although other representation classes are
* permitted). Nested classes inside class {@code DocFlavor} declare predefined
* static constant {@code DocFlavor} objects for these example doc flavors;
* class {@code DocFlavor}'s constructor can be used to create an arbitrary doc
* flavor.
* <ul>
* <li>Renderable image object -- The client supplies an object that
* implements interface
* {@link java.awt.image.renderable.RenderableImage RenderableImage}. The
* printer calls methods in that interface to obtain the image to be printed.
* <li>Printable object -- The client supplies an object that implements
* interface {@link java.awt.print.Printable Printable}. The printer calls
* methods in that interface to obtain the pages to be printed, one by one.
* For each page, the printer supplies a graphics context, and whatever the
* client draws in that graphics context gets printed.
* <li>Pageable object -- The client supplies an object that implements
* interface {@link java.awt.print.Pageable Pageable}. The printer calls
* methods in that interface to obtain the pages to be printed, one by one.
* For each page, the printer supplies a graphics context, and whatever the
* client draws in that graphics context gets printed.
* </ul>
*
* <hr>
* <h2>Pre-defined Doc Flavors</h2>
* A Java Print Service instance is not <b><i>required</i></b> to support the
* following print data formats and print data representation classes. In fact,
* a developer using this class should <b>never</b> assume that a particular
* print service supports the document types corresponding to these pre-defined
* doc flavors. Always query the print service to determine what doc flavors it
* supports. However, developers who have print services that support these doc
* flavors are encouraged to refer to the predefined singleton instances created
* here.
* <ul>
* <li>Plain text print data provided through a byte stream. Specifically, the
* following doc flavors are recommended to be supported:
* <br>·
* {@code ("text/plain", "java.io.InputStream")}
* <br>·
* {@code ("text/plain; charset=us-ascii", "java.io.InputStream")}
* <br>·
* {@code ("text/plain; charset=utf-8", "java.io.InputStream")}
* <li>Renderable image objects. Specifically, the following doc flavor is
* recommended to be supported:
* <br>·
* {@code ("application/x-java-jvm-local-objectref", "java.awt.image.renderable.RenderableImage")}
* </ul>
* A Java Print Service instance is allowed to support any other doc flavors (or
* none) in addition to the above mandatory ones, at the implementation's
* choice.
* <p>
* Support for the above doc flavors is desirable so a printing client can rely
* on being able to print on any JPS printer, regardless of which doc flavors
* the printer supports. If the printer doesn't support the client's preferred
* doc flavor, the client can at least print plain text, or the client can
* convert its data to a renderable image and print the image.
* <p>
* Furthermore, every Java Print Service instance must fulfill these
* requirements for processing plain text print data:
* <ul>
* <li>The character pair carriage return-line feed (CR-LF) means "go to
* column 1 of the next line."
* <li>A carriage return (CR) character standing by itself means "go to column
* 1 of the next line."
* <li>A line feed (LF) character standing by itself means "go to column 1 of
* the next line."
* </ul>
* The client must itself perform all plain text print data formatting not
* addressed by the above requirements.
*
* <h2>Design Rationale</h2>
* Class {@code DocFlavor} in package {@code javax.print} is similar to class
* {@link java.awt.datatransfer.DataFlavor}. Class {@code DataFlavor} is not
* used in the Java Print Service (JPS) API for three reasons which are all
* rooted in allowing the JPS API to be shared by other print services APIs
* which may need to run on Java profiles which do not include all of the Java
* Platform, Standard Edition.
* <ol type=1>
* <li>The JPS API is designed to be used in Java profiles which do not
* support AWT.
* <li>The implementation of class {@code java.awt.datatransfer.DataFlavor}
* does not guarantee that equivalent data flavors will have the same
* serialized representation. {@code DocFlavor} does, and can be used in
* services which need this.
* <li>The implementation of class {@code java.awt.datatransfer.DataFlavor}
* includes a human presentable name as part of the serialized representation.
* This is not appropriate as part of a service matching constraint.
* </ol>
* Class {@code DocFlavor}'s serialized representation uses the following
* canonical form of a MIME type string. Thus, two doc flavors with MIME types
* that are not identical but that are equivalent (that have the same canonical
* form) may be considered equal.
* <ul>
* <li>The media type, media subtype, and parameters are retained, but all
* comments and whitespace characters are discarded.
* <li>The media type, media subtype, and parameter names are converted to
* lowercase.
* <li>The parameter values retain their original case, except a charset
* parameter value for a text media type is converted to lowercase.
* <li>Quote characters surrounding parameter values are removed.
* <li>Quoting backslash characters inside parameter values are removed.
* <li>The parameters are arranged in ascending order of parameter name.
* </ul>
* Class {@code DocFlavor}'s serialized representation also contains the
* fully-qualified class <i>name</i> of the representation class (a
* {@code String} object), rather than the representation class itself (a
* {@code Class} object). This allows a client to examine the doc flavors a Java
* Print Service instance supports without having to load the representation
* classes, which may be problematic for limited-resource clients.
*
* @spec https://www.rfc-editor.org/info/rfc2045
* RFC 2045: Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies
* @spec https://www.rfc-editor.org/info/rfc2046
* RFC 2046: Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types
* @author Alan Kaminsky
*/
publicclassDocFlavorimplementsSerializable, Cloneable {
/**
* Use serialVersionUID from JDK 1.4 for interoperability.
*/
@Serial
privatestaticfinallongserialVersionUID = -4512080796965449721L;
/**
* A string representing the host operating system encoding. This will
* follow the conventions documented in
* <a href="https://www.rfc-editor.org/info/rfc2278">
* <i>RFC 2278: IANA Charset Registration Procedures</i></a>
* except where historical names are returned for compatibility with
* previous versions of the Java platform. The value returned from method is
* valid only for the VM which returns it, for use in a {@code DocFlavor}.
* This is the charset for all the "HOST" pre-defined {@code DocFlavors} in
* the executing VM.
* @spec https://www.rfc-editor.org/info/rfc2278
* RFC 2278: IANA Charset Registration Procedures
*/
publicstaticfinalStringhostEncoding = System.getProperty("file.encoding");
/**
* MIME type.
*/
privatetransientMimeTypemyMimeType;
/**
* Representation class name.
*
* @serial
*/
privateStringmyClassName;
/**
* String value for this doc flavor. Computed when needed and cached.
*/
privatetransientStringmyStringValue = null;
/**
* Constructs a new doc flavor object from the given MIME type and
* representation class name. The given MIME type is converted into
* canonical form and stored internally.
*
* @param mimeType MIME media type string
* @param className fully-qualified representation class name
* @throws NullPointerException if {@code mimeType} or {@code className} is
* {@code null}
* @throws IllegalArgumentException if {@code mimeType} does not obey the
* syntax for a MIME media type string
*/
publicDocFlavor(StringmimeType, StringclassName) {
if (className == null) {
thrownewNullPointerException();
}
myMimeType = newMimeType (mimeType);
myClassName = className;
}
/**
* Returns this doc flavor object's MIME type string based on the canonical
* form. Each parameter value is enclosed in quotes.
*
* @return the mime type
*/
publicStringgetMimeType() {
returnmyMimeType.getMimeType();
}
/**
* Returns this doc flavor object's media type (from the MIME type).
*
* @return the media type
*/
publicStringgetMediaType() {
returnmyMimeType.getMediaType();
}
/**
* Returns this doc flavor object's media subtype (from the MIME type).
*
* @return the media sub-type
*/
publicStringgetMediaSubtype() {
returnmyMimeType.getMediaSubtype();
}
/**
* Returns a {@code String} representing a MIME parameter. Mime types may
* include parameters which are usually optional. The charset for text types
* is a commonly useful example. This convenience method will return the
* value of the specified parameter if one was specified in the mime type
* for this flavor.
*
* @param paramName the name of the parameter. This name is internally
* converted to the canonical lower case format before performing
* the match.
* @return a string representing a mime parameter, or {@code null} if that
* parameter is not in the mime type string
* @throws NullPointerException if paramName is {@code null}
*/
publicStringgetParameter(StringparamName) {
returnmyMimeType.getParameterMap().get(paramName.toLowerCase());
}
/**
* Returns the name of this doc flavor object's representation class.
*
* @return the name of the representation class
*/
publicStringgetRepresentationClassName() {
returnmyClassName;
}
/**
* Converts this {@code DocFlavor} to a string.
*
* @return MIME type string based on the canonical form. Each parameter
* value is enclosed in quotes. A "class=" parameter is appended to
* the MIME type string to indicate the representation class name.
*/
publicStringtoString() {
returngetStringValue();
}
/**
* Returns a hash code for this doc flavor object.
*/
publicinthashCode() {
returngetStringValue().hashCode();
}
/**
* Determines if this doc flavor object is equal to the given object. The
* two are equal if the given object is not {@code null}, is an instance of
* {@code DocFlavor}, has a MIME type equivalent to this doc flavor object's
* MIME type (that is, the MIME types have the same media type, media
* subtype, and parameters), and has the same representation class name as
* this doc flavor object. Thus, if two doc flavor objects' MIME types are
* the same except for comments, they are considered equal. However, two doc
* flavor objects with MIME types of "text/plain" and "text/plain;
* charset=US-ASCII" are not considered equal, even though they represent
* the same media type (because the default character set for plain text is
* US-ASCII).
*
* @param obj {@code Object} to test
* @return {@code true} if this doc flavor object equals {@code obj},
* {@code false} otherwise
*/
publicbooleanequals(Objectobj) {
returnobjinstanceofDocFlavorother &&
getStringValue().equals(other.getStringValue());
}
/**
* Returns this doc flavor object's string value.
*
* @return the string value
*/
privateStringgetStringValue() {
if (myStringValue == null) {
myStringValue = myMimeType + "; class=\"" + myClassName + "\"";
}
returnmyStringValue;
}
/**
* Write the instance to a stream (ie serialize the object).
*
* @param s the output stream
* @throws IOException if I/O errors occur while writing to the underlying
* stream
*/
@Serial
privatevoidwriteObject(ObjectOutputStreams) throwsIOException {
s.defaultWriteObject();
s.writeObject(myMimeType.getMimeType());
}
/**
* Reconstitute an instance from a stream (that is, deserialize it).
*
* @param s the input stream
* @throws ClassNotFoundException if the class of a serialized object could
* not be found
* @throws IOException if I/O errors occur while reading from the underlying
* stream
* @serialData The serialised form of a {@code DocFlavor} is the
* {@code String} naming the representation class followed by
* the {@code String} representing the canonical form of the
* mime type
*/
@Serial
privatevoidreadObject(ObjectInputStreams)
throwsClassNotFoundException, IOException {
s.defaultReadObject();
myMimeType = newMimeType((String)s.readObject());
}
/**
* Class {@code DocFlavor.BYTE_ARRAY} provides predefined static constant
* {@code DocFlavor} objects for example doc flavors using a byte array
* ({@code byte[]}) as the print data representation class.
*
* @author Alan Kaminsky
*/
publicstaticclassBYTE_ARRAYextendsDocFlavor {
/**
* Use serialVersionUID from JDK 1.4 for interoperability.
*/
@Serial
privatestaticfinallongserialVersionUID = -9065578006593857475L;
/**
* Constructs a new doc flavor with the given MIME type and a print data
* representation class name of {@code "[B"} (byte array).
*
* @param mimeType MIME media type string
* @throws NullPointerException if {@code mimeType} is {@code null}
* @throws IllegalArgumentException if {@code mimeType} does not obey
* the syntax for a MIME media type string
*/
publicBYTE_ARRAY (StringmimeType) {
super (mimeType, "[B");
}
/**
* Doc flavor with MIME type = {@code "text/plain"}, encoded in the host
* platform encoding. See {@link DocFlavor#hostEncoding hostEncoding}.
* Print data representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_PLAIN_HOST =
newBYTE_ARRAY ("text/plain; charset="+hostEncoding);
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-8"},
* print data representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_PLAIN_UTF_8 =
newBYTE_ARRAY ("text/plain; charset=utf-8");
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-16"},
* print data representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_PLAIN_UTF_16 =
newBYTE_ARRAY ("text/plain; charset=utf-16");
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-16be"}
* (big-endian byte ordering), print data representation class name =
* {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_PLAIN_UTF_16BE =
newBYTE_ARRAY ("text/plain; charset=utf-16be");
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-16le"}
* (little-endian byte ordering), print data representation class name =
* {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_PLAIN_UTF_16LE =
newBYTE_ARRAY ("text/plain; charset=utf-16le");
/**
* Doc flavor with MIME type = {@code "text/plain; charset=us-ascii"},
* print data representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_PLAIN_US_ASCII =
newBYTE_ARRAY ("text/plain; charset=us-ascii");
/**
* Doc flavor with MIME type = {@code "text/html"}, encoded in the host
* platform encoding. See {@link DocFlavor#hostEncoding hostEncoding}.
* Print data representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_HTML_HOST =
newBYTE_ARRAY ("text/html; charset="+hostEncoding);
/**
* Doc flavor with MIME type = {@code "text/html; charset=utf-8"}, print
* data representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_HTML_UTF_8 =
newBYTE_ARRAY ("text/html; charset=utf-8");
/**
* Doc flavor with MIME type = {@code "text/html; charset=utf-16"},
* print data representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_HTML_UTF_16 =
newBYTE_ARRAY ("text/html; charset=utf-16");
/**
* Doc flavor with MIME type = {@code "text/html; charset=utf-16be"}
* (big-endian byte ordering), print data representation class name =
* {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_HTML_UTF_16BE =
newBYTE_ARRAY ("text/html; charset=utf-16be");
/**
* Doc flavor with MIME type = {@code "text/html; charset=utf-16le"}
* (little-endian byte ordering), print data representation class name =
* {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_HTML_UTF_16LE =
newBYTE_ARRAY ("text/html; charset=utf-16le");
/**
* Doc flavor with MIME type = {@code "text/html; charset=us-ascii"},
* print data representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYTEXT_HTML_US_ASCII =
newBYTE_ARRAY ("text/html; charset=us-ascii");
/**
* Doc flavor with MIME type = {@code "application/pdf"}, print data
* representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYPDF = newBYTE_ARRAY ("application/pdf");
/**
* Doc flavor with MIME type = {@code "application/postscript"}, print
* data representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYPOSTSCRIPT =
newBYTE_ARRAY ("application/postscript");
/**
* Doc flavor with MIME type = {@code "application/vnd.hp-PCL"}, print
* data representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYPCL =
newBYTE_ARRAY ("application/vnd.hp-PCL");
/**
* Doc flavor with MIME type = {@code "image/gif"}, print data
* representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYGIF = newBYTE_ARRAY ("image/gif");
/**
* Doc flavor with MIME type = {@code "image/jpeg"}, print data
* representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYJPEG = newBYTE_ARRAY ("image/jpeg");
/**
* Doc flavor with MIME type = {@code "image/png"}, print data
* representation class name = {@code "[B"} (byte array).
*/
publicstaticfinalBYTE_ARRAYPNG = newBYTE_ARRAY ("image/png");
/**
* Doc flavor with MIME type = {@code "application/octet-stream"}, print
* data representation class name = {@code "[B"} (byte array). The
* client must determine that data described using this
* {@code DocFlavor} is valid for the printer.
*/
publicstaticfinalBYTE_ARRAYAUTOSENSE =
newBYTE_ARRAY ("application/octet-stream");
}
/**
* Class {@code DocFlavor.INPUT_STREAM} provides predefined static constant
* {@code DocFlavor} objects for example doc flavors using a byte stream
* ({@link java.io.InputStream java.io.InputStream}) as the print data
* representation class.
*
* @author Alan Kaminsky
*/
publicstaticclassINPUT_STREAMextendsDocFlavor {
/**
* Use serialVersionUID from JDK 1.4 for interoperability.
*/
@Serial
privatestaticfinallongserialVersionUID = -7045842700749194127L;
/**
* Constructs a new doc flavor with the given MIME type and a print data
* representation class name of {@code "java.io.InputStream"} (byte
* stream).
*
* @param mimeType MIME media type string
* @throws NullPointerException if {@code mimeType} is {@code null}
* @throws IllegalArgumentException if {@code mimeType} does not obey
* the syntax for a MIME media type string.
*/
publicINPUT_STREAM (StringmimeType) {
super (mimeType, "java.io.InputStream");
}
/**
* Doc flavor with MIME type = {@code "text/plain"}, encoded in the host
* platform encoding. See {@link DocFlavor#hostEncoding hostEncoding}.
* Print data representation class name = {@code "java.io.InputStream"}
* (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_PLAIN_HOST =
newINPUT_STREAM ("text/plain; charset="+hostEncoding);
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-8"},
* print data representation class name = {@code "java.io.InputStream"}
* (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_PLAIN_UTF_8 =
newINPUT_STREAM ("text/plain; charset=utf-8");
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-16"},
* print data representation class name = {@code "java.io.InputStream"}
* (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_PLAIN_UTF_16 =
newINPUT_STREAM ("text/plain; charset=utf-16");
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-16be"}
* (big-endian byte ordering), print data representation class name =
* {@code "java.io.InputStream"} (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_PLAIN_UTF_16BE =
newINPUT_STREAM ("text/plain; charset=utf-16be");
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-16le"}
* (little-endian byte ordering), print data representation class name =
* {@code "java.io.InputStream"} (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_PLAIN_UTF_16LE =
newINPUT_STREAM ("text/plain; charset=utf-16le");
/**
* Doc flavor with MIME type = {@code "text/plain; charset=us-ascii"},
* print data representation class name = {@code "java.io.InputStream"}
* (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_PLAIN_US_ASCII =
newINPUT_STREAM ("text/plain; charset=us-ascii");
/**
* Doc flavor with MIME type = {@code "text/html"}, encoded in the host
* platform encoding. See {@link DocFlavor#hostEncoding hostEncoding}.
* Print data representation class name = {@code "java.io.InputStream"}
* (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_HTML_HOST =
newINPUT_STREAM ("text/html; charset="+hostEncoding);
/**
* Doc flavor with MIME type = {@code "text/html; charset=utf-8"}, print
* data representation class name = {@code "java.io.InputStream"} (byte
* stream).
*/
publicstaticfinalINPUT_STREAMTEXT_HTML_UTF_8 =
newINPUT_STREAM ("text/html; charset=utf-8");
/**
* Doc flavor with MIME type = {@code "text/html; charset=utf-16"},
* print data representation class name = {@code "java.io.InputStream"}
* (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_HTML_UTF_16 =
newINPUT_STREAM ("text/html; charset=utf-16");
/**
* Doc flavor with MIME type = {@code "text/html; charset=utf-16be"}
* (big-endian byte ordering), print data representation class name =
* {@code "java.io.InputStream"} (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_HTML_UTF_16BE =
newINPUT_STREAM ("text/html; charset=utf-16be");
/**
* Doc flavor with MIME type = {@code "text/html; charset=utf-16le"}
* (little-endian byte ordering), print data representation class name =
* {@code "java.io.InputStream"} (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_HTML_UTF_16LE =
newINPUT_STREAM ("text/html; charset=utf-16le");
/**
* Doc flavor with MIME type = {@code "text/html; charset=us-ascii"},
* print data representation class name = {@code "java.io.InputStream"}
* (byte stream).
*/
publicstaticfinalINPUT_STREAMTEXT_HTML_US_ASCII =
newINPUT_STREAM ("text/html; charset=us-ascii");
/**
* Doc flavor with MIME type = {@code "application/pdf"}, print data
* representation class name = {@code "java.io.InputStream"} (byte
* stream).
*/
publicstaticfinalINPUT_STREAMPDF = newINPUT_STREAM ("application/pdf");
/**
* Doc flavor with MIME type = {@code "application/postscript"}, print
* data representation class name = {@code "java.io.InputStream"} (byte
* stream).
*/
publicstaticfinalINPUT_STREAMPOSTSCRIPT =
newINPUT_STREAM ("application/postscript");
/**
* Doc flavor with MIME type = {@code "application/vnd.hp-PCL"}, print
* data representation class name = {@code "java.io.InputStream"} (byte
* stream).
*/
publicstaticfinalINPUT_STREAMPCL =
newINPUT_STREAM ("application/vnd.hp-PCL");
/**
* Doc flavor with MIME type = {@code "image/gif"}, print data
* representation class name = {@code "java.io.InputStream"} (byte
* stream).
*/
publicstaticfinalINPUT_STREAMGIF = newINPUT_STREAM ("image/gif");
/**
* Doc flavor with MIME type = {@code "image/jpeg"}, print data
* representation class name = {@code "java.io.InputStream"} (byte
* stream).
*/
publicstaticfinalINPUT_STREAMJPEG = newINPUT_STREAM ("image/jpeg");
/**
* Doc flavor with MIME type = {@code "image/png"}, print data
* representation class name = {@code "java.io.InputStream"} (byte
* stream).
*/
publicstaticfinalINPUT_STREAMPNG = newINPUT_STREAM ("image/png");
/**
* Doc flavor with MIME type = {@code "application/octet-stream"}, print
* data representation class name = {@code "java.io.InputStream"} (byte
* stream). The client must determine that data described using this
* {@code DocFlavor} is valid for the printer.
*/
publicstaticfinalINPUT_STREAMAUTOSENSE =
newINPUT_STREAM ("application/octet-stream");
}
/**
* Class {@code DocFlavor.URL} provides predefined static constant
* {@code DocFlavor} objects. For example doc flavors using a Uniform
* Resource Locator ({@link java.net.URL java.net.URL}) as the print data
* representation class.
*
* @author Alan Kaminsky
*/
publicstaticclassURLextendsDocFlavor {
/**
* Use serialVersionUID from JDK 1.4 for interoperability.
*/
@Serial
privatestaticfinallongserialVersionUID = 2936725788144902062L;
/**
* Constructs a new doc flavor with the given MIME type and a print data
* representation class name of {@code "java.net.URL"}.
*
* @param mimeType MIME media type string
* @throws NullPointerException if {@code mimeType} is {@code null}
* @throws IllegalArgumentException if {@code mimeType} does not obey
* the syntax for a MIME media type string
*/
publicURL (StringmimeType) {
super (mimeType, "java.net.URL");
}
/**
* Doc flavor with MIME type = {@code "text/plain"}, encoded in the host
* platform encoding. See {@link DocFlavor#hostEncoding hostEncoding}.
* Print data representation class name = {@code "java.net.URL"} (byte
* stream).
*/
publicstaticfinalURLTEXT_PLAIN_HOST =
newURL ("text/plain; charset="+hostEncoding);
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-8"},
* print data representation class name = {@code "java.net.URL"} (byte
* stream).
*/
publicstaticfinalURLTEXT_PLAIN_UTF_8 =
newURL ("text/plain; charset=utf-8");
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-16"},
* print data representation class name = {@code java.net.URL""} (byte
* stream).
*/
publicstaticfinalURLTEXT_PLAIN_UTF_16 =
newURL ("text/plain; charset=utf-16");
/**
* Doc flavor with MIME type = {@code "text/plain; charset=utf-16be"}
* (big-endian byte ordering), print data representation class name =