Example: Creating spooled files

Note: Read the Code example disclaimer for important legal information.
/////////////////////////////////////////////////////////////////////////
//
// Example that shows creating a spooled file on a server from an input stream.
//
/////////////////////////////////////////////////////////////////////////

import java.io.*;
import java.util.*;

import com.ibm.as400.access.*;

class NPExampleCreateSplf
{

// method to create the spooled file on the specified server, in the specified
// output queue from the given input stream.
public SpooledFile createSpooledFile(AS400 system, OutputQueue outputQueue, InputStream in)
{
    SpooledFile spooledFile = null;
    try
    {
        byte[] buf = new byte[2048];
        int bytesRead;
        SpooledFileOutputStream out;
        PrintParameterList parms = new PrintParameterList();

        // create a PrintParameterList with the values that we want
        // to override from the default printer file...we will override
        // the output queue and the copies value.
        parms.setParameter(PrintObject.ATTR_COPIES, 4);
        if (outputQueue != null)
        {
           parms.setParameter(PrintObject.ATTR_OUTPUT_QUEUE, outputQueue.getPath());
        }
        out = new SpooledFileOutputStream(system,
                                          parms,
                                          null,
                                          null);

        // read from the inputstream in until end of stream, passing all data
        // to the spooled file output stream.
        do
        {
            bytesRead = in.read(buf);
            if (bytesRead != -1)
            {
                out.write(buf, 0, bytesRead);
            }
        } while (bytesRead != -1);

        out.close();  // close the spooled file

        spooledFile = out.getSpooledFile();   // get a reference to the new spooled file

    }
    catch (Exception e)
    {
        //...handle exception...
    }
    return spooledFile;
}

}