Changeset 11414
- Timestamp:
- 12/04/08 19:19:09 (15 years ago)
- Location:
- branches/open-external-format/src/org/armedbear/lisp
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/open-external-format/src/org/armedbear/lisp/FileStream.java
r11406 r11414 50 50 private final Pathname pathname; 51 51 private final int bytesPerUnit; 52 private InputStream inst;53 private OutputStream outst;54 private Reader reader;55 private Writer writer;56 52 57 53 public FileStream(Pathname pathname, String namestring, … … 114 110 bytesPerUnit = 1; 115 111 if (isInputStream) { 116 reader = racf.getReader();112 initAsCharacterInputStream(racf.getReader()); 117 113 } 118 114 if (isOutputStream) { 119 writer = racf.getWriter();115 initAsCharacterOutputStream(racf.getWriter()); 120 116 } 121 117 } else { … … 130 126 bytesPerUnit = width / 8; 131 127 if (isInputStream) { 132 in st = racf.getInputStream();128 initAsBinaryInputStream(racf.getInputStream()); 133 129 } 134 130 if (isOutputStream) { 135 outst = racf.getOutputStream();131 initAsBinaryOutputStream(racf.getOutputStream()); 136 132 } 137 133 } … … 163 159 { 164 160 return pathname; 165 }166 167 @Override168 public LispObject listen() throws ConditionThrowable169 {170 try {171 if (isInputStream) {172 return (racf.position() < racf.length()) ? T : NIL;173 } else {174 streamNotInputStream();175 }176 }177 catch (IOException e) {178 error(new StreamError(this, e));179 }180 // Not reached.181 return NIL;182 161 } 183 162 … … 210 189 } 211 190 212 // Returns -1 at end of file.213 @Override214 protected int _readChar() throws ConditionThrowable215 {216 try {217 int c = reader.read();218 if (eolStyle == EolStyle.CRLF) {219 if (c == '\r') {220 int c2 = reader.read();221 if (c2 == '\n') {222 ++lineNumber;223 return c2;224 } else {225 // '\r' was not followed by '\n'226 // we cannot depend on characters to contain 1 byte only227 // so we need to revert to the last known position.228 // The classical use case for unreadChar229 racf.unreadChar((char)c2);230 }231 }232 return c;233 } else if (c == eolChar) {234 ++lineNumber;235 return c;236 } else {237 return c;238 }239 }240 catch (NullPointerException e) {241 streamNotInputStream();242 }243 catch (IOException e) {244 error(new StreamError(this, e));245 }246 // Not reached.247 return -1;248 }249 250 191 @Override 251 192 protected void _unreadChar(int n) throws ConditionThrowable … … 263 204 { 264 205 return true; 265 }266 267 @Override268 public void _writeChar(char c) throws ConditionThrowable269 {270 try {271 if (c == '\n') {272 if (eolStyle == EolStyle.CRLF)273 writer.write('\r');274 writer.write(eolChar);275 charPos = 0;276 } else {277 writer.write(c);278 ++charPos;279 }280 }281 catch (IOException e) {282 error(new StreamError(this, e));283 }284 }285 286 @Override287 public void _writeChars(char[] chars, int start, int end)288 throws ConditionThrowable {289 _writeChars(chars, start, end, true);290 }291 292 public void _writeChars(char[] chars, int start, int end, boolean maintainCharPos)293 throws ConditionThrowable294 {295 try {296 if (eolStyle == EolStyle.LF) {297 /* we can do a little bit better in this special case */298 writer.write(chars, start, end);299 if (maintainCharPos) {300 int lastlfpos = -1;301 for (int i = start; i < end; i++) {302 if (chars[i] == '\n') {303 lastlfpos = i;304 }305 }306 if (lastlfpos == -1) {307 charPos += end - start;308 } else {309 charPos = end - lastlfpos;310 }311 }312 } else if (eolStyle == EolStyle.CRLF) {313 for (int i = start; i < end; i++) {314 char c = chars[i];315 if (c == '\n') {316 writer.write('\r');317 writer.write('\n');318 charPos = 0;319 } else {320 writer.write(c);321 ++charPos;322 }323 }324 } else {325 for (int i = start; i < end; i++) {326 char c = chars[i];327 if (c == '\n') {328 writer.write(eolChar);329 charPos = 0;330 } else {331 writer.write(c);332 ++charPos;333 }334 }335 }336 }337 catch (IOException e) {338 error(new StreamError(this, e));339 }340 }341 342 @Override343 public void _writeString(String s) throws ConditionThrowable344 {345 _writeChars(s.toCharArray(), 0, s.length(), true);346 }347 348 @Override349 public void _writeLine(String s) throws ConditionThrowable350 {351 _writeChars(s.toCharArray(), 0, s.length(), false);352 if (eolStyle == EolStyle.CRLF)353 _writeChar('\r');354 _writeChar(eolChar);355 charPos = 0;356 }357 358 // Reads an 8-bit byte.359 @Override360 public int _readByte() throws ConditionThrowable361 {362 try {363 return inst.read(); // Reads an 8-bit byte.364 }365 catch (NullPointerException e) {366 streamNotInputStream();367 }368 catch (IOException e) {369 error(new StreamError(this, e));370 }371 // Not reached.372 return -1;373 }374 375 // Writes an 8-bit byte.376 @Override377 public void _writeByte(int n) throws ConditionThrowable378 {379 try {380 outst.write(n); // Writes an 8-bit byte.381 }382 catch (NullPointerException e) {383 streamNotOutputStream();384 }385 catch (IOException e) {386 error(new StreamError(this, e));387 }388 206 } 389 207 -
branches/open-external-format/src/org/armedbear/lisp/Stream.java
r11411 r11414 44 44 import java.io.PrintWriter; 45 45 import java.io.PushbackReader; 46 import java.io.Reader; 46 47 import java.io.StringWriter; 47 48 import java.io.Writer; … … 67 68 68 69 // Character input. 69 private PushbackReader reader; 70 private Reader reader; 71 private PushbackReader pushbackReader; 70 72 protected int offset; 71 73 protected int lineNumber; … … 107 109 108 110 // Binary input. 109 private BufferedInputStream in;111 private InputStream in; 110 112 111 113 // Binary output. 112 private BufferedOutputStream out; 114 private OutputStream out; 115 116 // end of line character sequence. 117 private char[] eolseq; 118 119 private void setEolSeq() { 120 if (eolStyle == EolStyle.CRLF) { 121 eolseq = new char[] { '\r', '\n' }; 122 } else if (eolStyle == EolStyle.CR) { 123 eolseq = new char[] { '\r' }; 124 } else { 125 eolseq = new char[] { '\n' }; 126 } 127 } 113 128 114 129 protected Stream() 115 130 { 131 setEolSeq(); 116 132 } 117 133 118 134 public Stream(InputStream inputStream, LispObject elementType) 119 135 { 120 136 this(inputStream, elementType, null); 121 137 } 122 138 … … 128 144 if (elementType == Symbol.CHARACTER || elementType == Symbol.BASE_CHAR) 129 145 { 130 isCharacterStream = true;131 146 InputStreamReader inputStreamReader; 132 147 try … … 143 158 new InputStreamReader(inputStream); 144 159 } 145 reader = new PushbackReader(new BufferedReader(inputStreamReader), 146 2); 160 pushbackReader = new PushbackReader(new BufferedReader(inputStreamReader), 161 2); 162 initAsCharacterInputStream(pushbackReader); 147 163 } 148 164 else 149 165 { 150 166 isBinaryStream = true; 151 in = new BufferedInputStream(inputStream);152 } 153 isInputStream = true;154 isOutputStream = false;167 InputStream in = new BufferedInputStream(inputStream); 168 initAsBinaryInputStream(in); 169 } 170 setEolSeq(); 155 171 } 156 172 … … 163 179 public Stream(OutputStream outputStream, LispObject elementType) 164 180 { 165 181 this(outputStream, elementType, null); 166 182 } 167 183 … … 172 188 if (elementType == Symbol.CHARACTER || elementType == Symbol.BASE_CHAR) 173 189 { 174 isCharacterStream = true;190 Writer writer; 175 191 try 176 192 { … … 184 200 writer = new OutputStreamWriter(outputStream); 185 201 } 202 initAsCharacterOutputStream(writer); 186 203 } 187 204 else 188 205 { 189 isBinaryStream = true; 190 out = new BufferedOutputStream(outputStream); 191 } 192 isInputStream = false; 193 isOutputStream = true; 206 OutputStream out = new BufferedOutputStream(outputStream); 207 initAsBinaryOutputStream(out); 208 } 209 setEolSeq(); 194 210 } 195 211 … … 199 215 this(outputStream, elementType); 200 216 setInteractive(interactive); 217 } 218 219 protected void initAsCharacterInputStream(Reader reader) 220 { 221 this.reader = reader; 222 isInputStream = true; 223 isCharacterStream = true; 224 } 225 226 protected void initAsBinaryInputStream(InputStream in) { 227 this.in = in; 228 isInputStream = true; 229 isBinaryStream = true; 230 } 231 232 protected void initAsCharacterOutputStream(Writer writer) { 233 this.writer = writer; 234 isOutputStream = true; 235 isCharacterStream = true; 236 } 237 238 protected void initAsBinaryOutputStream(OutputStream out) { 239 this.out = out; 240 isOutputStream = true; 241 isBinaryStream = true; 201 242 } 202 243 … … 255 296 eolChar = (eolStyle == EolStyle.CR) ? '\r' : '\n'; 256 297 externalFormat = format; 257 298 setEolSeq(); 258 299 return; 259 300 } … … 1798 1839 try 1799 1840 { 1800 reader.unread(n);1841 pushbackReader.unread(n); 1801 1842 --offset; 1802 1843 if (n == eolChar) … … 1849 1890 { 1850 1891 if (c == '\n') { 1851 if (eolStyle == EolStyle.CRLF) 1852 writer.write('\r'); 1853 writer.write(eolChar); 1892 writer.write(eolseq); 1854 1893 writer.flush(); 1855 1894 charPos = 0; … … 1899 1938 index = i; 1900 1939 break; 1901 1902 1940 } 1941 } 1903 1942 if (index < 0) 1904 1943 { 1905 1944 // No newline. 1906 1945 charPos += (end - start); 1907 1946 } 1908 1947 else 1909 1948 { 1910 1949 charPos = end - (index + 1); 1911 1950 writer.flush(); 1912 1913 1951 } 1952 } 1914 1953 catch (NullPointerException e) 1915 1954 { … … 1935 1974 try 1936 1975 { 1937 for (int i = 0; i < s.length(); i++) 1938 //###FIXME: the number of writes can be greatly reduced by 1939 // writing the space between newlines as chunks. 1940 _writeChar(s.charAt(i)); 1976 _writeChars(s.toCharArray(), 0, s.length()); 1941 1977 } 1942 1978 catch (NullPointerException e)
Note: See TracChangeset
for help on using the changeset viewer.