Skip to content

Commit

Permalink
Fix GzipReader encoding on 1.9 mode (produced encoding == null String)
Browse files Browse the repository at this point in the history
It's a fix for "-J-ea pointed out we were creating a bad ByteList with
no encoding in 1.9 mode..." reported by Tom.

The actual bug is from 1 line, that I forgot to set default
externalEncoding to runtime.getDefaultExternalEncoding()
But the problem is from name confusion.  Use proper names, readEncoding
and writeEncoding for enc, enc2 in Ruby's IO subsystem.
  • Loading branch information
Hiroshi Nakamura committed Feb 1, 2012
1 parent f53a857 commit 84e3e3f
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/org/jruby/ext/zlib/RubyZlib.java
Expand Up @@ -1137,25 +1137,25 @@ public static RubyGzipFile newInstance(IRubyObject recv, Block block) {
protected RubyString nullFreeComment;
protected IRubyObject realIo;
protected RubyTime mtime;
protected Encoding externalEncoding;
protected Encoding internalEncoding;
protected Encoding readEncoding; // enc
protected Encoding writeEncoding; // enc2
protected boolean sync = false;

public RubyGzipFile(Ruby runtime, RubyClass type) {
super(runtime, type);
mtime = RubyTime.newTime(runtime, new DateTime());
externalEncoding = null;
internalEncoding = null;
readEncoding = runtime.getDefaultExternalEncoding();
writeEncoding = null;
}

// c: gzfile_newstr
protected RubyString newStr(Ruby runtime, ByteList value) {
if (runtime.is1_9()) {
if (externalEncoding == null) {
return RubyString.newString(runtime, value, internalEncoding);
if (writeEncoding == null) {
return RubyString.newString(runtime, value, readEncoding);
}
return RubyString.newStringNoCopy(runtime, RubyString.transcode(
runtime.getCurrentContext(), value, externalEncoding, internalEncoding,
runtime.getCurrentContext(), value, readEncoding, writeEncoding,
runtime.getNil()));
} else {
return RubyString.newString(runtime, value);
Expand Down Expand Up @@ -1322,8 +1322,8 @@ public IRubyObject initialize19(IRubyObject[] args) {
if (!opt.isNil()) {
RubyIO.EncodingOption enc = RubyIO.extractEncodingOptions(opt);
if (enc != null) {
externalEncoding = enc.getExternalEncoding();
internalEncoding = enc.getInternalEncoding();
readEncoding = enc.getExternalEncoding();
writeEncoding = enc.getInternalEncoding();
}
}
}
Expand Down Expand Up @@ -1401,26 +1401,26 @@ private IRubyObject internalSepGets(ByteList sep) throws IOException {
return internalSepGets(sep, -1);
}

private ByteList newExternalByteList() {
private ByteList newReadByteList() {
ByteList byteList = new ByteList();
if (externalEncoding != null) byteList.setEncoding(externalEncoding);
if (readEncoding != null) byteList.setEncoding(readEncoding);
return byteList;
}

private ByteList newExternalByteList(int size) {
private ByteList newReadByteList(int size) {
ByteList byteList = new ByteList(size);
if (externalEncoding != null) byteList.setEncoding(externalEncoding);
if (readEncoding != null) byteList.setEncoding(readEncoding);
return byteList;
}

private ByteList newExternalByteList(byte[] buffer, int start, int length, boolean copy) {
private ByteList newReadByteList(byte[] buffer, int start, int length, boolean copy) {
ByteList byteList = new ByteList(buffer, start, length, copy);
if (externalEncoding != null) byteList.setEncoding(externalEncoding);
if (readEncoding != null) byteList.setEncoding(readEncoding);
return byteList;
}

private IRubyObject internalSepGets(ByteList sep, int limit) throws IOException {
ByteList result = newExternalByteList();
ByteList result = newReadByteList();
if (sep.getRealSize() == 0) sep = Stream.PARAGRAPH_SEPARATOR;
int ce = -1;
// TODO: CRuby does encoding aware 'gets'. Not yet implemented.
Expand Down Expand Up @@ -1521,7 +1521,7 @@ public IRubyObject readpartial(IRubyObject[] args) {
}

private IRubyObject readPartial(int len, RubyString outbuf) throws IOException {
ByteList val = newExternalByteList(10);
ByteList val = newReadByteList(10);
byte[] buffer = new byte[len];
int read = bufferedStream.read(buffer, 0, len);
if (read == -1) {
Expand All @@ -1540,7 +1540,7 @@ private IRubyObject readAll() throws IOException {
}

private IRubyObject readAll(int limit) throws IOException {
ByteList val = newExternalByteList(10);
ByteList val = newReadByteList(10);
int rest = limit == -1 ? BUFF_SIZE : limit;
byte[] buffer = new byte[rest];
while (rest > 0) {
Expand Down Expand Up @@ -1571,7 +1571,7 @@ private IRubyObject readSize(int len) throws IOException {
offset += read;
} // hmm...
this.position += buffer.length;
return newStr(getRuntime(), newExternalByteList(buffer, 0, len - toRead, false));
return newStr(getRuntime(), newReadByteList(buffer, 0, len - toRead, false));
}

@JRubyMethod(name = "lineno=", required = 1)
Expand Down Expand Up @@ -1872,8 +1872,8 @@ public IRubyObject initialize19(IRubyObject[] args, Block unused) {
if (!opt.isNil()) {
RubyIO.EncodingOption enc = RubyIO.extractEncodingOptions(opt);
if (enc != null) {
externalEncoding = enc.getExternalEncoding();
internalEncoding = enc.getInternalEncoding();
readEncoding = enc.getExternalEncoding();
writeEncoding = enc.getInternalEncoding();
}
IRubyObject[] newArgs = new IRubyObject[args.length - 1];
System.arraycopy(args, 0, newArgs, 0, args.length - 1);
Expand Down Expand Up @@ -2078,10 +2078,10 @@ public IRubyObject write(IRubyObject p1) {
ByteList bytes = p1.asString().getByteList();
Ruby runtime = getRuntime();
if (runtime.is1_9()) {
if (externalEncoding != null
&& externalEncoding != runtime.getEncodingService().getAscii8bitEncoding()) {
if (readEncoding != null
&& readEncoding != runtime.getEncodingService().getAscii8bitEncoding()) {
bytes = RubyString.transcode(runtime.getCurrentContext(), bytes, null,
externalEncoding, runtime.getNil());
readEncoding, runtime.getNil());
}
}
try {
Expand Down

0 comments on commit 84e3e3f

Please sign in to comment.