Skip to content

Commit f8409e7

Browse files
committed
Fix spurious "too late to set encoding" errors.
1 parent bfe5c17 commit f8409e7

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -320,27 +320,17 @@ public static SixModelObject setencoding(SixModelObject obj, String encoding, Th
320320

321321
String charset = null;
322322
if (encoding.equals("ascii"))
323-
charset = "US-ASCII";
323+
h.encoding = "US-ASCII";
324324
else if (encoding.equals("iso-8859-1"))
325-
charset = "ISO-8859-1";
325+
h.encoding = "ISO-8859-1";
326326
else if (encoding.equals("utf8"))
327-
charset = "UTF-8";
327+
h.encoding = "UTF-8";
328328
else if (encoding.equals("utf16"))
329-
charset = "UTF-16";
329+
h.encoding = "UTF-16";
330330
else if (encoding.equals("binary"))
331-
charset = "ISO-8859-1"; /* Byte oriented... */
331+
h.encoding = "ISO-8859-1"; /* Byte oriented... */
332332
else
333333
die_s("Unsupported encoding " + encoding, tc);
334-
335-
try {
336-
if (h.is != null)
337-
h.isr = new InputStreamReader(h.is, charset);
338-
if (h.os != null)
339-
h.osw = new OutputStreamWriter(h.os, charset);
340-
}
341-
catch (UnsupportedEncodingException e) {
342-
die_s(e.getMessage(), tc);
343-
}
344334
}
345335
else {
346336
die_s("setencoding requires an object with the IOHandle REPR", tc);
@@ -366,7 +356,7 @@ public static String printfh(SixModelObject obj, String data, ThreadContext tc)
366356
die_s("File handle is not opened for write", tc);
367357
try {
368358
if (h.osw == null)
369-
h.osw = new OutputStreamWriter(h.os, "UTF-8");
359+
h.osw = new OutputStreamWriter(h.os, h.encoding);
370360
h.osw.write(data);
371361
h.osw.flush();
372362
}
@@ -393,7 +383,7 @@ public static String readlinefh(SixModelObject obj, ThreadContext tc) {
393383
die_s("File handle is not opened for read", tc);
394384
try {
395385
if (h.isr == null)
396-
h.isr = new InputStreamReader(h.is, "UTF-8");
386+
h.isr = new InputStreamReader(h.is, h.encoding);
397387
if (h.br == null)
398388
h.br = new BufferedReader(h.isr);
399389
String line = h.br.readLine();
@@ -448,7 +438,7 @@ public static String readallfh(SixModelObject obj, ThreadContext tc) {
448438
die_s("File handle is not opened for read", tc);
449439
try {
450440
if (h.isr == null)
451-
h.isr = new InputStreamReader(h.is, "UTF-8");
441+
h.isr = new InputStreamReader(h.is, h.encoding);
452442
if (h.br == null)
453443
h.br = new BufferedReader(h.isr);
454444

src/vm/jvm/runtime/org/perl6/nqp/sixmodel/reprs/IOHandleInstance.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public class IOHandleInstance extends SixModelObject {
2626
/* The output stream; if null, we can't write to this. */
2727
public OutputStream os;
2828

29+
/* The (Java) encoding name to use. */
30+
public String encoding = "UTF-8";
31+
2932
/* These wrap the above streams and knows about encodings. If they
3033
* are still null, the encoding can still be set.
3134
*/

0 commit comments

Comments
 (0)