com.ibm.jzos

Class RecordWriter

  • java.lang.Object
    • com.ibm.jzos.RecordWriter
  • All Implemented Interfaces:
    ZFileConstants


    public abstract class RecordWriter
    extends java.lang.Object
    implements ZFileConstants
    This class provides record mode write access to z/OS datasets. Factory methods newWriterForDD and newWriter(name, flags) may be used to return an instance of an implementation subtype of this class. To create a new dataset, clients of this class must first allocate a DD statement using ZFile.bpxwdyn(String); all attributes (RECFM, LRECL, SPACE, etc) should be specified in the allocation, as this API will not necessarily use defaults provided by ZFile and the IBM C library.

    In order to provide more efficient dataset I/O performance, subtype implementations may use native BSAM I/O and accumulate records in Java into blocks which are written to the underlying dataset as they are filled (or until flush is called). In addition, native calls to underlying I/O routines may be buffered and use overlapped I/O techniques if available to improve throughput and to reduce CPU overhead. Refer to z/OS DFSMS Using Data Sets, SC26-7410 for more information.

    This class does not support writing to datasets which aren't supported by BSAM. The only public API to this class and its non-public implementation subclasses is via one of the static factory methods in this class; the implementation subclass instance returned is an implementation detail and is not controllable by the client, and may change in the future.

    All sequential dataset organizations and record formats supported by ZFile (the IBM z/OS C library) are supported. For DSORG=PS datasets, best performance will be realized when using blocked records and large blocking factors.

    Instances of ZFileException are thrown to report errors, but detailed codes and message text in these exceptions may reflect either BSAM access method errors or ZFile (C-library access method interface) errors.

    Example: Create a new dataset and write to it using a RecordWriter

    
     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
       } 
     }
     

    Example: Write to an existing dataset

    
     RecordWriter writer = null;
     try {
       writer = RecordWriter.newWriter("//my.dataset", ZFileConstants.FLAG_DISP_OLD);
       byte[] recordBuf = new byte[writer.getLrecl()];
       int bytesToWrite;
       while ((bytesToWrite = getNextAppRecord(recordBuf)) > 0) {
         writer.write(recordBuf, 0, bytesToWrite);
       }
     } finally {
       if (writer != null) {
          writer.close(); 
       }
     }
     

    There are two System properties that may be used to control the usage of BSAM implementation classes:

    1. jzos.bsam.allow.abends - if this is set to true, then ABENDs in the BSAM support will not be caught and thrown as ZFileExceptions. IBM Support may request this setting in order to get a dump.
    2. jzos.bsam.disable - if this is set to true, then ZFile implementations will be used in all cases rather than direct BSAM implementation classes.
    Since:
    2.4.1
    • Method Detail

      • newWriterForDD

        public static RecordWriter newWriterForDD(java.lang.String ddname)
                                           throws ZFileException
        Construct a new RecordWriter on the given DD.

        The data definition must be 1-8 characters with some rules.

        • Is 1 through 8 alphanumeric or national ($, #, @) characters.
        • The first character must be alphabetic or national ($, #, @).
        Parameters:
        ddname - the previously allocated DD name that is associated with the dataset or file
        Throws:
        ZFileException - if there was an error opening the dataset.
      • newWriter

        public static RecordWriter newWriter(java.lang.String name,
                                             int flags)
                                      throws ZFileException,
                                             RcException
        Construct a new RecordWriter for an existing z/OS data set.
        Parameters:
        name - the name of the file to open, in the same syntax as ZFile.ZFile(String, String), the same syntax defined by the C-Library fopen() function. If the name specifies a DD:DDNAME, then refer to newWriterForDD(String) for supported data definition syntax
        flags -
        • If the name specifies a DD:DDNAME, then flags must be 0.
        • If FLAG_DISP_SHR and the name refers to a dataset name, the dataset will be allocated with DISP=SHR. This option should be used with caution, since there will be no serialization of data access.
        • If FLAG_DISP_OLD or 0 and name refers to a dataset name, it will be allocated with DISP=OLD.
        • If FLAG_DISP_MOD and name refers to a dataset name, it will be allocated with DISP=MOD.
        Throws:
        ZFileException - if the file could not be opened.
        RcException - if an error occurs when attempting to allocate the dataset, the exception from ZFile.bpxwdyn(String) is thrown.
        Since:
        2.4.2
      • write

        public abstract void write(byte[] buf)
                            throws ZFileException
        Write a record from the buffer to the native file.
        Parameters:
        buf - the byte array representing the record to be written
        Throws:
        ZFileException - if the native call fails
      • write

        public abstract void write(byte[] buf,
                                   int offset,
                                   int len)
                            throws ZFileException
        Write a record from the buffer to the native file.
        Parameters:
        buf - the byte array representing the record to be written
        offset - the offset, inclusive, in buf to start writing bytes
        len - the number of bytes to write
        Throws:
        ZFileException - if the native call fails
      • close

        public abstract void close()
                            throws ZFileException
        Close the reader and underlying native file.

        This will also free the associated DD if getAutoFree() is true.

        Note: close() must be issued by the same thread as the factory method that creates the instance object (opening the dataset). Writing can occur on any thread.

        Throws:
        ZFileException - if the native call fails
      • flush

        public abstract void flush()
                            throws ZFileException
        Flush any outstanding writes on the native file.
        Throws:
        ZFileException - if the native call fails
      • getLrecl

        public abstract int getLrecl()
        Answer the LRECL, which is the maximum record length for variable length files.
      • getBlksize

        public abstract int getBlksize()
        Answer the BLKSIZE, which is the maximum block length supported by the dataset.
      • getRecfmBits

        public abstract int getRecfmBits()
        Answer the RECFM bits
        See Also:
        ZFile.getRecfmBits()
      • getRecfm

        public abstract java.lang.String getRecfm()
        Get the native file's record format.
        See Also:
        ZFile.getRecfm()
      • getDDName

        public abstract java.lang.String getDDName()
        Answers the DD name used to open the underlying file.
        Returns:
        String ddname
      • getDsn

        public abstract java.lang.String getDsn()
        Answers the native dataset's absolute name, or name(member) if the native dataset is a member of a PDS.
        Returns:
        String dataset name
      • getAutoFree

        public boolean getAutoFree()
        Answer a flag that indicates whether the DD name associated with this file will automatically be freed when the file is closed. By default, this is true if this RecordWriter was created using newWriter() supplying a dataset name.
        Returns:
        boolean
      • setAutoFree

        public void setAutoFree(boolean autoFree)
        Sets a flag that will cause the DD to automatically be freed after close().
        See Also:
        getAutoFree()
� Copyright IBM Corporation 2005, 2022.