Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix spurious "too late to set encoding" errors.
  • Loading branch information
jnthn committed Jul 3, 2013
1 parent bfe5c17 commit f8409e7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -320,27 +320,17 @@ public static SixModelObject setencoding(SixModelObject obj, String encoding, Th

String charset = null;
if (encoding.equals("ascii"))
charset = "US-ASCII";
h.encoding = "US-ASCII";
else if (encoding.equals("iso-8859-1"))
charset = "ISO-8859-1";
h.encoding = "ISO-8859-1";
else if (encoding.equals("utf8"))
charset = "UTF-8";
h.encoding = "UTF-8";
else if (encoding.equals("utf16"))
charset = "UTF-16";
h.encoding = "UTF-16";
else if (encoding.equals("binary"))
charset = "ISO-8859-1"; /* Byte oriented... */
h.encoding = "ISO-8859-1"; /* Byte oriented... */
else
die_s("Unsupported encoding " + encoding, tc);

try {
if (h.is != null)
h.isr = new InputStreamReader(h.is, charset);
if (h.os != null)
h.osw = new OutputStreamWriter(h.os, charset);
}
catch (UnsupportedEncodingException e) {
die_s(e.getMessage(), tc);
}
}
else {
die_s("setencoding requires an object with the IOHandle REPR", tc);
Expand All @@ -366,7 +356,7 @@ public static String printfh(SixModelObject obj, String data, ThreadContext tc)
die_s("File handle is not opened for write", tc);
try {
if (h.osw == null)
h.osw = new OutputStreamWriter(h.os, "UTF-8");
h.osw = new OutputStreamWriter(h.os, h.encoding);
h.osw.write(data);
h.osw.flush();
}
Expand All @@ -393,7 +383,7 @@ public static String readlinefh(SixModelObject obj, ThreadContext tc) {
die_s("File handle is not opened for read", tc);
try {
if (h.isr == null)
h.isr = new InputStreamReader(h.is, "UTF-8");
h.isr = new InputStreamReader(h.is, h.encoding);
if (h.br == null)
h.br = new BufferedReader(h.isr);
String line = h.br.readLine();
Expand Down Expand Up @@ -448,7 +438,7 @@ public static String readallfh(SixModelObject obj, ThreadContext tc) {
die_s("File handle is not opened for read", tc);
try {
if (h.isr == null)
h.isr = new InputStreamReader(h.is, "UTF-8");
h.isr = new InputStreamReader(h.is, h.encoding);
if (h.br == null)
h.br = new BufferedReader(h.isr);

Expand Down
Expand Up @@ -26,6 +26,9 @@ public class IOHandleInstance extends SixModelObject {
/* The output stream; if null, we can't write to this. */
public OutputStream os;

/* The (Java) encoding name to use. */
public String encoding = "UTF-8";

/* These wrap the above streams and knows about encodings. If they
* are still null, the encoding can still be set.
*/
Expand Down

0 comments on commit f8409e7

Please sign in to comment.