1 33 package org.jruby.util; 34 35 import java.io.EOFException ; 36 import java.io.IOException ; 37 import java.nio.channels.FileChannel ; 38 39 import org.jruby.Ruby; 40 41 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 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 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 { 91 if (!isOpen) { 92 throw new IOException ("not opened"); 93 } 94 } 95 96 protected void checkReadable() throws IOException , BadDescriptorException { 97 if (!isOpen) { 98 throw new BadDescriptorException(); 99 } 100 101 if (!modes.isReadable()) { 102 throw new IOException ("not opened for reading"); 103 } 104 } 105 106 protected void checkWriteable() throws IOException , BadDescriptorException { 107 checkWritable(); 108 } 109 110 protected void checkWritable() throws IOException , BadDescriptorException { 111 if (!isOpen) { 112 throw new BadDescriptorException(); 113 } 114 115 if (!modes.isWriteable()) { 116 throw new IOException ("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 , InvalidValueException { 137 checkPermissionsSubsetOf(subsetModes); 138 139 resetByModes(subsetModes); 140 } 141 142 public abstract ByteList gets(ByteList separatorString) throws IOException , BadDescriptorException, EOFException ; 143 public abstract ByteList getsEntireStream() throws IOException , BadDescriptorException, EOFException ; 144 145 149 public abstract ByteList read(int number) throws IOException , BadDescriptorException, EOFException ; 150 public abstract int write(ByteList string) throws IOException , BadDescriptorException; 151 152 public abstract int getc() throws IOException , BadDescriptorException, EOFException ; 153 public abstract void ungetc(int c); 154 public abstract void putc(int c) throws IOException , BadDescriptorException; 155 156 public abstract ByteList sysread(int number) throws IOException , BadDescriptorException, EOFException ; 157 public abstract int syswrite(ByteList buf) throws IOException , BadDescriptorException; 158 public abstract int syswrite(int ch) throws IOException , BadDescriptorException; 159 160 public abstract IOHandler cloneIOHandler() throws IOException , PipeException, InvalidValueException; 161 public abstract void close() throws IOException , BadDescriptorException; 162 public abstract void flush() throws IOException , BadDescriptorException; 163 168 public abstract void sync() throws IOException , BadDescriptorException; 169 176 public abstract boolean isEOF() throws IOException , BadDescriptorException; 177 178 183 public abstract int pid(); 184 185 194 public abstract long pos() throws IOException , PipeException; 195 196 protected abstract void resetByModes(IOModes newModes) throws IOException , InvalidValueException; 197 public abstract void rewind() throws IOException , PipeException, InvalidValueException; 198 199 205 public abstract void seek(long offset, int type) throws IOException , PipeException, InvalidValueException; 206 public abstract void truncate(long newLength) throws IOException , PipeException; 207 208 public class PipeException extends Exception { 209 private static final long serialVersionUID = 1L; 210 } 211 public class BadDescriptorException extends Exception { 212 private static final long serialVersionUID = 1L; 213 } 214 public class InvalidValueException extends Exception { 215 private static final long serialVersionUID = 1L; 216 } 217 } 218 | Popular Tags |