Skip to content

Commit

Permalink
Showing 11 changed files with 64 additions and 12 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ before_install:

before_script:
- unset GEM_PATH GEM_HOME IRBRC JRUBY_OPTS
- unset _JAVA_OPTIONS # travis/travis#8408
- export PATH="`pwd`/bin:$PATH"
- echo $HOME
- echo $JAVA_OPTS
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@
import org.jruby.util.MRIRecursionGuard;
import org.jruby.util.StrptimeParser;
import org.jruby.util.StrptimeToken;
import org.jruby.util.io.EncodingUtils;
import org.objectweb.asm.util.TraceClassVisitor;

import jnr.constants.Constant;
@@ -2798,7 +2799,7 @@ public Charset getDefaultCharset() {
enc = UTF8Encoding.INSTANCE;
}

Charset charset = enc.getCharset();
Charset charset = EncodingUtils.charsetForEncoding(enc);

return charset;
}
1 change: 0 additions & 1 deletion core/src/main/java/org/jruby/RubyModule.java
Original file line number Diff line number Diff line change
@@ -2492,7 +2492,6 @@ final void populateInstanceMethodNames(final Set<String> seen, final RubyArray a
protected void addMethodSymbols(Ruby runtime, Set<String> seen, RubyArray ary, boolean not, Visibility visibility) {
for (Map.Entry entry : getMethods().entrySet()) {
String methodName = (String) entry.getKey();

if (! seen.contains(methodName)) {
seen.add(methodName);

4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
@@ -538,7 +538,7 @@ public static RubyString newInternalFromJavaExternal(Ruby runtime, String str) {
// Ruby internal
Encoding internal = runtime.getDefaultInternalEncoding();
Charset rubyInt = null;
if ( internal != null ) rubyInt = internal.getCharset();
if ( internal != null ) rubyInt = EncodingUtils.charsetForEncoding(internal);

if ( rubyInt == null ) {
Encoding javaExtEncoding = runtime.getEncodingService().getJavaDefault();
@@ -5610,7 +5610,7 @@ public String getUnicodeValue() {

public static ByteList encodeBytelist(CharSequence value, Encoding encoding) {

Charset charset = encoding.getCharset();
Charset charset = EncodingUtils.charsetForEncoding(encoding);

// if null charset, let our transcoder handle it
if (charset == null) {
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ext/set/EnumerableExt.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* License Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/ir/operands/Symbol.java
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
import org.jruby.ir.persistence.IRWriterEncoder;
import org.jruby.runtime.ThreadContext;
import org.jruby.util.ByteList;
import org.jruby.util.io.EncodingUtils;

public class Symbol extends ImmutableLiteral {
public static final Symbol KW_REST_ARG_DUMMY = new Symbol("", ASCIIEncoding.INSTANCE);
@@ -18,7 +19,7 @@ public class Symbol extends ImmutableLiteral {
public Symbol(String name, Encoding encoding) {
super();

this.bytes = new ByteList(name.getBytes(encoding.getCharset()), encoding);
this.bytes = new ByteList(name.getBytes(EncodingUtils.charsetForEncoding(encoding)), encoding);
}

public Symbol(ByteList bytes) {
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/lexer/LexingCommon.java
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import org.jruby.lexer.yacc.StackState;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;
import org.jruby.util.io.EncodingUtils;

/**
* Code and constants common to both ripper and main parser.
@@ -174,7 +175,7 @@ public String createTokenString(int start) {
public String createAsEncodedString(byte[] bytes, int start, int length, Encoding encoding) {
// FIXME: We should be able to move some faster non-exception cache using Encoding.isDefined
try {
Charset charset = getEncoding().getCharset();
Charset charset = EncodingUtils.charsetForEncoding(getEncoding());
if (charset != null) {
if (charset == RubyEncoding.UTF8) {
return RubyEncoding.decodeUTF8(bytes, start, length);
4 changes: 3 additions & 1 deletion core/src/main/java/org/jruby/management/BeanManagerImpl.java
Original file line number Diff line number Diff line change
@@ -84,7 +84,9 @@ public boolean tryShutdownAgent() {

public boolean tryRestartAgent() {
try {
Class.forName("sun.management.Agent").getDeclaredMethod("startAgent").invoke(null);
Class agent = Class.forName("sun.management.Agent");
Method start = agent.getMethod("startAgent");
start.invoke(null);
return true;
} catch (Exception e) {
return false;
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/runtime/Helpers.java
Original file line number Diff line number Diff line change
@@ -2344,7 +2344,7 @@ public static String symbolBytesToString(ByteList value) {
if (encoding == USASCIIEncoding.INSTANCE || encoding == ASCIIEncoding.INSTANCE) {
return value.toString(); // raw
} else if (encoding instanceof UnicodeEncoding) {
return new String(value.getUnsafeBytes(), value.getBegin(), value.getRealSize(), value.getEncoding().getCharset());
return new String(value.getUnsafeBytes(), value.getBegin(), value.getRealSize(), EncodingUtils.charsetForEncoding(value.getEncoding()));
} else {
return value.toString(); // raw
}
@@ -2388,7 +2388,7 @@ public static String decodeByteList(Ruby runtime, ByteList value) {
* @return the decoded string
*/
public static String byteListToString(final ByteList bytes) {
final Charset charset = bytes.getEncoding().getCharset();
final Charset charset = EncodingUtils.charsetForEncoding(bytes.getEncoding());
if ( charset != null ) {
return new String(bytes.getUnsafeBytes(), bytes.getBegin(), bytes.getRealSize(), charset);
}
24 changes: 23 additions & 1 deletion core/src/main/java/org/jruby/util/io/EncodingUtils.java
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@
import org.jruby.util.StringSupport;
import org.jruby.util.TypeConverter;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -1332,7 +1333,7 @@ public static ByteList transcodeString(String string, Encoding toEncoding, int e

EConv ec = TranscoderDB.open(encoding.getName(), toEncoding.getName(), ecflags);

byte[] inBytes = string.getBytes(encoding.getCharset());
byte[] inBytes = string.getBytes(EncodingUtils.charsetForEncoding(encoding));
Ptr inPos = new Ptr(0);

int inStop = inBytes.length;
@@ -2242,6 +2243,27 @@ else if (c < 0x10000) {
return buf.length;
}

/**
* Get an appropriate Java Charset for the given Encoding.
*
* This works around a bug in jcodings where it would return null as the charset for encodings that should have
* a match, like Windows-1252. This method is equivalent to enc.getCharset in jcodings 1.0.25 and higher.
*
* See https://github.com/jruby/jruby/issues/4716 for more information.
*
* @param enc the encoding for which to get a matching charset
* @return the matching charset
*/
public static Charset charsetForEncoding(Encoding enc) {
Charset charset = enc.getCharset();

if (charset == null) {
charset = Charset.forName(enc.toString());
}

return charset;
}

public static int encCodelen(ThreadContext context, int c, Encoding enc) {
int n = enc.codeToMbcLength(c);
if (n == 0) {
25 changes: 25 additions & 0 deletions lib/ruby/stdlib/yaml/store.rb
Original file line number Diff line number Diff line change
@@ -108,4 +108,29 @@ def open_and_lock_file(filename, read_only)
return file
end
end

# FIXME: These two constants and the method open_and_lock_file should not
# be in this file (taken from pstore). See #4779.
RDWR_ACCESS = {mode: IO::RDWR | IO::CREAT | IO::BINARY, encoding: Encoding::UTF_8}.freeze
RD_ACCESS = {mode: IO::RDONLY | IO::BINARY, encoding: Encoding::UTF_8}.freeze
def open_and_lock_file(filename, read_only)
if read_only
begin
file = File.new(filename, RD_ACCESS)
begin
file.flock(File::LOCK_SH)
return file
rescue
file.close
raise
end
rescue Errno::ENOENT
return nil
end
else
file = File.new(filename, RDWR_ACCESS)
file.flock(File::LOCK_EX)
return file
end
end
end

0 comments on commit fd93ca5

Please sign in to comment.