| 1 package org.python.core; 3 4 import java.io.*; 5 6 13 16 17 public class PyFile extends PyObject 18 { 19 20 private static class FileWrapper { 21 protected boolean reading; 22 protected boolean writing; 23 protected boolean binary; 24 25 void setMode(String mode) { 26 reading = mode.indexOf('r') >= 0; 27 writing = mode.indexOf('w') >= 0 || mode.indexOf("+") >= 0 || 28 mode.indexOf('a') >= 0; 29 binary = mode.indexOf('b') >= 0; 30 } 31 public String read(int n) throws java.io.IOException { 32 throw new java.io.IOException ("file not open for reading"); 33 } 34 public int read() throws java.io.IOException { 35 throw new java.io.IOException ("file not open for reading"); 36 } 37 public int available() throws java.io.IOException { 38 throw new java.io.IOException ("file not open for reading"); 39 } 40 public void unread(int c) throws java.io.IOException { 41 throw new java.io.IOException ("file doesn't support unread"); 42 } 43 public void write(String s) throws java.io.IOException { 44 throw new java.io.IOException ("file not open for writing"); 45 } 46 public long tell() throws java.io.IOException { 47 throw new java.io.IOException ("file doesn't support tell/seek"); 48 } 49 public void seek(long pos, int how) throws java.io.IOException { 50 throw new java.io.IOException ("file doesn't support tell/seek"); 51 } 52 public void flush() throws java.io.IOException { 53 } 54 public void close() throws java.io.IOException { 55 } 56 public void truncate(long position) throws java.io.IOException { 57 throw new java.io.IOException ("file doesn't support truncate"); 58 } 59 60 public Object __tojava__(Class cls) throws IOException { 61 return null; 62 } 63 protected byte[] getBytes(String s) { 64 if (binary) { 67 byte[] buf = new byte[s.length()]; 68 s.getBytes(0, s.length(), buf, 0); 69 return buf; 70 } else 71 return s.getBytes(); 72 } 73 protected String getString(byte[] buf, int offset, int len) { 74 if (binary) { 77 return new String (buf, 0, offset, len); 78 } else 79 return new String (buf, offset, len); 80 } 81 } 82 83 private static class InputStreamWrapper extends FileWrapper { 84 java.io.InputStream istream; 85 86 public InputStreamWrapper(java.io.InputStream s) { 87 istream = s; 88 } 89 90 public String read(int n) throws java.io.IOException { 91 if (n == 0) 92 return ""; 94 if (n < 0) { 95 byte buf[] = new byte[1024]; 97 StringBuffer sbuf = new StringBuffer (); 98 for (int read=0; read >= 0; read=istream.read(buf)) 99 sbuf.append(getString(buf, 0, read)); 100 return sbuf.toString(); 101 } 102 byte buf[] = new byte[n]; 108 int read = istream.read(buf); 109 if (read < 0) 110 return ""; 112 return new String (buf, 0, 0, read); 113 } 114 115 public int read() throws java.io.IOException { 116 return istream.read(); 117 } 118 119 public int available() throws java.io.IOException { 120 return istream.available(); 121 } 122 123 public void unread(int c) throws java.io.IOException { 124 ((java.io.PushbackInputStream )istream).unread(c); 125 } 126 127 public void close() throws java.io.IOException { 128 istream.close(); 129 } 130 131 public Object __tojava__(Class cls) throws IOException { 132 if (InputStream.class.isAssignableFrom(cls)) 133 return istream; 134 return null; 135 } 136 } 137 138 private static class OutputStreamWrapper extends FileWrapper { 139 private java.io.OutputStream ostream; 140 141 public OutputStreamWrapper(java.io.OutputStream s) { 142 ostream = s; 143 } 144 145 private static final int MAX_WRITE = 30000; 146 147 public void write(String s) throws java.io.IOException { 148 byte[] bytes = getBytes(s); 149 int n = bytes.length; 150 int i = 0; 151 while (i < n) { 152 int sz = n-i; 153 sz = sz > MAX_WRITE ? MAX_WRITE : sz; 154 ostream.write(bytes, i, sz); 155 i += sz; 156 } 157 } 158 159 public void flush() throws java.io.IOException { 160 ostream.flush(); 161 } 162 163 public void close() throws java.io.IOException { 164 ostream.close(); 165 } 166 167 public Object __tojava__(Class cls) throws IOException { 168 if (OutputStream.class.isAssignableFrom(cls)) 169 return ostream; 170 return null; 171 } 172 } 173 174 private static class IOStreamWrapper extends InputStreamWrapper { 175 private java.io.OutputStream ostream; 176 177 public IOStreamWrapper(java.io.InputStream istream, 178 java.io.OutputStream ostream) { 179 super(istream); 180 this.ostream = ostream; 181 } 182 183 public void write(String s) throws java.io.IOException { 184 ostream.write(getBytes(s)); 185 } 186 187 public void flush() throws java.io.IOException { 188 ostream.flush(); 189 } 190 191 public void close() throws java.io.IOException { 192 ostream.close(); 193 istream.close(); 194 } 195 196 public Object __tojava__(Class cls) throws IOException { 197 if (OutputStream.class.isAssignableFrom(cls)) 198 return ostream; 199 return super.__tojava__(cls); 200 } 201 } 202 203 private static class WriterWrapper extends FileWrapper { 204 private java.io.Writer writer; 205 206 public WriterWrapper(java.io.Writer s) { 207 writer = s; 208 } 209 210 212 public void write(String s) throws java.io.IOException { 213 writer.write(s); 214 } 215 216 public void flush() throws java.io.IOException { 217 writer.flush(); 218 } 219 220 public void close() throws java.io.IOException { 221 writer.close(); 222 } 223 } 224 225 private static class RFileWrapper extends FileWrapper { 226 227 protected static final int defaultBufferSize = 4096; 228 229 230 protected java.io.RandomAccessFile file; 231 232 234 protected long filePosition; 235 236 237 protected byte buffer[]; 238 239 241 protected long bufferStart; 242 243 247 protected long dataEnd; 248 249 251 protected int dataSize; 252 253 254 protected boolean endOfFile; 255 256 257 boolean bufferModified = false; 258 259 public RFileWrapper(java.io.RandomAccessFile file) { 260 this(file, 8092); 261 } 262 263 public RFileWrapper(java.io.RandomAccessFile file, int bufferSize) { 264 this.file = file; 265 bufferStart = 0; 266 dataEnd = 0; 267 dataSize = 0; 268 filePosition = 0; 269 buffer = new byte[bufferSize]; 270 endOfFile = false; 271 } 272 273 public String read(int n) throws java.io.IOException { 274 if (n < 0) { 275 n = (int)(file.length() - filePosition); 276 if (n < 0) 277 n = 0; 278 } 279 byte[] buf = new byte[n]; 280 n = readBytes(buf, 0, n); 281 if (n < 0) 282 n = 0; 283 return getString(buf, 0, n); 284 } 285 286 287 private int readBytes( byte b[], int off, int len ) 288 throws IOException 289 { 290 if( endOfFile ) 292 return -1; 293 294 int bytesAvailable = (int)(dataEnd - filePosition); 297 if (bytesAvailable < 1) { 298 seek(filePosition, 0); 299 return readBytes( b, off, len ); 300 } 301 302 int copyLength = (bytesAvailable >= len) ? len : bytesAvailable; 304 System.arraycopy(buffer, (int)(filePosition - bufferStart), 305 b, off, copyLength); 306 filePosition += copyLength; 307 308 if (copyLength < len) { 310 int extraCopy = len - copyLength; 311 312 if (extraCopy > buffer.length) { 315 file.seek(filePosition); 316 extraCopy = file.read(b, off + copyLength, 317 len - copyLength); 318 } else { 319 seek(filePosition, 0); 322 if (!endOfFile) { 323 extraCopy = (extraCopy > dataSize) ? 324 dataSize : extraCopy; 325 System.arraycopy(buffer, 0, b, off + copyLength, 326 extraCopy); 327 } else { 328 extraCopy = -1; 329 } 330 } 331 332 if (extraCopy > 0) { 335 filePosition += extraCopy; 336 return copyLength + extraCopy; 337 } 338 } 339 340 return copyLength; 342 } 343 344 345 public int read() throws java.io.IOException { 346 if (filePosition < dataEnd) { 348 return (int)(buffer[(int)(filePosition++ - bufferStart)] 349 & 0xff); 350 } else if (endOfFile) { 351 return -1; 353 } else { 354 seek(filePosition, 0); 356 return read(); 357 } 358 } 359 360 public int available() throws java.io.IOException { 361 return 1; 362 } 363 364 public void unread(int c) throws java.io.IOException { 365 filePosition--; 366 } 367 368 public void write(String s) throws java.io.IOException { 369 byte[] b = getBytes(s); 370 int len = b.length; 371 372 if (len < buffer.length) { 374 int spaceInBuffer = 0; 376 int copyLength = 0; 377 if (filePosition >= bufferStart) 378 spaceInBuffer = (int)((bufferStart + buffer.length) - 379 filePosition); 380 if (spaceInBuffer > 0) { 381 copyLength = (spaceInBuffer > len) ? 383 len : spaceInBuffer; 384 System.arraycopy(b, 0, buffer, 385 (int)(filePosition - bufferStart), 386 copyLength ); 387 bufferModified = true; 388 long myDataEnd = filePosition + copyLength; 389 dataEnd = myDataEnd > dataEnd ? myDataEnd : dataEnd; 390 dataSize = (int)(dataEnd - bufferStart); 391 filePosition += copyLength; 392 } 393 394 if (copyLength < len) { 397 seek(filePosition, 0); 398 System.arraycopy(b, copyLength, buffer, 399 (int)(filePosition - bufferStart), 400 len - copyLength); 401 bufferModified = true; 402 long myDataEnd = filePosition + (len - copyLength); 403 dataEnd = myDataEnd > dataEnd ? myDataEnd : dataEnd; 404 dataSize = (int)(dataEnd - bufferStart); 405 filePosition += (len - copyLength); 406 } 407 } else { 408 410 if (bufferModified) { 412 flush( ); 413 bufferStart = dataEnd = dataSize = 0; 414 } 415 file.write( b, 0, len ); 416 filePosition += len; 417 } 418 } 419 420 public long tell() throws java.io.IOException { 421 return filePosition; 422 } 423 424 public void seek(long pos, int how) throws java.io.IOException { 425 if (how == 1) 426 pos += filePosition; 427 else if (how == 2) 428 pos += file.length(); 429 if (pos < 0) 430 pos = 0; 431 432 if (pos >= bufferStart && pos < dataEnd) { 434 filePosition = pos; 435 endOfFile = false; 436 return; 437 } 438 439 if (bufferModified) 441 flush(); 442 443 file.seek(pos); 445 filePosition = file.getFilePointer(); 446 bufferStart = filePosition; 447 448 dataSize = file.read(buffer); 450 if (dataSize < 0) { 451 dataSize = 0; 452 endOfFile = true; 453 } else { 454 endOfFile = false; 455 } 456 457 dataEnd = bufferStart + dataSize; 459 } 460 461 public void flush() throws java.io.IOException { 462 file.seek(bufferStart); 463 file.write(buffer, 0, dataSize); 464 bufferModified = false; 465 file.getFD().sync(); 466 } 467 468 public void close() throws java.io.IOException { 469 if (writing && bufferModified) { 470 file.seek(bufferStart); 471 file.write(buffer, 0, (int)dataSize); 472 } 473 474 file.close(); 475 } 476 477 public void truncate(long position) throws java.io.IOException { 478 flush(); 479 try { 480 java.lang.reflect.Method m = file.getClass().getMethod( 482 "setLength", new Class [] { Long.TYPE }); 483 m.invoke(file, new Object [] { new Long (position) }); 484 } catch (NoSuchMethodException exc) { 485 super.truncate(position); 486 } catch (SecurityException exc) { 487 super.truncate(position); 488 } catch (IllegalAccessException exc) { 489 super.truncate(position); 490 } catch (java.lang.reflect.InvocationTargetException exc) { 491 if (exc.getTargetException() instanceof IOException) 492 throw (IOException) exc.getTargetException(); 493 super.truncate(position); 494 } 495 } 496 497 public Object __tojava__(Class cls) throws IOException { 498 if (OutputStream.class.isAssignableFrom(cls) && writing) 499 return new FileOutputStream(file.getFD()); 500 else if (InputStream.class.isAssignableFrom(cls) && reading) 501 return new FileInputStream(file.getFD()); 502 return super.__tojava__(cls); 503 } 504 505 } 506 507 private static class TextWrapper extends FileWrapper { 508 private FileWrapper file; 509 private String sep; 510 private boolean sep_is_nl; 511 512 public TextWrapper(FileWrapper file) { 513 this.file = file; 514 sep = System.getProperty("line.separator"); 515 sep_is_nl = (sep == "\n"); 516 } 517 518 public String read(int n) throws java.io.IOException { 519 String s = this.file.read(n); 520 int index = s.indexOf('\r'); 521 if (index < 0) 522 return s; 523 StringBuffer buf = new StringBuffer (); 524 int start = 0; 525 int end = s.length(); 526 do { 527 buf.append(s.substring(start, index)); 528 buf.append('\n'); 529 start = index + 1; 530 if (start < end && s.charAt(start) == '\n') 531 start++; 532 index = s.indexOf('\r', start); 533 } while (index >= 0); 534 buf.append(s.substring(start)); 535 if (s.endsWith("\r") && file.available() > 0) { 536 int c = file.read(); 537 if (c != -1 && c != '\n') 538 file.unread(c); 539 } 540 return buf.toString(); 541 } 542 543 public int read() throws java.io.IOException { 544 int c = file.read(); 545 if (c != '\r') 546 return c; 547 if (file.available() > 0) { 548 c = file.read(); 549 if (c != -1 && c != '\n') 550 file.unread(c); 551 } 552 return '\n'; 553 } 554 555 public void write(String s) throws java.io.IOException { 556 if (!sep_is_nl) { 557 int index = s.indexOf('\n'); 558 if (index >= 0) { 559 StringBuffer buf = new StringBuffer (); 560 int start = 0; 561 do { 562 buf.append(s.substring(start, index)); 563 buf.append(sep); 564 start = index + 1; 565 index = s.indexOf('\n', start); 566 } while (index >= 0); 567 buf.append(s.substring(start)); 568 s = buf.toString(); 569 } 570 } 571 this.file.write(s); 572 } 573 574 public long tell() throws java.io.IOException { 575 return file.tell(); 576 } 577 578 public void seek(long pos, int how) throws java.io.IOException { 579 file.seek(pos, how); 580 } 581 582 public void flush() throws java.io.IOException { 583 file.flush(); 584 } 585 586 public void close() throws java.io.IOException { 587 file.close(); 588 } 589 590 public void truncate(long position) throws java.io.IOException { 591 file.truncate(position); 592 } 593 594 public Object __tojava__(Class cls) throws IOException { 595 return file.__tojava__(cls); 596 } 597 } 598 599 601 602 public static final String exposed_name="file"; 603 604 public static final Class exposed_base=PyObject.class; 605 606 public static void typeSetup(PyObject dict,PyType.Newstyle marker) { 607 dict.__setitem__("mode",new PyGetSetDescr("mode",PyFile.class,"getMode",null)); 608 dict.__setitem__("name",new PyGetSetDescr("name",PyFile.class,"getName",null)); 609 dict.__setitem__("closed",new PyGetSetDescr("closed",PyFile.class,"getClosed",null)); 610 class exposed___cmp__ extends PyBuiltinFunctionNarrow { 611 612 private PyFile self; 613 614 public PyObject getSelf() { 615 return self; 616 } 617 618 exposed___cmp__(PyFile self,PyBuiltinFunction.Info info) { 619 super(info); 620 this.self=self; 621 } 622 623 public PyBuiltinFunction makeBound(PyObject self) { 624 return new exposed___cmp__((PyFile)self,info); 625 } 626 627 public PyObject __call__(PyObject arg0) { 628 int ret=self.file___cmp__(arg0); 629 if (ret==-2) { 630 throw Py.TypeError("file"+".__cmp__(x,y) requires y to be '"+"file"+"', not a '"+(arg0).getType().fastGetName()+"'"); 631 } 632 return Py.newInteger(ret); 633 } 634 635 public PyObject inst_call(PyObject gself,PyObject arg0) { 636 PyFile self=(PyFile)gself; 637 int ret=self.file___cmp__(arg0); 638 if (ret==-2) { 639 throw Py.TypeError("file"+".__cmp__(x,y) requires y to be '"+"file"+"', not a '"+(arg0).getType().fastGetName()+"'"); 640 } 641 return Py.newInteger(ret); 642 } 643 644 } 645 dict.__setitem__("__cmp__",new PyMethodDescr("__cmp__",PyFile.class,1,1,new exposed___cmp__(null,null))); 646 class exposed___iter__ extends PyBuiltinFunctionNarrow { 647 648 private PyFile self; 649 650 public PyObject getSelf() { 651 return self; 652 } 653 654 exposed___iter__(PyFile self,PyBuiltinFunction.Info info) { 655 super(info); 656 this.self=self; 657 } 658 659 public PyBuiltinFunction makeBound(PyObject self) { 660 return new exposed___iter__((PyFile)self,info); 661 } 662 663 public PyObject __call__() { 664 return self.file___iter__(); 665 } 666 667 public PyObject inst_call(PyObject gself) { 668 PyFile self=(PyFile)gself; 669 return self.file___iter__(); 670 } 671 672 } 673 dict.__setitem__("__iter__",new PyMethodDescr("__iter__",PyFile.class,0,0,new exposed___iter__(null,null))); 674 class exposed___iternext__ extends PyBuiltinFunctionNarrow { 675 676 private PyFile self; 677 678 public PyObject getSelf() { 679 return self; 680 } 681 682 exposed___iternext__(PyFile self,PyBuiltinFunction.Info info) { 683 super(info); 684 this.self=self; 685 } 686 687 public PyBuiltinFunction makeBound(PyObject self) { 688 return new exposed___iternext__((PyFile)self,info); 689 } 690 691 public PyObject __call__() {
|