On 29/04/2023 11:48 am, Paul Gilmartin wrote:
Obviously a complete Java program needs very few comments.
Fair point... although I would contend that a Java program without
comments is easier to understand than DFSORT control statements! Here is
a version with comments (again, untested):
import java.io.*;
import java.util.*;
import com.ibm.jzos.*;
import com.ibm.jzos.fields.*;
import com.ibm.jzos.fields.daa.*;
public class App
{
public static void main(String[] args) throws IOException
{
// define JZOS fields for the key, count and ddname
BinaryUnsignedIntField keyField = new BinaryUnsignedIntField(0, 2);
BinaryUnsignedIntField countField = new
BinaryUnsignedIntField(4, 4);
StringField ddField = new StringField(8, 8);
// variable to save the current header record
// I'm not sure I understand the input data i.e. why this is
// necessary but I think this matches the logic from the File
Manager
// sample
byte[] nextHeader = null;
// map of ddname -> OutEntry containing RecordWriter and count
Map<String, OutEntry> outEntries = new HashMap<>();
RecordReader input = null;
try
{
// open the input dataset
input = RecordReader.newReaderForDD("INPUT");
int bytesRead = 0;
// create a buffer for the input record
byte[] record = new byte[input.getLrecl()];
// read records
while ((bytesRead = input.read(record)) >= 0)
{
if (keyField.getInt(record) == 0) // if key is zero
{
// copy and save the header record to a new byte array
nextHeader = Arrays.copyOfRange(record, 0, bytesRead);
}
else
{
// get the ddname from the record
// (this probably doesn't match the File Manager
sample -
// might need to concatenate/substring etc)
String ddname = ddField.getString(record);
// find the output entry for this ddname
OutEntry out = outEntries.get(ddname);
if (out == null) // doesn't exist: this is the
first record for database
{
out = new OutEntry(ddname); // create/open a
new entry
outEntries.put(ddname, out); // save it in the map
out.writer.write(nextHeader, 0,
nextHeader.length); // write the saved header entry
}
out.count++; // increment record count for this entry
countField.putInt(out.count, record); // update
sequence in the record
out.writer.write(record, 0, bytesRead); // write
the record
}
}
}
finally // close all files
{
if (input != null)
input.close();
for (OutEntry entry : outEntries.values()) // each output
writer
{
entry.writer.close();
}
}
}
/**
* A class to keep a RecordWriter and count for each ddname
* best practice would be to encapsulate with getters/setters
* but we're trying to keep things simple for the sample
*/
private static class OutEntry
{
OutEntry(String ddname) throws IOException
{
// open a RecordWriter for the supplied ddname
writer = RecordWriter.newWriterForDD(ddname);
}
int count = 0;
RecordWriter writer;
}
}
--
Andrew Rowley
Black Hill Software
----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN