📄 pyfile.java
字号:
// file.setLength(position); java.lang.reflect.Method m = file.getClass().getMethod( "setLength", new Class[] { Long.TYPE }); m.invoke(file, new Object[] { new Long(position) }); } catch (NoSuchMethodException exc) { super.truncate(position); } catch (SecurityException exc) { super.truncate(position); } catch (IllegalAccessException exc) { super.truncate(position); } catch (java.lang.reflect.InvocationTargetException exc) { if (exc.getTargetException() instanceof IOException) throw (IOException) exc.getTargetException(); super.truncate(position); } } public Object __tojava__(Class cls) throws IOException { if (OutputStream.class.isAssignableFrom(cls) && writing) return new FileOutputStream(file.getFD()); else if (InputStream.class.isAssignableFrom(cls) && reading) return new FileInputStream(file.getFD()); return super.__tojava__(cls); } } private static class TextWrapper extends FileWrapper { private FileWrapper file; private String sep; private boolean sep_is_nl; public TextWrapper(FileWrapper file) { this.file = file; sep = System.getProperty("line.separator"); sep_is_nl = (sep == "\n"); } public String read(int n) throws java.io.IOException { String s = this.file.read(n); int index = s.indexOf('\r'); if (index < 0) return s; StringBuffer buf = new StringBuffer(); int start = 0; int end = s.length(); do { buf.append(s.substring(start, index)); buf.append('\n'); start = index + 1; if (start < end && s.charAt(start) == '\n') start++; index = s.indexOf('\r', start); } while (index >= 0); buf.append(s.substring(start)); if (s.endsWith("\r") && file.available() > 0) { int c = file.read(); if (c != -1 && c != '\n') file.unread(c); } return buf.toString(); } public int read() throws java.io.IOException { int c = file.read(); if (c != '\r') return c; if (file.available() > 0) { c = file.read(); if (c != -1 && c != '\n') file.unread(c); } return '\n'; } public void write(String s) throws java.io.IOException { if (!sep_is_nl) { int index = s.indexOf('\n'); if (index >= 0) { StringBuffer buf = new StringBuffer(); int start = 0; do { buf.append(s.substring(start, index)); buf.append(sep); start = index + 1; index = s.indexOf('\n', start); } while (index >= 0); buf.append(s.substring(start)); s = buf.toString(); } } this.file.write(s); } public long tell() throws java.io.IOException { return file.tell(); } public void seek(long pos, int how) throws java.io.IOException { file.seek(pos, how); } public void flush() throws java.io.IOException { file.flush(); } public void close() throws java.io.IOException { file.close(); } public void truncate(long position) throws java.io.IOException { file.truncate(position); } public Object __tojava__(Class cls) throws IOException { return file.__tojava__(cls); } } public String name; public String mode; public boolean softspace; public boolean closed; private FileWrapper file; private static java.io.InputStream _pb(java.io.InputStream s, String mode) { if (mode.indexOf('b') < 0) { try { s = (java.io.PushbackInputStream)s; } catch (ClassCastException e) { s = new java.io.PushbackInputStream(s); } } return s; } public PyFile(FileWrapper file, String name, String mode) { file.setMode(mode); this.name = name; this.mode = mode; this.softspace = false; this.closed = false; if (mode.indexOf('b') < 0) this.file = new TextWrapper(file); else this.file = file; } public PyFile(java.io.InputStream istream, java.io.OutputStream ostream, String name, String mode) { this(new IOStreamWrapper(_pb(istream, mode), ostream), name, mode); } public PyFile(java.io.InputStream istream, java.io.OutputStream ostream, String name) { this(istream, ostream, name, "r+"); } public PyFile(java.io.InputStream istream, java.io.OutputStream ostream) { this(istream, ostream, "<???>", "r+"); } public PyFile(java.io.InputStream istream, String name, String mode) { this(new InputStreamWrapper(_pb(istream, mode)), name, mode); } public PyFile(java.io.InputStream istream, String name) { this(istream, name, "r"); } public PyFile(java.io.InputStream istream) { this(istream, "<???>", "r"); } public PyFile(java.io.OutputStream ostream, String name, String mode) { this(new OutputStreamWrapper(ostream), name, mode); } public PyFile(java.io.OutputStream ostream, String name) { this(ostream, name, "w"); } public PyFile(java.io.OutputStream ostream) { this(ostream, "<???>", "w"); } public PyFile(java.io.Writer ostream, String name, String mode) { this(new WriterWrapper(ostream), name, mode); } public PyFile(java.io.Writer ostream, String name) { this(ostream, name, "w"); } public PyFile(java.io.Writer ostream) { this(ostream, "<???>", "w"); } public PyFile(java.io.RandomAccessFile file, String name, String mode) { this(new RFileWrapper(file), name, mode); } public PyFile(java.io.RandomAccessFile file, String name) { this(file, name, "r+"); } public PyFile(java.io.RandomAccessFile file) { this(file, "<???>", "r+"); } public PyFile(String name, String mode, int bufsize) { this(_setup(name, mode, bufsize), name, mode); } public void __setattr__(String name, PyObject value) { // softspace is the only writeable file object attribute if (name == "softspace") softspace = value.__nonzero__(); else if (name == "mode" || name == "closed" || name == "name") throw Py.TypeError("readonly attribute: " + name); else throw Py.AttributeError(name); } public Object __tojava__(Class cls) { Object o = null; try { o = file.__tojava__(cls); } catch (java.io.IOException exc) { } if (o == null) o = super.__tojava__(cls); return o; } private static FileWrapper _setup(String name, String mode, int bufsize) { char c1 = ' '; char c2 = ' '; char c3 = ' '; int n = mode.length(); for (int i = 0; i < n; i++) { if ("awrtb+".indexOf(mode.charAt(i)) < 0) throw Py.IOError("Unknown open mode:" + mode); } if (n > 0) { c1 = mode.charAt(0); if (n > 1) { c2 = mode.charAt(1); if (n > 2) c3 = mode.charAt(2); } } String jmode = "r"; if (c1 == 'r') { if (c2 == '+' || c3 == '+') jmode = "rw"; else jmode = "r"; } else if (c1 == 'w' || c1 == 'a') jmode = "rw"; try { java.io.File f = new java.io.File(name); if (c1 == 'w') { // Hack to truncate the file without deleting it: // create a FileOutputStream for it and close it again. java.io.FileOutputStream fo = new java.io.FileOutputStream(f); fo.close(); fo = null; } // What about bufsize? java.io.RandomAccessFile rfile = new java.io.RandomAccessFile(f, jmode); RFileWrapper iofile = new RFileWrapper(rfile); if (c1 == 'a') iofile.seek(0, 2); return iofile; } catch (java.io.IOException e) { throw Py.IOError(e); } } public PyString read(int n) { if (closed) err_closed(); StringBuffer data = new StringBuffer(); try { while (n != 0) { String s = file.read(n); int len = s.length(); if (len == 0) break; data.append(s); if (n > 0) { n -= len; if (n <= 0) break; } } } catch (java.io.IOException e) { throw Py.IOError(e); } return new PyString(data.toString()); } public PyString read() { return read(-1); } public PyString readline(int max) { if (closed) err_closed(); StringBuffer s = new StringBuffer(); while (max < 0 || s.length() < max) { int c; try { c = file.read(); } catch (java.io.IOException e) { throw Py.IOError(e); } if (c < 0) break; s.append((char)c); if ((char)c == '\n') break; } return new PyString(s.toString()); } public PyString readline() { return readline(-1); } public PyObject readlines(int sizehint) { if (closed) err_closed(); PyList list = new PyList(); int bytesread = 0; for (;;) { PyString s = readline(); int len = s.__len__(); if (len == 0) // EOF break; bytesread += len; list.append(s); if (sizehint > 0 && bytesread > sizehint) break; } return list; } public PyObject readlines() { return readlines(0); } public PyObject __iter__() { return this; } public PyObject __iternext__() { PyString s = readline(); if (s.__len__() == 0) return null; return s; } public PyObject next() { PyObject ret = __iternext__(); if (ret == null) throw Py.StopIteration(""); return ret; } public PyObject xreadlines() { return this; } public void write(String s) { if (closed) err_closed(); try { file.write(s); softspace = false; } catch (java.io.IOException e) { throw Py.IOError(e); } } public void writelines(PyObject a) { PyObject item = null; for (int i = 0; (item = a.__finditem__(i)) != null; i++) { if (!(item instanceof PyString)) throw Py.TypeError("writelines() argument must be a " + "sequence of strings"); write(item.toString()); } } public long tell() { if (closed) err_closed(); try { return file.tell(); } catch (java.io.IOException e) { throw Py.IOError(e); } } public void seek(long pos, int how) { if (closed) err_closed(); try { file.seek(pos, how); } catch (java.io.IOException e) { throw Py.IOError(e); } } public void seek(long pos) { seek(pos, 0); } public void flush() { if (closed) err_closed(); try { file.flush(); } catch (java.io.IOException e) { throw Py.IOError(e); } } public void close() { try { file.close(); } catch (java.io.IOException e) { throw Py.IOError(e); } closed = true; file = new FileWrapper(); } public void truncate() { try { file.truncate(file.tell()); } catch (java.io.IOException e) { throw Py.IOError(e); } } public void truncate(long position) { try { file.truncate(position); } catch (java.io.IOException e) { throw Py.IOError(e); } } // TBD: should this be removed? I think it's better to raise an // AttributeError than an IOError here. public PyObject fileno() { throw Py.IOError("fileno() is not supported in jpython"); } public String toString() { return "<file " + name + ", mode " + mode + " " + Py.idstr(this) + ">"; } private void err_closed() { throw Py.ValueError("I/O operation on closed file"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -