1 32 package org.jruby.util; 33 34 import java.io.InputStream ; 35 import java.io.OutputStream ; 36 import java.io.IOException ; 37 import java.io.EOFException ; 38 39 import org.jruby.Ruby; 40 41 43 public abstract class IOHandlerJavaIO extends IOHandler { 44 private int ungotc = -1; 46 47 protected IOHandlerJavaIO(Ruby runtime) { 48 super(runtime); 49 } 50 51 public ByteList gets(ByteList separatorString) throws IOException , BadDescriptorException { 52 checkReadable(); 53 54 if (separatorString == null) { 55 return getsEntireStream(); 56 } 57 58 final ByteList separator = (separatorString == PARAGRAPH_DELIMETER) ? 59 ByteList.create("\n\n") : separatorString; 60 61 byte c = (byte)read(); 62 if (c == -1) { 63 return null; 64 } 65 66 ByteList buffer = new ByteList(); 67 68 LineLoop : while (true) { 69 while (c != separator.bytes[0] && c != -1) { 70 buffer.append(c); 71 c = (byte)read(); 72 } 73 for (int i = 0; i < separator.realSize; i++) { 74 if (c == -1) { 75 break LineLoop; 76 } else if (c != separator.bytes[i]) { 77 continue LineLoop; 78 } 79 buffer.append(c); 80 if (i < separator.realSize - 1) { 81 c = (byte)read(); 82 } 83 } 84 break; 85 } 86 87 if (separatorString == PARAGRAPH_DELIMETER) { 88 while (c == separator.bytes[0]) { 89 c = (byte)read(); 90 } 91 ungetc(c); 92 } 93 94 return buffer; 95 } 96 97 public ByteList getsEntireStream() throws IOException { 98 ByteList result = new ByteList(); 99 int c; 100 while ((c = (byte)read()) != -1) { 101 result.append(c); 102 } 103 104 if (result.realSize == 0) { 106 return null; 107 } 108 109 return result; 110 } 111 112 public int read() throws IOException { 113 try { 114 if (ungotc >= 0) { 115 int c = ungotc; 116 ungotc = -1; 117 return c; 118 } 119 120 return sysread(); 121 } catch (EOFException e) { 122 return -1; 123 } 124 } 125 126 public int getc() throws IOException , BadDescriptorException { 127 checkReadable(); 128 129 int c = read(); 130 131 if (c == -1) { 132 return c; 133 } 134 return c & 0xff; 135 } 136 137 public ByteList read(int number) throws IOException , BadDescriptorException { 138 try { 139 140 if (ungotc >= 0) { 141 ByteList buf2 = sysread(number - 1); 142 buf2.prepend((byte)ungotc); 143 ungotc = -1; 144 return buf2; 145 } 146 147 return sysread(number); 148 } catch (EOFException e) { 149 return null; 150 } 151 } 152 153 public void ungetc(int c) { 154 if (c >= 0) { 156 ungotc = c; 157 } 158 } 159 160 public void putc(int c) throws IOException , BadDescriptorException { 161 try { 162 syswrite(c); 163 } catch (IOException e) { 164 } 165 } 166 167 public int write(ByteList string) throws IOException , BadDescriptorException { 168 return syswrite(string); 169 } 170 171 protected int sysread(ByteList buf, int length) throws IOException { 172 if (buf == null) { 173 throw new IOException ("sysread2: Buf is null"); 174 } 175 176 int i = 0; 177 for (;i < length; i++) { 178 int c = sysread(); 179 180 if (c == -1) { 181 if (i <= 0) { 182 return -1; 183 } 184 break; 185 } 186 187 buf.append(c); 188 } 189 190 return i; 191 } 192 193 public ByteList sysread(int number) throws IOException , BadDescriptorException { 195 if (!isOpen()) { 196 throw new IOException ("File not open"); 197 } 198 checkReadable(); 199 200 ByteList buf = new ByteList(number); 201 int position = 0; 202 203 while (position < number) { 204 int s = sysread(buf, number - position); 205 206 if (s == -1) { 207 if (position <= 0) { 208 throw new EOFException (); 209 } 210 break; 211 } 212 213 position += s; 214 } 215 216 return buf; 217 } 218 219 public abstract int sysread() throws IOException ; 220 221 public abstract InputStream getInputStream(); 222 public abstract OutputStream getOutputStream(); 223 } 224 | Popular Tags |