This discussion is archived
6 Replies Latest reply: Jul 4, 2011 3:38 PM by 830726 RSS

i might be breaking the javadoc specification for java.io.InputStream

830726 Newbie
Currently Being Moderated
*[note]*: This is not intended to be an fully functioning class yet. Step-by-step I am learning how to extend an existing class and create one that might be useful someday. Since "close()" is the simplest method in InputStream, I am starting here.

This InputStream makes an arbitrary number of files in one directory look like one big file. I can keep reading until the last file to be read EOF is reached.
public class DirectoryInputStream extends InputStream {
  private boolean moreFiles = true;
  private int inputFileNmb = 0;
  private File[] files;
  private InputStream inStrm;
  private InputStream[] holdOntoOpenStreams;

  public DirectoryInputStream(File dataSrc) throws Exception {
    files = getFilesInDir(dataSrc);
    // holdOntoOpenStream = new FileInputStream[files.length]; <-- towards maintaining the integrity of "close()"
    chngFile();
  }

  private void chngFile() throws IOException {
    if(inputFileNmb != 0) inStrm.close();  // <-- violates integrity of "close()". Don't do this if the javadoc for close() is to be followed
    inStrm = new FileInputStream(files[inputFileNmb]);
    // holdOntoOpenStreams[inputFileNmb] = inStrm; <-- could be used to hold onto open InputStreams until close() is called

    inputFileNmb++;
    if(inputFileNmb == files.length) moreFiles = false;
  }
    
  public int read() throws IOException {
    // code removed that deals with returning System.getProperty("line.separator") if the EOF of a file is reached and there are other files still to be read
    int c = inStrm.read();
        
    if((c == -1) && !moreFiles) {
      inStrm.close();  // <-- violates integrity of "close()" ?
      return -1;
    } 

    if((c == -1) && moreFiles) {
        // ...deal with transitioning inStrm to connect to a different file. "chngFile()" is invoked in the process, etc.
        c = inStrem.read();
    }
    return c;
  }

  // following the API specifications, this is what an overridden "close()" should look like?
  public void close() throws IOException {
    inStrm.close();
    for(int i = 0; i < (inputFileNmb - 1); i++) {
      holdOntoOpenStreams.close();
}
}
}
Internalizing the closing of streams into this class seems like a feature.
Yet, As per the API "close()":
*Closes this input stream and releases any system resources associated with the stream.*
Certainly, I am not doing that.

My opinion is that I am violating the java.io.InputStream specification,
In OOP, you should never do this.
But, this is an exception because the resources associated with open FileInputStream objects are too limited.

--thank you

Edited by: triym on Jul 2, 2011 7:48 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points