Permalink
Browse files

DefaultExternal internally should be set sooner than later. RubyIO sh…

…ould just use Encoding objects and binmode should convert from US-ASCII to ASCII-8bit
  • Loading branch information...
1 parent 7632457 commit d54191127fa4589e41d2391d4be135472edac9f0 @enebo enebo committed with yokolet Jan 7, 2011
Showing with 29 additions and 27 deletions.
  1. +3 −0 src/org/jruby/Ruby.java
  2. +26 −27 src/org/jruby/RubyIO.java
View
@@ -128,6 +128,7 @@
import java.io.File;
import java.util.EnumSet;
import java.util.concurrent.atomic.AtomicLong;
+import org.jcodings.specific.USASCIIEncoding;
import org.jruby.RubyInstanceConfig.CompileMode;
import org.jruby.ast.RootNode;
import org.jruby.ast.executable.RuntimeCache;
@@ -1181,6 +1182,8 @@ private void initEncodings() {
Encoding loadedEncoding = encodingService.loadEncoding(ByteList.create(encoding));
if (loadedEncoding == null) throw new MainExitException(1, "unknown encoding name - " + encoding);
setDefaultExternalEncoding(loadedEncoding);
+ } else {
+ setDefaultExternalEncoding(USASCIIEncoding.INSTANCE);
}
encoding = config.getInternalEncoding();
View
@@ -92,6 +92,8 @@
import org.jruby.util.io.SelectorFactory;
import java.nio.channels.spi.SelectorProvider;
+import org.jcodings.specific.ASCIIEncoding;
+import org.jcodings.specific.USASCIIEncoding;
import static org.jruby.CompatVersion.*;
import static org.jruby.RubyEnumerator.enumeratorize;
@@ -629,8 +631,7 @@ private IRubyObject getline(Ruby runtime, ByteList separator, long limit, ByteLi
incrementLineno(runtime, myOpenFile);
return str;
} else if (limit == 0) {
- return RubyString.newEmptyString(runtime,
- RubyEncoding.getEncodingFromObject(runtime, externalEncoding));
+ return RubyString.newEmptyString(runtime, externalEncoding);
} else if (separator.length() == 1 && limit < 0) {
return getlineFast(runtime, separator.get(0) & 0xFF, cache);
} else {
@@ -718,9 +719,7 @@ private IRubyObject getline(Ruby runtime, ByteList separator, long limit, ByteLi
}
private Encoding getExternalEncoding(Ruby runtime) {
- return externalEncoding != null ?
- RubyEncoding.getEncodingFromObject(runtime, externalEncoding) :
- runtime.getDefaultExternalEncoding();
+ return externalEncoding != null ? externalEncoding : runtime.getDefaultExternalEncoding();
}
private RubyString makeString(Ruby runtime, ByteList buffer, boolean isCached) {
@@ -1001,12 +1000,16 @@ protected Stream fdopen(ChannelDescriptor existingDescriptor, ModeFlags modes) t
@JRubyMethod(compat = RUBY1_9)
public IRubyObject external_encoding(ThreadContext context) {
- return externalEncoding != null ? externalEncoding : RubyEncoding.getDefaultExternal(context.getRuntime());
+ return externalEncoding != null ?
+ context.getRuntime().getEncodingService().getEncoding(externalEncoding) :
+ context.getRuntime().getNil();
}
@JRubyMethod(compat = RUBY1_9)
public IRubyObject internal_encoding(ThreadContext context) {
- return internalEncoding != null ? internalEncoding : context.getRuntime().getNil();
+ return internalEncoding != null ?
+ context.getRuntime().getEncodingService().getEncoding(internalEncoding) :
+ context.getRuntime().getNil();
}
@JRubyMethod(compat=RUBY1_9)
@@ -1035,25 +1038,20 @@ private void setExternalEncoding(ThreadContext context, IRubyObject encoding) {
private void setInternalEncoding(ThreadContext context, IRubyObject encoding) {
- IRubyObject internalEncodingOption = getEncodingCommon(context, encoding);
+ Encoding internalEncodingOption = getEncodingCommon(context, encoding);
- if (internalEncodingOption.toString().equals(external_encoding(context).toString())) {
+ if (internalEncodingOption == externalEncoding) {
context.getRuntime().getWarnings().warn("Ignoring internal encoding " + encoding
+ ": it is identical to external encoding " + external_encoding(context));
} else {
internalEncoding = internalEncodingOption;
}
}
- private IRubyObject getEncodingCommon(ThreadContext context, IRubyObject encoding) {
- IRubyObject rubyEncoding = null;
- if (encoding instanceof RubyEncoding) {
- rubyEncoding = encoding;
- } else {
- Encoding encodingObj = RubyEncoding.getEncodingFromObject(context.getRuntime(), encoding);
- rubyEncoding = RubyEncoding.convertEncodingToRubyEncoding(context.getRuntime(), encodingObj);
- }
- return rubyEncoding;
+ private Encoding getEncodingCommon(ThreadContext context, IRubyObject encoding) {
+ if (encoding instanceof RubyEncoding) return ((RubyEncoding) encoding).getEncoding();
+
+ return RubyEncoding.getEncodingFromObject(context.getRuntime(), encoding);
}
@JRubyMethod(required = 1, optional = 2, meta = true)
@@ -1166,8 +1164,11 @@ public IRubyObject autoclose_set(ThreadContext context, IRubyObject autoclose) {
@JRubyMethod(name = "binmode")
public IRubyObject binmode() {
- if (isClosed()) {
- throw getRuntime().newIOError("closed stream");
+ if (isClosed()) throw getRuntime().newIOError("closed stream");
+
+ Ruby runtime = getRuntime();
+ if (getExternalEncoding(runtime) == USASCIIEncoding.INSTANCE) {
+ externalEncoding = ASCIIEncoding.INSTANCE;
}
openFile.setBinmode();
return this;
@@ -2489,7 +2490,7 @@ private IRubyObject getPartial(ThreadContext context, IRubyObject[] args, boolea
ByteList newBuf = empty ? ByteList.EMPTY_BYTELIST.dup() : buf;
if (externalEncoding != null) { // TODO: Encapsulate into something more central (when adding trancoding)
- newBuf.setEncoding(RubyEncoding.getEncodingFromObject(runtime, externalEncoding));
+ newBuf.setEncoding(externalEncoding);
}
string.view(newBuf);
@@ -2988,9 +2989,7 @@ public IRubyObject each_charInternal(final ThreadContext context, final Block bl
byte c = (byte)RubyNumeric.fix2int(ch);
int n = runtime.getKCode().getEncoding().length(c);
RubyString str = runtime.newString();
- if (externalEncoding != null) {
- str.setEncoding(RubyEncoding.getEncodingFromObject(runtime, externalEncoding));
- }
+ if (externalEncoding != null) str.setEncoding(externalEncoding);
str.setTaint(true);
str.cat(c);
@@ -3287,7 +3286,7 @@ public static IRubyObject select_static(ThreadContext context, Ruby runtime, IRu
}
}
- if (r.size() == 0 && w.size() == 0 && e.size() == 0) {
+ if (r.isEmpty() && w.isEmpty() && e.isEmpty()) {
return runtime.getNil();
}
@@ -3975,6 +3974,6 @@ public IRubyObject readline(ThreadContext context, IRubyObject[] args) {
protected OpenFile openFile;
protected List<RubyThread> blockingThreads;
- protected IRubyObject externalEncoding;
- protected IRubyObject internalEncoding;
+ protected Encoding externalEncoding;
+ protected Encoding internalEncoding;
}

0 comments on commit d541911

Please sign in to comment.