Jini Print Service Tutorial

Introduction
Lesson 1. The Very Basics
Lesson 2. Print Data Attributes
Lesson 3. Lookup
Lesson 4. Print Job Status
Lesson 5. Print Job Events
Lesson 6. File Print Jobs
Lesson 7. URL Print Jobs
Lesson 8. Service Registration

Lesson 6. File Print Jobs

The previous lessons have used a doc that reads the print data from a string. Another flavor of doc can read the print data from a file located on the client. Below is an example code fragment that prints a local file named "foobar.txt". To transition from the example of Lesson 3 to the current example, we will have to switch to a different kind of doc object, class InputStreamDoc. (We'll omit the print job status monitoring code.)

Lines 37-45 construct an InputStreamDoc object instead of a StringDoc object. The first parameter is an input stream from which the Jini Print Service instance will read the print data. Line 40 opens a FileInputStream on the file named "foobar.txt"; this may throw a FileNotFoundException if the file can't be opened, caught and handled on lines 86-90. Line 39 layers a BufferedInputStream on top of the FileInputStream, and the BufferedInputStream goes into the doc for the Jini Print Service instance to read. (Using a BufferedInputStream improves the efficiency if the Jini Print Service instance should happen to read the input stream one byte at a time.) The second parameter is the MIME type of the data in the file, namely ASCII-encoded plain text. The third parameter is an attribute set containing the document-level attributes. This doc object then undergoes the standard printing process. That's it! The rest of the code is the same as before.

When the Print Request is submitted and the Jini Print Service instance asks the doc object for the print data representation object, the doc object returns the input stream to the Print Job. The Jini Print Service instance reads the input stream up to the end-of-file, then closes the input stream. Whatever bytes the Jini Print Service instance reads -- namely, the contents of the file "foobar.txt" -- constitute the print data.

Although this code will probably work, it has a couple shortcomings. First, what if an I/O error occurs while the Jini Print Service instance is reading the input stream -- how can we detect that, and what do we do in response? Second, what if the Jini Print Service instance suffers a distributed failure, such that it doesn't finish reading the input stream and close the input stream -- how can we detect that, so we can clean up the input stream ourselves? This is a problem similar to the one the Print Job's lease addresses, except the Print Job's and the client's roles are reversed. Lessons TBD will rectify these shortcomings.


DRAFT STANDARD VERSION 1.0 (23-MAY-2000)