Issue1061

classification
Title: PyFile should accept a reader
Type: behaviour Severity: minor
Components: Core Versions: 2.2.1rc1
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: colinhevans, fwierzbicki, zyasoft
Priority: normal Keywords:

Created on 2008-06-20.19:11:23 by colinhevans, last changed 2009-03-22.03:40:58 by zyasoft.

Messages
msg3295 (view) Author: Colin Evans (colinhevans) Date: 2008-06-20.19:11:13
PyFile accepts a Writer and should accept a Reader as well, as there
isn't any other way to force a PyFile to use a specific encoding.

As an aside, a lot of Jython code relies on the "platform encoding"
(i.e. PyString.from_bytes) without allowing an encoding to be specified.
msg3296 (view) Author: Colin Evans (colinhevans) Date: 2008-06-20.19:33:45
here's the code that is required:

    private static class ReaderWrapper extends FileWrapper {
        Reader istream;

        public ReaderWrapper(Reader r) {
            istream = r;
        }

        public String read(int n) throws IOException {
            if (n == 0)
                // nothing to do
                return "";
            if (n < 0) {
                // read until we hit EOF
                char buf[] = new char[1024];
                StringBuffer sbuf = new StringBuffer();
                for (int read=0; read >= 0; read=istream.read(buf))
                    sbuf.append(buf, 0, read);
                return sbuf.toString();
            }
            // read the next chunk available, but make sure it's at least
            // one byte so as not to trip the `empty string' return value
            // test done by the caller
            //int avail = istream.available();
            //n = (n > avail) ? n : avail;
            char buf[] = new char[n];
            int read = istream.read(buf);
            if (read < 0)
                // EOF encountered
                return "";
            return new String(buf);
        }

        public int read() throws IOException {
            return istream.read();
        }

        public int available() throws IOException {
            return 0;
        }

        public void close() throws IOException {
            istream.close();
        }

        public Object __tojava__(Class cls) throws IOException {
            if (InputStream.class.isAssignableFrom(cls))
                return istream;
            return null;
        }
    }
msg3529 (view) Author: Jim Baker (zyasoft) Date: 2008-09-13.19:48:59
This looks good, but could you resubmit this as a patch (with svn diff),
so we can more readily evaluate. Any change will be against 2.5.
msg4329 (view) Author: Jim Baker (zyasoft) Date: 2009-03-22.03:40:58
We will not be using Reader/Writer for PyFile, since it's byte-oriented
and R/W are character-oriented.
History
Date User Action Args
2009-03-22 03:40:59zyasoftsetstatus: open -> closed
resolution: wont fix
messages: + msg4329
2008-12-17 19:53:26fwierzbickisetnosy: + fwierzbicki
2008-12-17 19:53:13fwierzbickisetpriority: normal
2008-09-13 19:48:59zyasoftsetassignee: zyasoft
messages: + msg3529
nosy: + zyasoft
2008-06-20 19:33:47colinhevanssetmessages: + msg3296
2008-06-20 19:11:23colinhevanscreate