Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5026575

RandomAccessFile.ReadLine() does not support line breaks in z/OS

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs
    • generic
    • generic

      Name: dk106046 Date: 04/05/2004

      Problem:

      This is with regard to using RandomAccessFile.readLine() method to read an EBCDIC file on the z/OS USS platform.

      The line break is defined as "\n" or "\r" in the readLine() method. As the RandomAccessFile.readLine() method does not do any conversion from EBCDIC to ASCII, the linebreak does not match. i.e EBCDIC linebreak does not match with "\n" or "\r" , hence there is continuous output without any linebreaks in an application where RandomAccessFile.ReadLine() is called in a loop until EOF is reached. Also, it is necessary to convert the data back to EBCDIC strings to print.

      Example testcase:

      import java.io.RandomAccessFile;
      import java.io.IOException;
      import java.io.FileNotFoundException;
                                                 
      public class TestRandomAccessFile {
                                                 
       public static void main(String[] args) {
                                                                                  
            String filename;
                                                                             
            // File name is the first command line argument
             if (args.length >= 1)
                filename = args[0];
             else
                filename = "test.in";
             try {
               RandomAccessFile rafile = new RandomAccessFile(filename, "r");
                                                                             
               String line;
               do {
                  line = rafile.readLine();
                  if (line != null) {
                 
                    System.out.println("Line read from file: " + line);
                            }
                             } while (line !=null);
                }
              catch (FileNotFoundException e) {
                   System.out.println("Could not find file " + filename);
                }
              catch (IOException e) {
                  System.out.println("Could not read file " + filename);
                }
           }
       }

      Analysis:

      RandomAccessFile should not have a readLine() method, as it can't work. The reason for this is that RandomAccessFile is a class to read/write raw data to/from a file (it is similar to a File class with read and write methods), but the readLine() method needs to interpret the data as text which requires character converters to be used based on a specified codepage.

      readLine could be fixed to use the default character convertor (as determined by file.encoding) but this doesn't really make sense as:
      a) The input file data may actually be ASCII or EBCDIC encoded.
      b) RandomAccessFile is for accessing raw data from the file, so any character convertor code may confuse users who are expecting this behavior.
      c) InputStreamReaders and OutputStreamReaders should be used to interpret file data as text,that's what they're designed to do.
      d) For other classes that implement DataInput (which includes readLine()) readLine() has been deprecated for the above reason. See DataInputStream and ObjectInputStream descriptions.

      Solution:

      Deprecate RandomAccessFile.readLine() for the future, as it makes no sense on z/OS.


      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            dkorbel David Korbel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: