From 8c21999692adef512af246b0c7a037857ae5aea1 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Wed, 31 May 2017 10:25:54 -0500 Subject: Fall back on UTF-8 when an IO-like object's Charset isn't found. Fixes #319. --- ext/java/PsychParser.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'ext') diff --git a/ext/java/PsychParser.java b/ext/java/PsychParser.java index ec00501..b3e747e 100644 --- a/ext/java/PsychParser.java +++ b/ext/java/PsychParser.java @@ -158,10 +158,15 @@ public class PsychParser extends RubyObject { // fall back on IOInputStream, using default charset if (yaml.respondsTo("read")) { - Encoding enc = (yaml instanceof RubyIO) - ? ((RubyIO)yaml).getReadEncoding() - : UTF8Encoding.INSTANCE; - Charset charset = enc.getCharset(); + Charset charset = null; + if (yaml instanceof RubyIO) { + Encoding enc = ((RubyIO) yaml).getReadEncoding(); + charset = enc.getCharset(); + } + if (charset == null) { + // If we can't get it from the IO or it doesn't have a charset, fall back on UTF-8 + charset = UTF8Encoding.INSTANCE.getCharset(); + } return new StreamReader(new InputStreamReader(new IOInputStream(yaml), charset)); } else { throw runtime.newTypeError(yaml, runtime.getIO()); -- cgit v1.2.3