I read the contents of spool files all the time using this class. In my
situation, XML data is put into the spool file for processing later (an
XSLT transformation). I need to extract just the xml text content of the
spool file to do this. The 'special' characters you are seeing are print
control characters (e.g. page feed). In order to get the text out you
need to do use a workstation customization object that strips out the
control characters. Give this a try:

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.ibm.as400.access.AS400Exception;
import com.ibm.as400.access.AS400SecurityException;
import com.ibm.as400.access.ErrorCompletingRequestException;
import com.ibm.as400.access.PrintObject;
import com.ibm.as400.access.PrintParameterList;
import com.ibm.as400.access.RequestNotSupportedException;
import com.ibm.as400.access.SpooledFile;

public class SpoolFileReader {
private static final String PLAIN_TEXT_CUSTOMIZATION_OBJ =
"/QSYS.LIB/QWPDEFAULT.WSCST";
private static final PrintParameterList printParameterList = new
PrintParameterList();
static {
initializePrintParameterList();
}

private static void initializePrintParameterList() {
printParameterList.setParameter(PrintObject.ATTR_MFGTYPE, "*WSCST"
);
printParameterList.setParameter(PrintObject.
ATTR_WORKSTATION_CUST_OBJECT, PLAIN_TEXT_CUSTOMIZATION_OBJ);
}

private final SpooledFile spooledFile;

public SpoolFileReader(SpooledFile spooledFile) {
this.spooledFile = spooledFile;
}

public String extractText() throws AS400Exception,
AS400SecurityException, ErrorCompletingRequestException, IOException,
InterruptedException, RequestNotSupportedException {
// DO NOT WRAP THE TRANSFORMED INPUT STREAM with a
PrintControlFilterInputStream.
// This would be the standard, best way to read from the stream,
but it causes the read to take ~30 seconds instead of < 1 second.
// Instead, read the input stream into a String, then do a second
read from that string with the PrintControlFilterInputStream.
final InputStream inStream = spooledFile
.getTransformedInputStream(printParameterList);
final BufferedReader reader = new BufferedReader(new
InputStreamReader(inStream));

String currLine = null;
final StringBuffer buffer = new StringBuffer();
while ((currLine = reader.readLine()) != null) {
buffer.append(currLine);
}

final String filterContent = filterContent(buffer.toString());
reader.close();

return filterContent;

}

private static String filterContent(final String rawSpoolFileContent)
throws IOException {
final InputStream filteredInputStream = new
PrintControlFilterInputStream(new
ByteArrayInputStream(rawSpoolFileContent.getBytes()));
final BufferedReader filteredReader = new BufferedReader(new
InputStreamReader(filteredInputStream));
final StringBuffer filteredBuffer = new StringBuffer();
String currLine = null;
while ((currLine = filteredReader.readLine()) != null) {
filteredBuffer.append(currLine);
}

filteredReader.close();
filteredInputStream.close();

return filteredBuffer.toString();
}

private static class PrintControlFilterInputStream extends InputStream
{
private InputStream delegate;

public PrintControlFilterInputStream(InputStream is) {
delegate = is;
}

public int read() throws IOException {
int result;
do {
result = delegate.read();
} while (result != -1 && (result < 32 || result > 126));

return result;
}

}


}

Then in your calling code, do something like this:
String content = new SpoolFileReader(spoolFile).extractText()

Hope this helps,
Phil




"Urbanek, Marty" <Marty_Urbanek@xxxxxxxxxxxx>
Sent by: java400-l-bounces@xxxxxxxxxxxx
08/11/2007 08:25 AM
Please respond to
Java Programming on and around the iSeries / AS400
<java400-l@xxxxxxxxxxxx>


To
<java400-l@xxxxxxxxxxxx>
cc

Subject
RE: Reading spool file with Java






Thanks, Joe.

There is some sort of header of approximately a couple hundred chars
that is mostly unreadable except for the occasional printable character
that shows up, such as a K or a <.

Following the "header", the "report" part looks pretty good, but there
seems to be some sort of marker before each text field. Most of them are
left braces "{" but sometimes it is a left brace + another character
such as {/ or {". Almost like the left braces could be a tab.

I cannot tell what the data is in hex because I don't know enough Java
to make it print the data in hex.

Thanks,
-Marty

------------------------------

date: Thu, 8 Nov 2007 08:18:13 -0600
from: "Joe Pluta" <joepluta@xxxxxxxxxxxxxxxxx>
subject: RE: Reading spool file with Java

Hi Marty!

Is the "junk" hex data or text? It's been a little while since I looked
at
the data in depth, but it might be the printer control information
(space
and skip) from the file. I'll check my code.

Joe


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2021 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.