File and MVS data set I/O
MVS data set I/O
You can use the standard java.io package to access only files in the HFS or zFS file system. The JZOS toolkit complements this package by providing classes that enable Java™ applications to interact with MVS data sets. Java programs can use JZOS to access any MVS data set that is supported by the C/C++ library, including the following types:
- Partitioned data set (PDS)
- Partitioned data set extended (PDSE)
- Sequential Files
- Virtual sequential access file (VSAM) of the type KSDS, RRDS, or ESDS
- Record mode
- Each read or write processes a single record of a data set.
- Stream mode
- Data set records are presented as a stream of bytes. Each read or write operation reads a
portion of those bytes, irrespective of record boundaries. Stream mode has two types:
- Text (stream) mode: Data set records are converted to a stream of bytes and a "new line" record delimiter is placed in the stream between records, after trailing blanks are removed.
- Binary (stream) mode: Data set records are placed in the stream as is, without record separators.
General data set access with the ZFile class
The com.ibm.jzos.Zfile JZOS class is a general-purpose class that wraps the z/OS® C/C++ Library I/O routines, fopen(), fread(), fwrite(), and so on, for accessing MVS data sets in stream (text or binary) mode and in record mode. API documentation for the ZFile class refers to each C/C++ library I/O routine that is called, so that you can consult the documentation for these routines directly. See z/ OS XL C/C++ Run-Time Library Reference and z/OS XL C/C++ Programming Guide.
Operating system independent text file I/O with the FileFactory class
The com.ibm.jzos.FileFactory JZOS class allows for portable creation of streams, readers and writers on POSIX/DOS files (by using the java.io package), or MVS data sets (by using the ZFile class in text mode). The FileFactory class determines which underlying file system to use based on the file name, so that a portable application can be configured at run time to use the appropriate file name or file system.
High-speed data set record I/O with the RecordReader and RecordWriter classes
The com.ibm.jzos.RecordReader and com.ibm.jzos.RecordWriter classes provide a high-performance interface for reading and writing sequential data sets in record mode. These classes use the native z/OS basic sequential access method (BSAM) for data set interaction. Consider using these classes where a Java application requires very high-performance record mode I/O. In some cases, these classes can provide a 60-70% reduction in CPU usage compared to the ZFile class. The best results from the RecordReader and RecordWriter classes are seen on data sets with a relatively large blocking factor.
Usage recommendations
- For POSIX file stream style access to MVS data sets, use the ZFile class (or the simpler and more operating system independent FileFactory class if only text mode access is required).
- For sequential record mode access to data sets (both text and binary), use the RecordReader and RecordWriter classes. Use the ZFile class in cases where applications require functionality that is not provided by these classes (such as positioning).
- For generalized access to VSAM data sets (KSDS, RRDS, or ESDS), use the ZFile class. The following examples illustrate some common use cases.
Examples
BufferedReader rdr = FileFactory.newBufferedReader("//DD:INPUT");
try {
String line;
while ((line = rdr.readLine()) != null) { System.out.println(line);
}
} finally {
rdr.close();
}
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
MyDocumentHandler handler = new MyDocumentHandler();
ZFile zFile = new Zfile("//MY.XML.DATA", "rb");
try {
parser.parse(zFile.getInputStream(), handler);
} finally {
zFile.close();
}
RecordReader reader = null;
try {
reader = RecordReader.newReader("//my.dataset", ZFileConstants.FLAG_DISP_SHR);
byte[] recordBuf = new byte[reader.getLrecl()];
while ((bytesRead = reader.read(recordBuf)) >= 0) {
...
}
} finally {
if (reader != null) {
reader.close();
}
}
String ddname = ZFile.allocDummyDDName();
String cmd = "alloc fi("+ddname+") da(HLQ.MYDATA) reuse new catalog msg(2)"
+ " recfm(f,b) space(100,50) cyl"
+ " lrecl(80)";
ZFile.bpxwdyn(cmd); // might throw RcException
RecordWriter writer = null;
try {
writer = RecordWriter.newWriterForDD(ddname);
byte[] recordBuf = new byte[writer.getLrecl()];
int bytesToWrite;
while ((bytesToWrite = getNextAppRecord(recordBuf)) > 0) {
writer.write(recordBuf, 0, bytesToWrite);
}
} finally {
if (writer != null) {
try {
writer.close();
} catch (ZFileException zfe) {
zfe.printStackTrace(); // but continue
}
}
try {
ZFile.bpxwdyn("free fi(" + ddname + ") msg(2)");
} catch (RcException rce) {
rce.printStackTrace(); // but continue
}
}