KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PyFile


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 import java.io.*;
5
6 // To do:
7
// - readinto(array)
8
// - modes w, a should disallow reading
9
// - what to do about buffer size?
10
// - isatty()
11
// - fileno() (defined, but always raises an exception, for urllib)
12

13 /**
14  * A python file wrapper around a java stream, reader/writer or file.
15  */

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 JavaDoc 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 JavaDoc read(int n) throws java.io.IOException JavaDoc {
32             throw new java.io.IOException JavaDoc("file not open for reading");
33         }
34         public int read() throws java.io.IOException JavaDoc {
35             throw new java.io.IOException JavaDoc("file not open for reading");
36         }
37         public int available() throws java.io.IOException JavaDoc {
38             throw new java.io.IOException JavaDoc("file not open for reading");
39         }
40         public void unread(int c) throws java.io.IOException JavaDoc {
41             throw new java.io.IOException JavaDoc("file doesn't support unread");
42         }
43         public void write(String JavaDoc s) throws java.io.IOException JavaDoc {
44             throw new java.io.IOException JavaDoc("file not open for writing");
45         }
46         public long tell() throws java.io.IOException JavaDoc {
47             throw new java.io.IOException JavaDoc("file doesn't support tell/seek");
48         }
49         public void seek(long pos, int how) throws java.io.IOException JavaDoc {
50             throw new java.io.IOException JavaDoc("file doesn't support tell/seek");
51         }
52         public void flush() throws java.io.IOException JavaDoc {
53         }
54         public void close() throws java.io.IOException JavaDoc {
55         }
56         public void truncate(long position) throws java.io.IOException JavaDoc {
57             throw new java.io.IOException JavaDoc("file doesn't support truncate");
58         }
59
60         public Object JavaDoc __tojava__(Class JavaDoc cls) throws IOException {
61             return null;
62         }
63         protected byte[] getBytes(String JavaDoc s) {
64             // Yes, I known the method is depricated, but it is the fastest
65
// way of converting between between byte[] and String
66
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 JavaDoc getString(byte[] buf, int offset, int len) {
74             // Yes, I known the method is depricated, but it is the fastest
75
// way of converting between between byte[] and String
76
if (binary) {
77                 return new String JavaDoc(buf, 0, offset, len);
78             } else
79                 return new String JavaDoc(buf, offset, len);
80         }
81     }
82
83     private static class InputStreamWrapper extends FileWrapper {
84         java.io.InputStream JavaDoc istream;
85
86         public InputStreamWrapper(java.io.InputStream JavaDoc s) {
87             istream = s;
88         }
89
90         public String JavaDoc read(int n) throws java.io.IOException JavaDoc {
91             if (n == 0)
92                 // nothing to do
93
return "";
94             if (n < 0) {
95                 // read until we hit EOF
96
byte buf[] = new byte[1024];
97                 StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
98                 for (int read=0; read >= 0; read=istream.read(buf))
99                     sbuf.append(getString(buf, 0, read));
100                 return sbuf.toString();
101             }
102             // read the next chunk available, but make sure it's at least
103
// one byte so as not to trip the `empty string' return value
104
// test done by the caller
105
//int avail = istream.available();
106
//n = (n > avail) ? n : avail;
107
byte buf[] = new byte[n];
108             int read = istream.read(buf);
109             if (read < 0)
110                 // EOF encountered
111
return "";
112             return new String JavaDoc(buf, 0, 0, read);
113         }
114
115         public int read() throws java.io.IOException JavaDoc {
116             return istream.read();
117         }
118
119         public int available() throws java.io.IOException JavaDoc {
120             return istream.available();
121         }
122
123         public void unread(int c) throws java.io.IOException JavaDoc {
124             ((java.io.PushbackInputStream JavaDoc)istream).unread(c);
125         }
126
127         public void close() throws java.io.IOException JavaDoc {
128             istream.close();
129         }
130
131         public Object JavaDoc __tojava__(Class JavaDoc 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 JavaDoc ostream;
140
141         public OutputStreamWrapper(java.io.OutputStream JavaDoc s) {
142             ostream = s;
143         }
144
145         private static final int MAX_WRITE = 30000;
146
147         public void write(String JavaDoc s) throws java.io.IOException JavaDoc {
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 JavaDoc {
160             ostream.flush();
161         }
162
163         public void close() throws java.io.IOException JavaDoc {
164             ostream.close();
165         }
166
167         public Object JavaDoc __tojava__(Class JavaDoc 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 JavaDoc ostream;
176
177         public IOStreamWrapper(java.io.InputStream JavaDoc istream,
178                                java.io.OutputStream JavaDoc ostream) {
179             super(istream);
180             this.ostream = ostream;
181         }
182
183         public void write(String JavaDoc s) throws java.io.IOException JavaDoc {
184             ostream.write(getBytes(s));
185         }
186
187         public void flush() throws java.io.IOException JavaDoc {
188             ostream.flush();
189         }
190
191         public void close() throws java.io.IOException JavaDoc {
192             ostream.close();
193             istream.close();
194         }
195
196         public Object JavaDoc __tojava__(Class JavaDoc 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 JavaDoc writer;
205
206         public WriterWrapper(java.io.Writer JavaDoc s) {
207             writer = s;
208         }
209
210         //private static final int MAX_WRITE = 30000;
211

212         public void write(String JavaDoc s) throws java.io.IOException JavaDoc {
213             writer.write(s);
214         }
215
216         public void flush() throws java.io.IOException JavaDoc {
217             writer.flush();
218         }
219
220         public void close() throws java.io.IOException JavaDoc {
221             writer.close();
222         }
223     }
224
225     private static class RFileWrapper extends FileWrapper {
226         /** The default buffer size, in bytes. */
227         protected static final int defaultBufferSize = 4096;
228
229         /** The underlying java.io.RandomAccessFile. */
230         protected java.io.RandomAccessFile JavaDoc file;
231
232         /** The offset in bytes from the file start, of the next read or
233          * write operation. */

234         protected long filePosition;
235
236         /** The buffer used to load the data. */
237         protected byte buffer[];
238
239         /** The offset in bytes of the start of the buffer, from the start
240          * of the file. */

241         protected long bufferStart;
242
243         /** The offset in bytes of the end of the data in the buffer, from
244          * the start of the file. This can be calculated from
245          * <code>bufferStart + dataSize</code>, but it is cached to speed
246          * up the read( ) method. */

247         protected long dataEnd;
248
249         /** The size of the data stored in the buffer, in bytes. This may be
250          * less than the size of the buffer.*/

251         protected int dataSize;
252
253         /** True if we are at the end of the file. */
254         protected boolean endOfFile;
255
256         /** True if the data in the buffer has been modified. */
257         boolean bufferModified = false;
258
259         public RFileWrapper(java.io.RandomAccessFile JavaDoc file) {
260             this(file, 8092);
261         }
262
263         public RFileWrapper(java.io.RandomAccessFile JavaDoc 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 JavaDoc read(int n) throws java.io.IOException JavaDoc {
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             // Check for end of file.
291
if( endOfFile )
292                 return -1;
293
294             // See how many bytes are available in the buffer - if none,
295
// seek to the file position to update the buffer and try again.
296
int bytesAvailable = (int)(dataEnd - filePosition);
297             if (bytesAvailable < 1) {
298                 seek(filePosition, 0);
299                 return readBytes( b, off, len );
300             }
301
302             // Copy as much as we can.
303
int copyLength = (bytesAvailable >= len) ? len : bytesAvailable;
304             System.arraycopy(buffer, (int)(filePosition - bufferStart),
305                              b, off, copyLength);
306             filePosition += copyLength;
307
308             // If there is more to copy...
309
if (copyLength < len) {
310                 int extraCopy = len - copyLength;
311
312                 // If the amount remaining is more than a buffer's
313
// length, read it directly from the file.
314
if (extraCopy > buffer.length) {
315                     file.seek(filePosition);
316                     extraCopy = file.read(b, off + copyLength,
317                                           len - copyLength);
318                 } else {
319                     // ...or read a new buffer full, and copy as much
320
// as possible...
321
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 we did manage to copy any more, update the file
333
// position and return the amount copied.
334
if (extraCopy > 0) {
335                     filePosition += extraCopy;
336                     return copyLength + extraCopy;
337                 }
338             }
339
340             // Return the amount copied.
341
return copyLength;
342         }
343
344
345         public int read() throws java.io.IOException JavaDoc {
346             // If the file position is within the data, return the byte...
347
if (filePosition < dataEnd) {
348                 return (int)(buffer[(int)(filePosition++ - bufferStart)]
349                                    & 0xff);
350             } else if (endOfFile) {
351                // ...or should we indicate EOF...
352
return -1;
353             } else {
354                 // ...or seek to fill the buffer, and try again.
355
seek(filePosition, 0);
356                 return read();
357             }
358         }
359
360         public int available() throws java.io.IOException JavaDoc {
361             return 1;
362         }
363
364         public void unread(int c) throws java.io.IOException JavaDoc {
365             filePosition--;
366         }
367
368         public void write(String JavaDoc s) throws java.io.IOException JavaDoc {
369             byte[] b = getBytes(s);
370             int len = b.length;
371
372             // If the amount of data is small (less than a full buffer)...
373
if (len < buffer.length) {
374                 // If any of the data fits within the buffer...
375
int spaceInBuffer = 0;
376                 int copyLength = 0;
377                 if (filePosition >= bufferStart)
378                     spaceInBuffer = (int)((bufferStart + buffer.length) -
379                                           filePosition);
380                 if (spaceInBuffer > 0) {
381                     // Copy as much as possible to the buffer.
382
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 there is any data remaining, move to the
395
// new position and copy to the new buffer.
396
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                 // ...or write a lot of data...
409

410                 // Flush the current buffer, and write this data to the file.
411
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 JavaDoc {
421             return filePosition;
422         }
423
424         public void seek(long pos, int how) throws java.io.IOException JavaDoc {
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 the seek is into the buffer, just update the file pointer.
433
if (pos >= bufferStart && pos < dataEnd) {
434                 filePosition = pos;
435                 endOfFile = false;
436                 return;
437             }
438
439             // If the current buffer is modified, write it to disk.
440
if (bufferModified)
441                 flush();
442
443             // Move to the position on the disk.
444
file.seek(pos);
445             filePosition = file.getFilePointer();
446             bufferStart = filePosition;
447
448             // Fill the buffer from the disk.
449
dataSize = file.read(buffer);
450             if (dataSize < 0) {
451                 dataSize = 0;
452                 endOfFile = true;
453             } else {
454                 endOfFile = false;
455             }
456
457             // Cache the position of the buffer end.
458
dataEnd = bufferStart + dataSize;
459         }
460
461         public void flush() throws java.io.IOException JavaDoc {
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 JavaDoc {
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 JavaDoc {
478             flush();
479             try {
480                 // file.setLength(position);
481
java.lang.reflect.Method JavaDoc m = file.getClass().getMethod(
482                         "setLength", new Class JavaDoc[] { Long.TYPE });
483                 m.invoke(file, new Object JavaDoc[] { new Long JavaDoc(position) });
484             } catch (NoSuchMethodException JavaDoc exc) {
485                 super.truncate(position);
486             } catch (SecurityException JavaDoc exc) {
487                 super.truncate(position);
488             } catch (IllegalAccessException JavaDoc exc) {
489                 super.truncate(position);
490             } catch (java.lang.reflect.InvocationTargetException JavaDoc exc) {
491                 if (exc.getTargetException() instanceof IOException)
492                     throw (IOException) exc.getTargetException();
493                 super.truncate(position);
494             }
495         }
496
497         public Object JavaDoc __tojava__(Class JavaDoc 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 JavaDoc 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 JavaDoc read(int n) throws java.io.IOException JavaDoc {
519             String JavaDoc s = this.file.read(n);
520             int index = s.indexOf('\r');
521             if (index < 0)
522                 return s;
523             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc {
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 JavaDoc s) throws java.io.IOException JavaDoc {
556             if (!sep_is_nl) {
557                 int index = s.indexOf('\n');
558                 if (index >= 0) {
559                     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc {
575             return file.tell();
576         }
577
578         public void seek(long pos, int how) throws java.io.IOException JavaDoc {
579             file.seek(pos, how);
580         }
581
582         public void flush() throws java.io.IOException JavaDoc {
583             file.flush();
584         }
585
586         public void close() throws java.io.IOException JavaDoc {
587             file.close();
588         }
589
590         public void truncate(long position) throws java.io.IOException JavaDoc {
591             file.truncate(position);
592         }
593
594         public Object JavaDoc __tojava__(Class JavaDoc cls) throws IOException {
595             return file.__tojava__(cls);
596         }
597     }
598
599     //~ BEGIN GENERATED
600
/* type info */
601
602     public static final String JavaDoc exposed_name="file";
603
604     public static final Class JavaDoc 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__() {
692                 return self.file___iternext__();
693             }
694
695             public PyObject inst_call(PyObject gself) {
696                 PyFile self=(PyFile)gself;
697                 return self.file___iternext__();
698             }
699
700         }
701         dict.__setitem__("__iternext__",new PyMethodDescr("__iternext__",PyFile.class,0,0,new exposed___iternext__(null,null)));
702         class exposed___nonzero__ extends PyBuiltinFunctionNarrow {
703
704             private PyFile self;
705
706             public PyObject getSelf() {
707                 return self;
708             }
709
710             exposed___nonzero__(PyFile self,PyBuiltinFunction.Info info) {
711                 super(info);
712                 this.self=self;
713             }
714
715             public PyBuiltinFunction makeBound(PyObject self) {
716                 return new exposed___nonzero__((PyFile)self,info);
717             }
718
719             public PyObject __call__() {
720                 return Py.newBoolean(self.file___nonzero__());
721             }
722
723             public PyObject inst_call(PyObject gself) {
724                 PyFile self=(PyFile)gself;
725                 return Py.newBoolean(self.file___nonzero__());
726             }
727
728         }
729         dict.__setitem__("__nonzero__",new PyMethodDescr("__nonzero__",PyFile.class,0,0,new exposed___nonzero__(null,null)));
730         class exposed___repr__ extends PyBuiltinFunctionNarrow {
731
732             private PyFile self;
733
734             public PyObject getSelf() {
735                 return self;
736             }
737
738             exposed___repr__(PyFile self,PyBuiltinFunction.Info info) {
739                 super(info);
740                 this.self=self;
741             }
742
743             public PyBuiltinFunction makeBound(PyObject self) {
744                 return new exposed___repr__((PyFile)self,info);
745             }
746
747             public PyObject __call__() {
748                 return new PyString(self.file_toString());
749             }
750
751             public PyObject inst_call(PyObject gself) {
752                 PyFile self=(PyFile)gself;
753                 return new PyString(self.file_toString());
754             }
755
756         }
757         dict.__setitem__("__repr__",new PyMethodDescr("__repr__",PyFile.class,0,0,new exposed___repr__(null,null)));
758         class exposed___str__ extends PyBuiltinFunctionNarrow {
759
760             private PyFile self;
761
762             public PyObject getSelf() {
763                 return self;
764             }
765
766             exposed___str__(PyFile self,PyBuiltinFunction.Info info) {
767                 super(info);
768                 this.self=self;
769             }
770
771             public PyBuiltinFunction makeBound(PyObject self) {
772                 return new exposed___str__((PyFile)self,info);
773             }
774
775             public PyObject __call__() {
776                 return new PyString(self.file_toString());
777             }
778
779             public PyObject inst_call(PyObject gself) {
780                 PyFile self=(PyFile)gself;
781                 return new PyString(self.file_toString());
782             }
783
784         }
785         dict.__setitem__("__str__",new PyMethodDescr("__str__",PyFile.class,0,0,new exposed___str__(null,null)));
786         class exposed_close extends PyBuiltinFunctionNarrow {
787
788             private PyFile self;
789
790             public PyObject getSelf() {
791                 return self;
792             }
793
794             exposed_close(PyFile self,PyBuiltinFunction.Info info) {
795                 super(info);
796                 this.self=self;
797             }
798
799             public PyBuiltinFunction makeBound(PyObject self) {
800                 return new exposed_close((PyFile)self,info);
801             }
802
803             public PyObject __call__() {
804                 self.file_close();
805                 return Py.None;
806             }
807
808             public PyObject inst_call(PyObject gself) {
809                 PyFile self=(PyFile)gself;
810                 self.file_close();
811                 return Py.None;
812             }
813
814         }
815         dict.__setitem__("close",new PyMethodDescr("close",PyFile.class,0,0,new exposed_close(null,null)));
816         class exposed_flush extends PyBuiltinFunctionNarrow {
817
818             private PyFile self;
819
820             public PyObject getSelf() {
821                 return self;
822             }
823
824             exposed_flush(PyFile self,PyBuiltinFunction.Info info) {
825                 super(info);
826                 this.self=self;
827             }
828
829             public PyBuiltinFunction makeBound(PyObject self) {
830                 return new exposed_flush((PyFile)self,info);
831             }
832
833             public PyObject __call__() {
834                 self.file_flush();
835                 return Py.None;
836             }
837
838             public PyObject inst_call(PyObject gself) {
839                 PyFile self=(PyFile)gself;
840                 self.file_flush();
841                 return Py.None;
842             }
843
844         }
845         dict.__setitem__("flush",new PyMethodDescr("flush",PyFile.class,0,0,new exposed_flush(null,null)));
846         class exposed_read extends PyBuiltinFunctionNarrow {
847
848             private PyFile self;
849
850             public PyObject getSelf() {
851                 return self;
852             }
853
854             exposed_read(PyFile self,PyBuiltinFunction.Info info) {
855                 super(info);
856                 this.self=self;
857             }
858
859             public PyBuiltinFunction makeBound(PyObject self) {
860                 return new exposed_read((PyFile)self,info);
861             }
862
863             public PyObject __call__(PyObject arg0) {
864                 try {
865                     return new PyString(self.file_read(arg0.asInt(0)));
866                 } catch (PyObject.ConversionException e) {
867                     String JavaDoc msg;
868                     switch (e.index) {
869                     case 0:
870                         msg="expected an integer";
871                         break;
872                     default:
873                         msg="xxx";
874                     }
875                     throw Py.TypeError(msg);
876                 }
877             }
878
879             public PyObject inst_call(PyObject gself,PyObject arg0) {
880                 PyFile self=(PyFile)gself;
881                 try {
882                     return new PyString(self.file_read(arg0.asInt(0)));
883                 } catch (PyObject.ConversionException e) {
884                     String JavaDoc msg;
885                     switch (e.index) {
886                     case 0:
887                         msg="expected an integer";
888                         break;
889                     default:
890                         msg="xxx";
891                     }
892                     throw Py.TypeError(msg);
893                 }
894             }
895
896             public PyObject __call__() {
897                 return new PyString(self.file_read());
898             }
899
900             public PyObject inst_call(PyObject gself) {
901                 PyFile self=(PyFile)gself;
902                 return new PyString(self.file_read());
903             }
904
905         }
906         dict.__setitem__("read",new PyMethodDescr("read",PyFile.class,0,1,new exposed_read(null,null)));
907         class exposed_readline extends PyBuiltinFunctionNarrow {
908
909             private PyFile self;
910
911             public PyObject getSelf() {
912                 return self;
913             }
914
915             exposed_readline(PyFile self,PyBuiltinFunction.Info info) {
916                 super(info);
917                 this.self=self;
918             }
919
920             public PyBuiltinFunction makeBound(PyObject self) {
921                 return new exposed_readline((PyFile)self,info);
922             }
923
924             public PyObject __call__(PyObject arg0) {
925                 try {
926                     return new PyString(self.file_readline(arg0.asInt(0)));
927                 } catch (PyObject.ConversionException e) {
928                     String JavaDoc msg;
929                     switch (e.index) {
930                     case 0:
931                         msg="expected an integer";
932                         break;
933                     default:
934                         msg="xxx";
935                     }
936                     throw Py.TypeError(msg);
937                 }
938             }
939
940             public PyObject inst_call(PyObject gself,PyObject arg0) {
941                 PyFile self=(PyFile)gself;
942                 try {
943                     return new PyString(self.file_readline(arg0.asInt(0)));
944                 } catch (PyObject.ConversionException e) {
945                     String JavaDoc msg;
946                     switch (e.index) {
947                     case 0:
948                         msg="expected an integer";
949                         break;
950                     default:
951                         msg="xxx";
952                     }
953                     throw Py.TypeError(msg);
954                 }
955             }
956
957             public PyObject __call__() {
958                 return new PyString(self.file_readline());
959             }
960
961             public PyObject inst_call(PyObject gself) {
962                 PyFile self=(PyFile)gself;
963                 return new PyString(self.file_readline());
964             }
965
966         }
967         dict.__setitem__("readline",new PyMethodDescr("readline",PyFile.class,0,1,new exposed_readline(null,null)));
968         class exposed_readlines extends PyBuiltinFunctionNarrow {
969
970             private PyFile self;
971
972             public PyObject getSelf() {
973                 return self;
974             }
975
976             exposed_readlines(PyFile self,PyBuiltinFunction.Info info) {
977                 super(info);
978                 this.self=self;
979             }
980
981             public PyBuiltinFunction makeBound(PyObject self) {
982                 return new exposed_readlines((PyFile)self,info);
983             }
984
985             public PyObject __call__(PyObject arg0) {
986                 try {
987                     return self.file_readlines(arg0.asInt(0));
988                 } catch (PyObject.ConversionException e) {
989                     String JavaDoc msg;
990                     switch (e.index) {
991                     case 0:
992                         msg="expected an integer";
993                         break;
994                     default:
995                         msg="xxx";
996                     }
997                     throw Py.TypeError(msg);
998                 }
999             }
1000
1001            public PyObject inst_call(PyObject gself,PyObject arg0) {
1002                PyFile self=(PyFile)gself;
1003                try {
1004                    return self.file_readlines(arg0.asInt(0));
1005                } catch (PyObject.ConversionException e) {
1006                    String JavaDoc msg;
1007                    switch (e.index) {
1008                    case 0:
1009                        msg="expected an integer";
1010                        break;
1011                    default:
1012                        msg="xxx";
1013                    }
1014                    throw Py.TypeError(msg);
1015                }
1016            }
1017
1018            public PyObject __call__() {
1019                return self.file_readlines();
1020            }
1021
1022            public PyObject inst_call(PyObject gself) {
1023                PyFile self=(PyFile)gself;
1024                return self.file_readlines();
1025            }
1026
1027        }
1028        dict.__setitem__("readlines",new PyMethodDescr("readlines",PyFile.class,0,1,new exposed_readlines(null,null)));
1029        class exposed_seek extends PyBuiltinFunctionNarrow {
1030
1031            private PyFile self;
1032
1033            public PyObject getSelf() {
1034                return self;
1035            }
1036
1037            exposed_seek(PyFile self,PyBuiltinFunction.Info info) {
1038                super(info);
1039                this.self=self;
1040            }
1041
1042            public PyBuiltinFunction makeBound(PyObject self) {
1043                return new exposed_seek((PyFile)self,info);
1044            }
1045
1046            public PyObject __call__(PyObject arg0,PyObject arg1) {
1047                try {
1048                    self.file_seek(arg0.asLong(0),arg1.asInt(1));
1049                    return Py.None;
1050                } catch (PyObject.ConversionException e) {
1051                    String JavaDoc msg;
1052                    switch (e.index) {
1053                    case 0:
1054                        msg="expected a long";
1055                        break;
1056                    case 1:
1057                        msg="expected an integer";
1058                        break;
1059                    default:
1060                        msg="xxx";
1061                    }
1062                    throw Py.TypeError(msg);
1063                }
1064            }
1065
1066            public PyObject inst_call(PyObject gself,PyObject arg0,PyObject arg1) {
1067                PyFile self=(PyFile)gself;
1068                try {
1069                    self.file_seek(arg0.asLong(0),arg1.asInt(1));
1070                    return Py.None;
1071                } catch (PyObject.ConversionException e) {
1072                    String JavaDoc msg;
1073                    switch (e.index) {
1074                    case 0:
1075                        msg="expected a long";
1076                        break;
1077                    case 1:
1078                        msg="expected an integer";
1079                        break;
1080                    default:
1081                        msg="xxx";
1082                    }
1083                    throw Py.TypeError(msg);
1084                }
1085            }
1086
1087            public PyObject __call__(PyObject arg0) {
1088                try {
1089                    self.file_seek(arg0.asLong(0));
1090                    return Py.None;
1091                } catch (PyObject.ConversionException e) {
1092                    String JavaDoc msg;
1093                    switch (e.index) {
1094                    case 0:
1095                        msg="expected a long";
1096                        break;
1097                    default:
1098                        msg="xxx";
1099                    }
1100                    throw Py.TypeError(msg);
1101                }
1102            }
1103
1104            public PyObject inst_call(PyObject gself,PyObject arg0) {
1105                PyFile self=(PyFile)gself;
1106                try {
1107                    self.file_seek(arg0.asLong(0));
1108                    return Py.None;
1109                } catch (PyObject.ConversionException e) {
1110                    String JavaDoc msg;
1111                    switch (e.index) {
1112                    case 0:
1113                        msg="expected a long";
1114                        break;
1115                    default:
1116                        msg="xxx";
1117                    }
1118                    throw Py.TypeError(msg);
1119                }
1120            }
1121
1122        }
1123        dict.__setitem__("seek",new PyMethodDescr("seek",PyFile.class,1,2,new exposed_seek(null,null)));
1124        class exposed_tell extends PyBuiltinFunctionNarrow {
1125
1126            private PyFile self;
1127
1128            public PyObject getSelf() {
1129                return self;
1130            }
1131
1132            exposed_tell(PyFile self,PyBuiltinFunction.Info info) {
1133                super(info);
1134                this.self=self;
1135            }
1136
1137            public PyBuiltinFunction makeBound(PyObject self) {
1138                return new exposed_tell((PyFile)self,info);
1139            }
1140
1141            public PyObject __call__() {
1142                return new PyLong(self.file_tell());
1143            }
1144
1145            public PyObject inst_call(PyObject gself) {
1146                PyFile self=(PyFile)gself;
1147                return new PyLong(self.file_tell());
1148            }
1149
1150        }
1151        dict.__setitem__("tell",new PyMethodDescr("tell",PyFile.class,0,0,new exposed_tell(null,null)));
1152        class exposed_next extends PyBuiltinFunctionNarrow {
1153
1154            private PyFile self;
1155
1156            public PyObject getSelf() {
1157                return self;
1158            }
1159
1160            exposed_next(PyFile self,PyBuiltinFunction.Info info) {
1161                super(info);
1162                this.self=self;
1163            }
1164
1165            public PyBuiltinFunction makeBound(PyObject self) {
1166                return new exposed_next((PyFile)self,info);
1167            }
1168
1169            public PyObject __call__() {
1170                return self.file_next();
1171            }
1172
1173            public PyObject inst_call(PyObject gself) {
1174                PyFile self=(PyFile)gself;
1175                return self.file_next();
1176            }
1177
1178        }
1179        dict.__setitem__("next",new PyMethodDescr("next",PyFile.class,0,0,new exposed_next(null,null)));
1180        class exposed_truncate extends PyBuiltinFunctionNarrow {
1181
1182            private PyFile self;
1183
1184            public PyObject getSelf() {
1185                return self;
1186            }
1187
1188            exposed_truncate(PyFile self,PyBuiltinFunction.Info info) {
1189                super(info);
1190                this.self=self;
1191            }
1192
1193            public PyBuiltinFunction makeBound(PyObject self) {
1194                return new exposed_truncate((PyFile)self,info);
1195            }
1196
1197            public PyObject __call__(PyObject arg0) {
1198                try {
1199                    self.file_truncate(arg0.asLong(0));
1200                    return Py.None;
1201                } catch (PyObject.ConversionException e) {
1202                    String JavaDoc msg;
1203                    switch (e.index) {
1204                    case 0:
1205                        msg="expected a long";
1206                        break;
1207                    default:
1208                        msg="xxx";
1209                    }
1210                    throw Py.TypeError(msg);
1211                }
1212            }
1213
1214            public PyObject inst_call(PyObject gself,PyObject arg0) {
1215                PyFile self=(PyFile)gself;
1216                try {
1217                    self.file_truncate(arg0.asLong(0));
1218                    return Py.None;
1219                } catch (PyObject.ConversionException e) {
1220                    String JavaDoc msg;
1221                    switch (e.index) {
1222                    case 0:
1223                        msg="expected a long";
1224                        break;
1225                    default:
1226                        msg="xxx";
1227                    }
1228                    throw Py.TypeError(msg);
1229                }
1230            }
1231
1232            public PyObject __call__() {
1233                self.file_truncate();
1234                return Py.None;
1235            }
1236
1237            public PyObject inst_call(PyObject gself) {
1238                PyFile self=(PyFile)gself;
1239                self.file_truncate();
1240                return Py.None;
1241            }
1242
1243        }
1244        dict.__setitem__("truncate",new PyMethodDescr("truncate",PyFile.class,0,1,new exposed_truncate(null,null)));
1245        class exposed_write extends PyBuiltinFunctionNarrow {
1246
1247            private PyFile self;
1248
1249            public PyObject getSelf() {
1250                return self;
1251            }
1252
1253            exposed_write(PyFile self,PyBuiltinFunction.Info info) {
1254                super(info);
1255                this.self=self;
1256            }
1257
1258            public PyBuiltinFunction makeBound(PyObject self) {
1259                return new exposed_write((PyFile)self,info);
1260            }
1261
1262            public PyObject __call__(PyObject arg0) {
1263                try {
1264                    self.file_write(arg0.asString(0));
1265                    return Py.None;
1266                } catch (PyObject.ConversionException e) {
1267                    String JavaDoc msg;
1268                    switch (e.index) {
1269                    case 0:
1270                        msg="expected a string";
1271                        break;
1272                    default:
1273                        msg="xxx";
1274                    }
1275                    throw Py.TypeError(msg);
1276                }
1277            }
1278
1279            public PyObject inst_call(PyObject gself,PyObject arg0) {
1280                PyFile self=(PyFile)gself;
1281                try {
1282                    self.file_write(arg0.asString(0));
1283                    return Py.None;
1284                } catch (PyObject.ConversionException e) {
1285                    String JavaDoc msg;
1286                    switch (e.index) {
1287                    case 0:
1288                        msg="expected a string";
1289                        break;
1290                    default:
1291                        msg="xxx";
1292                    }
1293                    throw Py.TypeError(msg);
1294                }
1295            }
1296
1297        }
1298        dict.__setitem__("write",new PyMethodDescr("write",PyFile.class,1,1,new exposed_write(null,null)));
1299        class exposed_writelines extends PyBuiltinFunctionNarrow {
1300
1301            private PyFile self;
1302
1303            public PyObject getSelf() {
1304                return self;
1305            }
1306
1307            exposed_writelines(PyFile self,PyBuiltinFunction.Info info) {
1308                super(info);
1309                this.self=self;
1310            }
1311
1312            public PyBuiltinFunction makeBound(PyObject self) {
1313                return new exposed_writelines((PyFile)self,info);
1314            }
1315
1316            public PyObject __call__(PyObject arg0) {
1317                self.file_writelines(arg0);
1318                return Py.None;
1319            }
1320
1321            public PyObject inst_call(PyObject gself,PyObject arg0) {
1322                PyFile self=(PyFile)gself;
1323                self.file_writelines(arg0);
1324                return Py.None;
1325            }
1326
1327        }
1328        dict.__setitem__("writelines",new PyMethodDescr("writelines",PyFile.class,1,1,new exposed_writelines(null,null)));
1329        class exposed_xreadlines extends PyBuiltinFunctionNarrow {
1330
1331            private PyFile self;
1332
1333            public PyObject getSelf() {
1334                return self;
1335            }
1336
1337            exposed_xreadlines(PyFile self,PyBuiltinFunction.Info info) {
1338                super(info);
1339                this.self=self;
1340            }
1341
1342            public PyBuiltinFunction makeBound(PyObject self) {
1343                return new exposed_xreadlines((PyFile)self,info);
1344            }
1345
1346            public PyObject __call__() {
1347                return self.file_xreadlines();
1348            }
1349
1350            public PyObject inst_call(PyObject gself) {
1351                PyFile self=(PyFile)gself;
1352                return self.file_xreadlines();
1353            }
1354
1355        }
1356        dict.__setitem__("xreadlines",new PyMethodDescr("xreadlines",PyFile.class,0,0,new exposed_xreadlines(null,null)));
1357        class exposed___init__ extends PyBuiltinFunctionWide {
1358
1359            private PyFile self;
1360
1361            public PyObject getSelf() {
1362                return self;
1363            }
1364
1365            exposed___init__(PyFile self,PyBuiltinFunction.Info info) {
1366                super(info);
1367                this.self=self;
1368            }
1369
1370            public PyBuiltinFunction makeBound(PyObject self) {
1371                return new exposed___init__((PyFile)self,info);
1372            }
1373
1374            public PyObject inst_call(PyObject self,PyObject[]args) {
1375                return inst_call(self,args,Py.NoKeywords);
1376            }
1377
1378            public PyObject __call__(PyObject[]args) {
1379                return __call__(args,Py.NoKeywords);
1380            }
1381
1382            public PyObject __call__(PyObject[]args,String JavaDoc[]keywords) {
1383                self.file_init(args,keywords);
1384                return Py.None;
1385            }
1386
1387            public PyObject inst_call(PyObject gself,PyObject[]args,String JavaDoc[]keywords) {
1388                PyFile self=(PyFile)gself;
1389                self.file_init(args,keywords);
1390                return Py.None;
1391            }
1392
1393        }
1394        dict.__setitem__("__init__",new PyMethodDescr("__init__",PyFile.class,-1,-1,new exposed___init__(null,null)));
1395        dict.__setitem__("__new__",new PyNewWrapper(PyFile.class,"__new__",-1,-1) {
1396            public PyObject new_impl(boolean init, PyType subtype, PyObject[] args, String JavaDoc[] keywords) {
1397                PyFile newobj;
1398                if (for_type == subtype) {
1399                    newobj = null;
1400                    if (init) {
1401                        if (args.length == 0) {
1402                            newobj = new PyFile();
1403                            newobj.file_init(args, keywords);
1404                        } else if (args[0] instanceof PyString ||
1405                                (args[0] instanceof PyJavaInstance &&
1406                                ((PyJavaInstance) args[0]).javaProxy == String JavaDoc.class)) {
1407                            // If first arg is a PyString or String, assume its being
1408
// called as a builtin.
1409
newobj = new PyFile();
1410                            newobj.file_init(args, keywords);
1411                        } else {
1412                            // assume it's being called as a java class
1413
PyJavaClass pjc = new PyJavaClass(PyFile.class);
1414                            newobj = (PyFile) pjc.__call__(args, keywords);
1415                        }
1416                    } else {
1417                        newobj = new PyFile();
1418                    }
1419                } else {
1420                    newobj = new PyFileDerived(subtype);
1421                }
1422                return newobj;
1423            }
1424
1425        });
1426    }
1427    //~ END GENERATED
1428

1429    public String JavaDoc name;
1430    public String JavaDoc mode;
1431    public boolean softspace;
1432    public boolean closed;
1433
1434    private FileWrapper file;
1435
1436    private static java.io.InputStream JavaDoc _pb(java.io.InputStream JavaDoc s, String JavaDoc mode)
1437    {
1438        if (mode.indexOf('b') < 0) {
1439            if(s instanceof java.io.PushbackInputStream JavaDoc) {
1440                return s;
1441            }
1442            return new java.io.PushbackInputStream JavaDoc(s);
1443        }
1444        return s;
1445    }
1446
1447    final void file_init(PyObject[] args,String JavaDoc[] kwds) {
1448
1449        ArgParser ap = new ArgParser("file", args, kwds, new String JavaDoc[] { "name", "mode" }, 1);
1450        String JavaDoc nameArg = ap.getString(0, null);
1451        String JavaDoc modeArg = ap.getString(1, "r");
1452        int buffArg = 0;//ap.getInt(2, 0);
1453
FileWrapper fw = _setup(nameArg, modeArg, buffArg);
1454
1455        //xxx: c&p'ed from one of the constructors.
1456
fw.setMode(modeArg);
1457        this.name = nameArg;
1458        this.mode = modeArg;
1459        this.softspace = false;
1460        this.closed = false;
1461        if (modeArg.indexOf('b') < 0)
1462            this.file = new TextWrapper(fw);
1463        else
1464            this.file = fw;
1465    }
1466
1467    public PyFile() {
1468        //xxx: this constructor should only be used in conjunction with file_init
1469
}
1470
1471    public PyFile(PyType subType) {
1472        super(subType);
1473    }
1474
1475    public PyFile(FileWrapper file, String JavaDoc name, String JavaDoc mode) {
1476        file.setMode(mode);
1477        this.name = name;
1478        this.mode = mode;
1479        this.softspace = false;
1480        this.closed = false;
1481        if (mode.indexOf('b') < 0)
1482            this.file = new TextWrapper(file);
1483        else
1484            this.file = file;
1485    }
1486
1487    public PyFile(java.io.InputStream JavaDoc istream, java.io.OutputStream JavaDoc ostream,
1488                  String JavaDoc name, String JavaDoc mode)
1489    {
1490        this(new IOStreamWrapper(_pb(istream, mode), ostream), name, mode);
1491    }
1492
1493    public PyFile(java.io.InputStream JavaDoc istream, java.io.OutputStream JavaDoc ostream,
1494                  String JavaDoc name)
1495    {
1496        this(istream, ostream, name, "r+");
1497    }
1498
1499    public PyFile(java.io.InputStream JavaDoc istream, java.io.OutputStream JavaDoc ostream) {
1500        this(istream, ostream, "<???>", "r+");
1501    }
1502
1503    public PyFile(java.io.InputStream JavaDoc istream, String JavaDoc name, String JavaDoc mode) {
1504        this(new InputStreamWrapper(_pb(istream, mode)), name, mode);
1505    }
1506
1507    public PyFile(java.io.InputStream JavaDoc istream, String JavaDoc name) {
1508        this(istream, name, "r");
1509    }
1510
1511    public PyFile(java.io.InputStream JavaDoc istream) {
1512        this(istream, "<???>", "r");
1513    }
1514
1515    public PyFile(java.io.OutputStream JavaDoc ostream, String JavaDoc name, String JavaDoc mode) {
1516        this(new OutputStreamWrapper(ostream), name, mode);
1517    }
1518
1519    public PyFile(java.io.OutputStream JavaDoc ostream, String JavaDoc name) {
1520        this(ostream, name, "w");
1521    }
1522
1523    public PyFile(java.io.OutputStream JavaDoc ostream) {
1524        this(ostream, "<???>", "w");
1525    }
1526
1527    public PyFile(java.io.Writer JavaDoc ostream, String JavaDoc name, String JavaDoc mode) {
1528        this(new WriterWrapper(ostream), name, mode);
1529    }
1530
1531    public PyFile(java.io.Writer JavaDoc ostream, String JavaDoc name) {
1532        this(ostream, name, "w");
1533    }
1534
1535    public PyFile(java.io.Writer JavaDoc ostream) {
1536        this(ostream, "<???>", "w");
1537    }
1538
1539    public PyFile(java.io.RandomAccessFile JavaDoc file, String JavaDoc name, String JavaDoc mode) {
1540        this(new RFileWrapper(file), name, mode);
1541    }
1542
1543    public PyFile(java.io.RandomAccessFile JavaDoc file, String JavaDoc name) {
1544        this(file, name, "r+");
1545    }
1546
1547    public PyFile(java.io.RandomAccessFile JavaDoc file) {
1548        this(file, "<???>", "r+");
1549    }
1550
1551    public PyFile(String JavaDoc name, String JavaDoc mode, int bufsize) {
1552        this(_setup(name, mode, bufsize), name, mode);
1553    }
1554
1555    public void __setattr__(String JavaDoc name, PyObject value) {
1556        // softspace is the only writeable file object attribute
1557
if (name == "softspace")
1558            softspace = value.__nonzero__();
1559        else if (name == "mode" || name == "closed" || name == "name")
1560            throw Py.TypeError("readonly attribute: " + name);
1561        else
1562            throw Py.AttributeError(name);
1563    }
1564
1565    public Object JavaDoc __tojava__(Class JavaDoc cls) {
1566        Object JavaDoc o = null;
1567        try {
1568            o = file.__tojava__(cls);
1569        } catch (java.io.IOException JavaDoc exc) { }
1570        if (o == null)
1571            o = super.__tojava__(cls);
1572        return o;
1573    }
1574
1575    private static FileWrapper _setup(String JavaDoc name, String JavaDoc mode, int bufsize) {
1576        char c1 = ' ';
1577        char c2 = ' ';
1578        char c3 = ' ';
1579        int n = mode.length();
1580        for (int i = 0; i < n; i++) {
1581            if ("awrtb+".indexOf(mode.charAt(i)) < 0)
1582                throw Py.IOError("Unknown open mode:" + mode);
1583        }
1584        if (n > 0) {
1585            c1 = mode.charAt(0);
1586            if (n > 1) {
1587                c2 = mode.charAt(1);
1588                if (n > 2)
1589                    c3 = mode.charAt(2);
1590            }
1591        }
1592        String JavaDoc jmode = "r";
1593        if (c1 == 'r') {
1594            if (c2 == '+' || c3 == '+') jmode = "rw";
1595            else jmode = "r";
1596        }
1597        else if (c1 == 'w' || c1 == 'a') jmode = "rw";
1598        try {
1599            java.io.File JavaDoc f = new java.io.File JavaDoc(name);
1600            if (c1 == 'r') {
1601                if (!f.exists()) {
1602                    throw new java.io.IOException JavaDoc("No such file or directory: " + name);
1603                }
1604            }
1605            if (c1 == 'w') {
1606                // Hack to truncate the file without deleting it:
1607
// create a FileOutputStream for it and close it again.
1608
java.io.FileOutputStream JavaDoc fo = new java.io.FileOutputStream JavaDoc(f);
1609                fo.close();
1610                fo = null;
1611            }
1612            // What about bufsize?
1613
java.io.RandomAccessFile JavaDoc rfile =
1614                new java.io.RandomAccessFile JavaDoc(f, jmode);
1615            RFileWrapper iofile = new RFileWrapper(rfile);
1616            if (c1 == 'a')
1617                iofile.seek(0, 2);
1618            return iofile;
1619        } catch (java.io.IOException JavaDoc e) {
1620            throw Py.IOError(e);
1621        }
1622    }
1623
1624    final String JavaDoc file_read(int n) {
1625        if (closed)
1626            err_closed();
1627        StringBuffer JavaDoc data = new StringBuffer JavaDoc();
1628        try {
1629            while (n != 0) {
1630                String JavaDoc s = file.read(n);
1631                int len = s.length();
1632                if (len == 0)
1633                    break;
1634                data.append(s);
1635                if (n > 0) {
1636                    n -= len;
1637                    if (n <= 0)
1638                        break;
1639                }
1640            }
1641        } catch (java.io.IOException JavaDoc e) {
1642            throw Py.IOError(e);
1643        }
1644        return data.toString();
1645    }
1646
1647    public String JavaDoc read(int n) {
1648        return file_read(n);
1649    }
1650
1651    final String JavaDoc file_read() {
1652        return file_read(-1);
1653    }
1654
1655    public String JavaDoc read() {
1656        return file_read();
1657    }
1658
1659    final String JavaDoc file_readline(int max) {
1660        if (closed)
1661            err_closed();
1662        StringBuffer JavaDoc s = new StringBuffer JavaDoc();
1663        while (max < 0 || s.length() < max) {
1664            int c;
1665            try {
1666                c = file.read();
1667            } catch (java.io.IOException JavaDoc e) {
1668                throw Py.IOError(e);
1669            }
1670            if (c < 0)
1671                break;
1672            s.append((char)c);
1673            if ((char)c == '\n')
1674                break;
1675        }
1676        return s.toString();
1677    }
1678
1679    public String JavaDoc readline(int max) {
1680        return file_readline(max);
1681    }
1682
1683    public String JavaDoc readline() {
1684        return file_readline();
1685    }
1686
1687    final String JavaDoc file_readline() {
1688        return file_readline(-1);
1689    }
1690
1691    final PyObject file_readlines(int sizehint) {
1692        if (closed)
1693            err_closed();
1694        PyList list = new PyList();
1695        int bytesread = 0;
1696        for (;;) {
1697            String JavaDoc s = readline();
1698            int len = s.length();
1699            if (len == 0)
1700                // EOF
1701
break;
1702            bytesread += len;
1703            list.append(new PyString(s));
1704            if (sizehint > 0 && bytesread > sizehint)
1705                break;
1706        }
1707        return list;
1708    }
1709
1710    public PyObject readlines(int sizehint) {
1711        return file_readlines(sizehint);
1712    }
1713
1714    final PyObject file_readlines() {
1715        return file_readlines(0);
1716    }
1717
1718    public PyObject readlines() {
1719        return file_readlines();
1720    }
1721
1722    public PyObject __iter__() {
1723        return file___iter__();
1724    }
1725
1726    final PyObject file___iter__() {
1727        return this;
1728    }
1729
1730    public PyObject __iternext__() {
1731        return file___iternext__();
1732    }
1733
1734    final PyObject file___iternext__() {
1735        PyString s = new PyString(readline());
1736        if (s.__len__() == 0)
1737            return null;
1738        return s;
1739    }
1740
1741    final PyObject file_next() {
1742        PyObject ret = __iternext__();
1743        if (ret == null)
1744            throw Py.StopIteration("");
1745        return ret;
1746    }
1747
1748    public PyObject next() {
1749        return file_next();
1750    }
1751
1752    final PyObject file_xreadlines() {
1753        return this;
1754    }
1755
1756    public PyObject xreadlines() {
1757        return file_xreadlines();
1758    }
1759
1760    final void file_write(String JavaDoc s) {
1761        if (closed)
1762            err_closed();
1763        try {
1764            file.write(s);
1765            softspace = false;
1766        } catch (java.io.IOException JavaDoc e) {
1767            throw Py.IOError(e);
1768        }
1769    }
1770
1771    public void write(String JavaDoc s) {
1772        file_write(s);
1773    }
1774
1775    final void file_writelines(PyObject a) {
1776        PyObject iter = Py.iter(a, "writelines() requires an iterable argument");
1777
1778        PyObject item = null;
1779        while((item = iter.__iternext__()) != null) {
1780            if (!(item instanceof PyString))
1781                throw Py.TypeError("writelines() argument must be a " +
1782                                   "sequence of strings");
1783            write(item.toString());
1784        }
1785    }
1786
1787    public void writelines(PyObject a) {
1788        file_writelines(a);
1789    }
1790
1791    final long file_tell() {
1792        if (closed)
1793            err_closed();
1794        try {
1795            return file.tell();
1796        } catch (java.io.IOException JavaDoc e) {
1797            throw Py.IOError(e);
1798        }
1799    }
1800
1801    public long tell() {
1802        return file_tell();
1803    }
1804
1805    final void file_seek(long pos, int how) {
1806        if (closed)
1807            err_closed();
1808        try {
1809            file.seek(pos, how);
1810        } catch (java.io.IOException JavaDoc e) {
1811            throw Py.IOError(e);
1812        }
1813    }
1814
1815    public void seek(long pos, int how) {
1816        file_seek(pos, how);
1817    }
1818
1819    final void file_seek(long pos) {
1820        seek(pos, 0);
1821    }
1822
1823    public void seek(long pos) {
1824        file_seek(pos);
1825    }
1826
1827    final void file_flush() {
1828        if (closed)
1829            err_closed();
1830        try {
1831            file.flush();
1832        } catch (java.io.IOException JavaDoc e) {
1833            throw Py.IOError(e);
1834        }
1835    }
1836
1837    public void flush() {
1838        file_flush();
1839    }
1840
1841    final void file_close() {
1842        try {
1843            file.close();
1844        } catch (java.io.IOException JavaDoc e) {
1845            throw Py.IOError(e);
1846        }
1847        closed = true;
1848        file = new FileWrapper();
1849    }
1850
1851    public void close() {
1852        file_close();
1853    }
1854
1855    final void file_truncate() {
1856          try {
1857              file.truncate(file.tell());
1858          } catch (java.io.IOException JavaDoc e) {
1859              throw Py.IOError(e);
1860          }
1861     }
1862
1863    public void truncate() {
1864        file_truncate();
1865    }
1866
1867    final void file_truncate(long position) {
1868         try {
1869              file.truncate(position);
1870         } catch (java.io.IOException JavaDoc e) {
1871              throw Py.IOError(e);
1872         }
1873     }
1874
1875     public void truncate(long position) {
1876         file_truncate(position);
1877     }
1878
1879    // TBD: should this be removed? I think it's better to raise an
1880
// AttributeError than an IOError here.
1881
public PyObject fileno() {
1882        throw Py.IOError("fileno() is not supported in jpython");
1883    }
1884
1885    final String JavaDoc file_toString() {
1886        StringBuffer JavaDoc s = new StringBuffer JavaDoc("<");
1887        if (closed) {
1888            s.append("closed ");
1889        } else {
1890            s.append("open ");
1891        }
1892        s.append("file '");
1893        s.append(name);
1894        s.append("', mode '");
1895        s.append(mode);
1896        s.append("' ");
1897        s.append(Py.idstr(this));
1898        s.append(">");
1899        return s.toString();
1900    }
1901
1902    public String JavaDoc toString() {
1903        return file_toString();
1904    }
1905
1906    final int file___cmp__(PyObject o) {
1907        return super.__cmp__(o);
1908    }
1909
1910    final boolean file___nonzero__() {
1911        return super.__nonzero__();
1912    }
1913
1914    private void err_closed() {
1915        throw Py.ValueError("I/O operation on closed file");
1916    }
1917
1918    public String JavaDoc getMode() {
1919        return mode;
1920    }
1921
1922    public String JavaDoc getName() {
1923        return name;
1924    }
1925
1926    public boolean getClosed() {
1927        return closed;
1928    }
1929}
1930
Popular Tags