View Javadoc
1   //******************************************************************************
2   //
3   // File:    StreamFile.java
4   // Package: edu.rit.pj.io
5   // Unit:    Class edu.rit.pj.io.StreamFile
6   //
7   // This Java source file is copyright (C) 2006 by Alan Kaminsky. All rights
8   // reserved. For further information, contact the author, Alan Kaminsky, at
9   // ark@cs.rit.edu.
10  //
11  // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12  // software; you can redistribute it and/or modify it under the terms of the GNU
13  // General Public License as published by the Free Software Foundation; either
14  // version 3 of the License, or (at your option) any later version.
15  //
16  // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17  // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18  // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19  //
20  // Linking this library statically or dynamically with other modules is making a
21  // combined work based on this library. Thus, the terms and conditions of the GNU
22  // General Public License cover the whole combination.
23  //
24  // As a special exception, the copyright holders of this library give you
25  // permission to link this library with independent modules to produce an
26  // executable, regardless of the license terms of these independent modules, and
27  // to copy and distribute the resulting executable under terms of your choice,
28  // provided that you also meet, for each linked independent module, the terms
29  // and conditions of the license of that module. An independent module is a module
30  // which is not derived from or based on this library. If you modify this library,
31  // you may extend this exception to your version of the library, but you are not
32  // obligated to do so. If you do not wish to do so, delete this exception
33  // statement from your version.
34  //
35  // A copy of the GNU General Public License is provided in the file gpl.txt. You
36  // may also obtain a copy of the GNU General Public License on the World Wide
37  // Web at http://www.gnu.org/licenses/gpl.html.
38  //
39  //******************************************************************************
40  package edu.rit.pj.io;
41  
42  import java.io.File;
43  import java.io.FileInputStream;
44  import java.io.FileOutputStream;
45  import java.io.IOException;
46  import java.io.InputStream;
47  import java.io.OutputStream;
48  
49  import edu.rit.pj.cluster.JobBackend;
50  
51  /**
52   * Class StreamFile represents a file that resides in the user's account in the
53   * job frontend process of a PJ cluster parallel program. Operations are
54   * provided to open an input stream or an output stream to read or write the
55   * file in the frontend processor.
56   *
57   * @author Alan Kaminsky
58   * @version 06-Nov-2006
59   */
60  public class StreamFile {
61  
62  // Hidden data members.
63      private File myFile;
64  
65  // Exported constructors.
66      /**
67       * Construct a new stream file that refers to the given file in the frontend
68       * processor.
69       *
70       * @param theFile File.
71       * @exception NullPointerException (unchecked exception) Thrown if
72       * <code>theFile</code> is null.
73       */
74      public StreamFile(File theFile) {
75          if (theFile == null) {
76              throw new NullPointerException("StreamFile(): theFile is null");
77          }
78          myFile = theFile;
79      }
80  
81  // Exported operations.
82      /**
83       * Obtain the file in the frontend processor to which this stream file
84       * refers.
85       *
86       * @return File.
87       */
88      public File getFile() {
89          return myFile;
90      }
91  
92      /**
93       * Open an output stream for writing this stream file. If the file does not
94       * exist, it is created; if the file exists, it is overwritten.
95       * <P>
96       * When called from a job backend process in a cluster parallel program, the
97       * returned output stream communicates with the job frontend process to
98       * write the file in the frontend processor. Otherwise, the returned output
99       * stream is a normal file output stream to write the file directly.
100      * <P>
101      * <I>Note:</I> The returned output stream does not do any buffering. Each
102      * method call sends a message to and receives a message from the job
103      * frontend. Consider layering a BufferedOutputStream on top of the returned
104      * output stream.
105      *
106      * @return Output stream.
107      * @exception IOException Thrown if an I/O error occurred.
108      * @throws java.io.IOException if any.
109      */
110     public OutputStream getOutputStream()
111             throws IOException {
112         return getOutputStream(false);
113     }
114 
115     /**
116      * Open an output stream for writing this stream file. If the file does not
117      * exist, it is created; if the file exists and the <code>append</code> flag is
118      * false, the file is overwritten; if the file exists and the
119      * <code>append</code> flag is true, data is written after the end of the file.
120      * <P>
121      * When called from a job backend process in a cluster parallel program, the
122      * returned output stream communicates with the job frontend process to
123      * write the file in the frontend processor. Otherwise, the returned output
124      * stream is a normal file output stream to write the file directly.
125      * <P>
126      * <I>Note:</I> The returned output stream does not do any buffering. Each
127      * method call sends a message to and receives a message from the job
128      * frontend. Consider layering a BufferedOutputStream on top of the returned
129      * output stream.
130      *
131      * @param append True to append, false to overwrite.
132      * @return Output stream.
133      * @exception IOException Thrown if an I/O error occurred.
134      * @throws java.io.IOException if any.
135      */
136     public OutputStream getOutputStream(boolean append)
137             throws IOException {
138         JobBackend backend = JobBackend.getJobBackend();
139         if (backend == null) {
140             return new FileOutputStream(myFile, append);
141         } else {
142             return backend.getFileWriter().open(myFile, append);
143         }
144     }
145 
146     /**
147      * Open an input stream for reading this stream file.
148      * <P>
149      * When called from a job backend process in a cluster parallel program, the
150      * returned input stream communicates with the job frontend process to read
151      * the file in the frontend processor. Otherwise, the returned input stream
152      * is a normal file input stream to read the file directly.
153      * <P>
154      * <I>Note:</I> The returned input stream does not do any buffering. Each
155      * method call sends a message to and receives a message from the job
156      * frontend. Consider layering a BufferedInputStream on top of the returned
157      * input stream.
158      *
159      * @return Input stream.
160      * @exception IOException Thrown if an I/O error occurred.
161      * @throws java.io.IOException if any.
162      */
163     public InputStream getInputStream()
164             throws IOException {
165         JobBackend backend = JobBackend.getJobBackend();
166         if (backend == null) {
167             return new FileInputStream(myFile);
168         } else {
169             return backend.getFileReader().open(myFile);
170         }
171     }
172 
173     /**
174      * {@inheritDoc}
175      *
176      * Determine if this stream file is equal to the given object.
177      */
178     public boolean equals(Object obj) {
179         return (obj instanceof StreamFile)
180                 && (this.myFile.equals(((StreamFile) obj).myFile));
181     }
182 
183     /**
184      * Returns a hash code for this stream file.
185      *
186      * @return Hash code.
187      */
188     public int hashCode() {
189         return myFile.hashCode();
190     }
191 
192     /**
193      * Returns a string version of this stream file.
194      *
195      * @return String version.
196      */
197     public String toString() {
198         return myFile.toString();
199     }
200 
201 }