KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > util > IOHandler


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
15  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
16  * Copyright (C) 2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
17  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
18  * Copyright (C) 2005 Charles O Nutter <headius@headius.com>
19  * Copyright (C) 2006 Evan Buswell <evan@heron.sytes.net>
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * either of the GNU General Public License Version 2 or later (the "GPL"),
23  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24  * in which case the provisions of the GPL or the LGPL are applicable instead
25  * of those above. If you wish to allow use of your version of this file only
26  * under the terms of either the GPL or the LGPL, and not to allow others to
27  * use your version of this file under the terms of the CPL, indicate your
28  * decision by deleting the provisions above and replace them with the notice
29  * and other provisions required by the GPL or the LGPL. If you do not delete
30  * the provisions above, a recipient may use your version of this file under
31  * the terms of any one of the CPL, the GPL or the LGPL.
32  ***** END LICENSE BLOCK *****/

33 package org.jruby.util;
34
35 import java.io.EOFException JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.nio.channels.FileChannel JavaDoc;
38
39 import org.jruby.Ruby;
40
41 /**
42  */

43 public abstract class IOHandler {
44     public static final int SEEK_SET = 0;
45     public static final int SEEK_CUR = 1;
46     public static final int SEEK_END = 2;
47     
48     // We use a highly uncommon string to represent the paragraph delimeter.
49
// The 100% solution is not really worth the extra code.
50
// CON: I used getBytes here to avoid the exception-handling hassle and because
51
// these bytes should be the same in almost all encodings.
52
public static final ByteList PARAGRAPH_DELIMETER = ByteList.create("PARAGRPH_DELIM_MRK_ER");
53     
54     private Ruby runtime;
55     protected IOModes modes;
56     protected int fileno;
57     protected boolean isOpen = false;
58     protected boolean isSync = false;
59     
60     protected IOHandler(Ruby runtime) {
61         this.runtime = runtime;
62     }
63
64     public int getFileno() {
65         return fileno;
66     }
67     
68     public void setFileno(int fileno) {
69         this.fileno = fileno;
70     }
71
72     protected Ruby getRuntime() {
73         return runtime;
74     }
75     
76     public abstract FileChannel JavaDoc getFileChannel();
77     
78     public boolean isReadable() {
79         return modes.isReadable();
80     }
81
82     public boolean isOpen() {
83         return isOpen;
84     }
85
86     public boolean isWriteable() {
87         return modes.isWriteable();
88     }
89
90     protected void checkOpen() throws IOException JavaDoc {
91         if (!isOpen) {
92             throw new IOException JavaDoc("not opened");
93         }
94     }
95     
96     protected void checkReadable() throws IOException JavaDoc, BadDescriptorException {
97         if (!isOpen) {
98             throw new BadDescriptorException();
99         }
100         
101         if (!modes.isReadable()) {
102             throw new IOException JavaDoc("not opened for reading");
103         }
104     }
105
106     protected void checkWriteable() throws IOException JavaDoc, BadDescriptorException {
107     checkWritable();
108     }
109
110     protected void checkWritable() throws IOException JavaDoc, BadDescriptorException {
111         if (!isOpen) {
112             throw new BadDescriptorException();
113         }
114         
115         if (!modes.isWriteable()) {
116             throw new IOException JavaDoc("not opened for writing");
117         }
118     }
119
120     public void checkPermissionsSubsetOf(IOModes subsetModes) {
121         subsetModes.checkSubsetOf(modes);
122     }
123     
124     public IOModes getModes() {
125         return modes;
126     }
127     
128     public boolean isSync() {
129         return isSync;
130     }
131
132     public void setIsSync(boolean isSync) {
133         this.isSync = isSync;
134     }
135
136     public void reset(IOModes subsetModes) throws IOException JavaDoc, InvalidValueException {
137         checkPermissionsSubsetOf(subsetModes);
138         
139         resetByModes(subsetModes);
140     }
141
142     public abstract ByteList gets(ByteList separatorString) throws IOException JavaDoc, BadDescriptorException, EOFException JavaDoc;
143     public abstract ByteList getsEntireStream() throws IOException JavaDoc, BadDescriptorException, EOFException JavaDoc;
144
145     // TODO: We overflow on large files...We could increase to long to limit
146
// this, but then the impl gets more involved since java io APIs based on
147
// int (means we have to chunk up a long into a series of int ops).
148

149     public abstract ByteList read(int number) throws IOException JavaDoc, BadDescriptorException, EOFException JavaDoc;
150     public abstract int write(ByteList string) throws IOException JavaDoc, BadDescriptorException;
151
152     public abstract int getc() throws IOException JavaDoc, BadDescriptorException, EOFException JavaDoc;
153     public abstract void ungetc(int c);
154     public abstract void putc(int c) throws IOException JavaDoc, BadDescriptorException;
155     
156     public abstract ByteList sysread(int number) throws IOException JavaDoc, BadDescriptorException, EOFException JavaDoc;
157     public abstract int syswrite(ByteList buf) throws IOException JavaDoc, BadDescriptorException;
158     public abstract int syswrite(int ch) throws IOException JavaDoc, BadDescriptorException;
159     
160     public abstract IOHandler cloneIOHandler() throws IOException JavaDoc, PipeException, InvalidValueException;
161     public abstract void close() throws IOException JavaDoc, BadDescriptorException;
162     public abstract void flush() throws IOException JavaDoc, BadDescriptorException;
163     /**
164      * <p>Flush and sync all writes to the filesystem.</p>
165      *
166      * @throws IOException if the sync does not work
167      */

168     public abstract void sync() throws IOException JavaDoc, BadDescriptorException;
169     /**
170      * <p>Return true when at end of file (EOF).</p>
171      *
172      * @return true if at EOF; false otherwise
173      * @throws IOException
174      * @throws BadDescriptorException
175      */

176     public abstract boolean isEOF() throws IOException JavaDoc, BadDescriptorException;
177     
178     /**
179      * <p>Get the process ID associated with this handler.</p>
180      *
181      * @return the pid if the IOHandler represents a process; otherwise -1
182      */

183     public abstract int pid();
184     
185     /**
186      * <p>Get the current position within the file associated with this
187      * handler.</p>
188      *
189      * @return the current position in the file.
190      * @throws IOException
191      * @throws PipeException ESPIPE (illegal seek) when not a file
192      *
193      */

194     public abstract long pos() throws IOException JavaDoc, PipeException;
195     
196     protected abstract void resetByModes(IOModes newModes) throws IOException JavaDoc, InvalidValueException;
197     public abstract void rewind() throws IOException JavaDoc, PipeException, InvalidValueException;
198     
199     /**
200      * <p>Perform a seek based on pos(). </p>
201      * @throws IOException
202      * @throws PipeException
203      * @throws InvalidValueException
204      */

205     public abstract void seek(long offset, int type) throws IOException JavaDoc, PipeException, InvalidValueException;
206     public abstract void truncate(long newLength) throws IOException JavaDoc, PipeException;
207     
208     public class PipeException extends Exception JavaDoc {
209         private static final long serialVersionUID = 1L;
210     }
211     public class BadDescriptorException extends Exception JavaDoc {
212         private static final long serialVersionUID = 1L;
213     }
214     public class InvalidValueException extends Exception JavaDoc {
215         private static final long serialVersionUID = 1L;
216     }
217 }
218
Popular Tags