Changeset 11431
- Timestamp:
- 12/07/08 07:39:54 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/open-external-format/src/org/armedbear/lisp/util/RandomAccessCharacterFile.java
r11407 r11431 36 36 37 37 import java.io.IOException; 38 import java.io. InputStream;38 import java.io.PushbackInputStream; 39 39 import java.io.OutputStream; 40 40 import java.io.RandomAccessFile; 41 import java.io.PushbackReader; 41 42 import java.io.Reader; 43 import java.io.StringReader; 42 44 import java.io.Writer; 43 45 import java.nio.ByteBuffer; … … 51 53 public class RandomAccessCharacterFile { 52 54 53 private class RandomAccessInputStream extends InputStream { 54 55 private RandomAccessInputStream() { 56 } 57 58 private byte[] buf = new byte[1]; 59 55 private class RandomAccessInputStream extends PushbackInputStream { 56 57 public RandomAccessInputStream() { 58 super(null); 59 } 60 61 private byte[] read_buf = new byte[1]; 62 63 @Override 60 64 public int read() throws IOException { 61 int len = read( buf);65 int len = read(read_buf); 62 66 if (len == 1) { 63 67 // byte is signed, char is unsigned, int is signed. 64 68 // buf can hold 0xff, we want it as 0xff in int, not -1. 65 return 0xff & (int) buf[0];69 return 0xff & (int) read_buf[0]; 66 70 } else { 67 71 return -1; … … 73 77 return RandomAccessCharacterFile.this.read(b, off, len); 74 78 } 79 80 @Override 81 public void unread(int b) throws IOException { 82 RandomAccessCharacterFile.this.unreadByte((byte)b); 83 } 84 85 @Override 86 public void unread(byte[] b, int off, int len) throws IOException { 87 for (int i = 0; i < len; i++) 88 this.unread(b[off+i]); 89 } 90 91 @Override 92 public void unread(byte[] b) throws IOException { 93 this.unread(b, 0, b.length); 94 } 95 96 @Override 97 public int available() throws IOException { 98 return (int)(RandomAccessCharacterFile.this.length() 99 - RandomAccessCharacterFile.this.position()); 100 } 101 102 @Override 103 public synchronized void mark(int readlimit) { 104 } 105 106 @Override 107 public boolean markSupported() { 108 return false; 109 } 110 111 @Override 112 public synchronized void reset() throws IOException { 113 throw new IOException("Operation not supported"); 114 } 115 116 @Override 117 public long skip(long n) throws IOException { 118 RandomAccessCharacterFile.this.position(RandomAccessCharacterFile.this.position()+n); 119 return n; 120 } 121 122 @Override 123 public int read(byte[] b) throws IOException { 124 return this.read(b, 0, b.length); 125 } 75 126 76 127 @Override … … 106 157 } 107 158 } 108 109 private class RandomAccessReader extends Reader { 159 160 // dummy reader which we need to call the Pushback constructor 161 // because a null value won't work 162 private static Reader staticReader = new StringReader(""); 163 164 private class RandomAccessReader extends PushbackReader { 110 165 111 166 private RandomAccessReader() { 112 } 113 167 // because we override all methods of Pushbackreader, 168 // staticReader will never be referenced 169 super(staticReader); 170 } 171 172 @Override 114 173 public void close() throws IOException { 115 174 RandomAccessCharacterFile.this.close(); 116 175 } 117 118 @Override 119 public int read(char[] cb, int off, int len) throws IOException { 176 177 private char[] read_buf = new char[1]; 178 179 @Override 180 public int read() throws IOException { 181 int n = this.read(read_buf); 182 183 if (n == 1) 184 return read_buf[0]; 185 else 186 return -1; 187 } 188 189 @Override 190 public void unread(int c) throws IOException { 191 RandomAccessCharacterFile.this.unreadChar((char)c); 192 } 193 194 @Override 195 public void unread(char[] cbuf, int off, int len) throws IOException { 196 for (int i = 0; i < len; i++) 197 this.unread(cbuf[off+i]); 198 } 199 200 @Override 201 public void unread(char[] cbuf) throws IOException { 202 this.unread(cbuf, 0, cbuf.length); 203 } 204 205 @Override 206 public int read(CharBuffer target) throws IOException { 207 //FIXME: to be implemented 208 throw new IOException("Not implemented"); 209 } 210 211 @Override 212 public int read(char[] cbuf) throws IOException { 213 return RandomAccessCharacterFile.this.read(cbuf, 0, cbuf.length); 214 } 215 216 217 218 @Override 219 public int read(char[] cb, int off, int len) throws IOException { 120 220 return RandomAccessCharacterFile.this.read(cb, off, len); 121 221 } … … 199 299 } 200 300 201 public Reader getReader() {301 public PushbackReader getReader() { 202 302 return reader; 203 303 } 204 304 205 public InputStream getInputStream() {305 public PushbackInputStream getInputStream() { 206 306 return inputStream; 207 307 }
Note: See TracChangeset
for help on using the changeset viewer.