1 8 9 package org.python.modules; 10 11 import java.io.*; 12 import java.util.*; 13 14 import org.python.core.*; 15 16 23 public class cStringIO { 24 28 public static StringIO StringIO() { 29 return new StringIO(); 30 } 31 32 37 public static StringIO StringIO(String buf) { 38 return new StringIO(buf); 39 } 40 41 42 47 public static class StringIO extends PyObject { 48 transient public boolean softspace = false; 49 transient public String name = "<cStringIO>"; 50 transient public String mode = "w"; 51 transient public boolean closed = false; 52 53 transient private char[] buf; 54 transient private int count; 55 transient private int pos; 56 57 58 StringIO() { 59 this.buf = new char[16]; 60 } 61 62 63 StringIO(String buf) { 64 this.buf = new char[buf.length() + 16]; 65 write(buf); 66 seek(0); 67 } 68 69 70 public void __setattr__(String name, PyObject value) { 71 if (name == "softspace") { 72 softspace = value.__nonzero__(); 73 return; 74 } 75 super.__setattr__(name, value); 76 } 77 78 public PyObject __iter__() { 79 return new PyCallIter(__getattr__("readline"), Py.newString("")); 80 } 81 82 85 public void close() { 86 buf = null; 87 closed = true; 88 } 89 90 91 95 public boolean isatty() { 96 return false; 97 } 98 99 100 104 public void seek(long pos) { 105 seek(pos, 0); 106 } 107 108 109 114 public void seek(long pos, int mode) { 115 if (mode == 1) 116 this.pos = (int)pos + this.pos; 117 else if (mode == 2) 118 this.pos = (int)pos + count; 119 this.pos = Math.max(0, (int)pos); 120 } 121 122 125 public void reset() { 126 pos = 0; 127 } 128 129 133 public long tell() { 134 return pos; 135 } 136 137 138 139 144 public String read() { 145 return read(-1); 146 } 147 148 149 157 public String read(int size) { 158 opencheck(); 159 int newpos = (size < 0) ? count : Math.min(pos+size, count); 160 String r = null; 161 if (size == 1 && newpos > pos) { 162 r = cStringIO.getString(buf[pos]); 163 } else { 164 r = new String (buf, pos, newpos-pos); 165 } 166 pos = newpos; 167 return r; 168 } 169 170 171 private int indexOf(char ch, int pos) { 172 for (int i = pos; i < count; i++) { 173 if (buf[i] == ch) 174 return i; 175 } 176 return -1; 177 } 178 179 180 187 public String readline() { 188 return readline(-1); 189 } 190 191 192 201 public String readline(int length) { 202 opencheck(); 203 int i = indexOf('\n', pos); 204 int newpos = (i < 0) ? count : i+1; 205 if (length != -1 && pos + length < newpos) 206 newpos = pos + length; 207 String r = new String (buf, pos, newpos-pos); 208 pos = newpos; 209 return r; 210 } 211 212 213 217 public String readlineNoNl() { 218 int i = indexOf('\n', pos); 219 int newpos = (i < 0) ? count : i; 220 String r = new String (buf, pos, newpos-pos); 221 pos = newpos; 222 if (pos < count) pos++; 224 return r; 225 } 226 227 228 229 234 public PyObject readlines() { 235 return readlines(0); 236 } 237 238 239 244 public PyObject readlines(int sizehint) { 245 opencheck(); 246 int total = 0; 247 PyList lines = new PyList(); 248 String line = readline(); 249 while (line.length() > 0) { 250 lines.append(new PyString(line)); 251 total += line.length(); 252 if (0 < sizehint && sizehint <= total) 253 break; 254 line = readline(); 255 } 256 return lines; 257 } 258 259 262 public void truncate() { 263 truncate(-1); 264 } 265 266 269 public void truncate(int pos) { 270 opencheck(); 271 if (pos < 0) 272 pos = this.pos; 273 if (count > pos) 274 count = pos; 275 } 276 277 278 private void expandCapacity(int newLength) { 279 int newCapacity = (buf.length + 1) * 2; 280 if (newLength > newCapacity) { 281 newCapacity = newLength; 282 } 283 284 char newBuf[] = new char[newCapacity]; 285 System.arraycopy(buf, 0, newBuf, 0, count); 286 buf = newBuf; 287 } 289 290 291 295 public void write(String s) { 296 opencheck(); 297 int newpos = pos + s.length(); 298 299 if (newpos >= buf.length) 300 expandCapacity(newpos); 301 if (newpos > count) 302 count = newpos; 303 304 s.getChars(0, s.length(), buf, pos); 305 pos = newpos; 306 } 307 308 309 313 public void writeChar(char ch) { 314 if (pos+1 >= buf.length) 315 expandCapacity(pos+1); 316 buf[pos++] = ch; 317 if (pos > count) 318 count = pos; 319 } 320 321 322 325 public void writelines(String [] lines) { 326 for (int i = 0; i < lines.length; i++) { 327 write(lines[i]); 328 } 329 } 330 331 332 335 public void flush() { 336 opencheck(); 337 } 338 339 340 345 public String getvalue() { 346 opencheck(); 347 return new String (buf, 0, count); 348 } 349 350 351 private final void opencheck() { 352 if (buf == null) 353 throw Py.ValueError("I/O operation on closed file"); 354 } 355 } 356 357 358 private static String [] strings = new String [256]; 359 static String getString(char ch) { 360 if ((int)ch > 255) { 361 return new String (new char[] { ch }); 362 } 363 364 String s = strings[(int)ch]; 365 366 if (s == null) { 367 s = new String (new char[] { ch }); 368 strings[(int)ch] = s; 369 } 370 return s; 371 } 372 } 373 | Popular Tags |