Skip to content

Commit

Permalink
Fix up how our 223 engine encodes input and decodes output.
Browse files Browse the repository at this point in the history
* Incoming scripts should be decoded from String to byte[] using
  default internal encoding, rather than trusting JDK's
  file.encoding to be appropriate.
* Outgoing streams wrapping writers should decode strings based
  on current default internal encoding.

Fixes #2403
headius committed Mar 14, 2016
1 parent 949216a commit e02641b
Showing 3 changed files with 30 additions and 4 deletions.
30 changes: 27 additions & 3 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -174,6 +174,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.ClosedChannelException;
import java.nio.charset.Charset;
import java.security.AccessControlException;
import java.security.SecureRandom;
import java.util.ArrayList;
@@ -491,7 +492,7 @@ public IRubyObject evalScriptlet(String script, DynamicScope scope) {
* @return The last value of the script
*/
public IRubyObject executeScript(String script, String filename) {
byte[] bytes = script.getBytes();
byte[] bytes = encodeToBytes(script);

RootNode root = (RootNode) parseInline(new ByteArrayInputStream(bytes), filename, null);
ThreadContext context = getCurrentContext();
@@ -692,7 +693,7 @@ public IRubyObject runWithGetsLoop(RootNode scriptNode, boolean printing, boolea
private RootNode addGetsLoop(RootNode oldRoot, boolean printing, boolean processLineEndings, boolean split) {
ISourcePosition pos = oldRoot.getPosition();
BlockNode newBody = new BlockNode(pos);
newBody.add(new GlobalAsgnNode(pos, "$/", new StrNode(pos, new ByteList(getInstanceConfig().getRecordSeparator().getBytes()))));
newBody.add(new GlobalAsgnNode(pos, "$/", new StrNode(pos, ((RubyString) globalVariables.get("$/")).getByteList())));

if (processLineEndings) newBody.add(new GlobalAsgnNode(pos, "$\\", new GlobalVarNode(pos, "$/")));

@@ -2786,7 +2787,16 @@ private void setupSourceEncoding(ParserConfiguration parserConfig, Encoding defa

public Node parseEval(String content, String file, DynamicScope scope, int lineNumber) {
addEvalParseToStats();
return parser.parse(file, content.getBytes(), scope, new ParserConfiguration(this, lineNumber, false, false, config));

return parser.parse(file, encodeToBytes(content), scope, new ParserConfiguration(this, lineNumber, false, false, config));
}

private byte[] encodeToBytes(String string) {
Charset charset = getDefaultCharset();

byte[] bytes = charset == null ? string.getBytes() : string.getBytes(charset);

return bytes;
}

@Deprecated
@@ -2848,6 +2858,20 @@ public void setDefaultExternalEncoding(Encoding defaultExternalEncoding) {
this.defaultExternalEncoding = defaultExternalEncoding;
}

/**
* Get the default java.nio.charset.Charset for the current default internal encoding.
*/
public Charset getDefaultCharset() {
Encoding enc = getDefaultInternalEncoding();
if (enc == null) {
enc = UTF8Encoding.INSTANCE;
}

Charset charset = enc.getCharset();

return charset;
}

public EncodingService getEncodingService() {
return encodingService;
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/embed/jsr223/Utils.java
Original file line number Diff line number Diff line change
@@ -175,7 +175,7 @@ private static void setErrorWriter(ScriptingContainer container, Writer writer)
}

private static RubyIO getRubyIO(Ruby runtime, Writer writer) throws IOException, BadDescriptorException {
PrintStream pstream = new PrintStream(new WriterOutputStream(writer), true);
PrintStream pstream = new PrintStream(new WriterOutputStream(writer, runtime.getDefaultCharset().name()), true);
RubyIO io = new RubyIO(runtime, pstream, false);
boolean locked = io.getOpenFile().lock();
try {
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@
import javax.script.Invocable;
import javax.script.Compilable;
import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;

0 comments on commit e02641b

Please sign in to comment.