Skip to content

Commit

Permalink
Revert "fix #831. The XmlReader shouldn't consume the entire input st…
Browse files Browse the repository at this point in the history
…ream before calling the block."

This reverts commit 2302c3f.
  • Loading branch information
jvshahid committed Jan 12, 2013
1 parent 2302c3f commit 2d7041f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
1 change: 0 additions & 1 deletion ext/java/nokogiri/NokogiriService.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ private static void createNokogiriClassCahce(Ruby ruby) {
nokogiriClassCache.put("Nokogiri::XML::XPathContext", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::XPathContext"));
nokogiriClassCache.put("Nokogiri::XML::AttributeDecl", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::AttributeDecl"));
nokogiriClassCache.put("Nokogiri::XML::SAX::ParserContext", (RubyClass)ruby.getClassFromPath("Nokogiri::XML::SAX::ParserContext"));
nokogiriClassCache.put("StringIO", (RubyClass)ruby.getClassFromPath("StringIO"));
}

private void init(Ruby ruby) {
Expand Down
26 changes: 12 additions & 14 deletions ext/java/nokogiri/XmlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
import static nokogiri.internals.NokogiriHelpers.stringOrBlank;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.Stack;

Expand All @@ -45,21 +45,22 @@
import nokogiri.internals.ParserContext.Options;
import nokogiri.internals.ReaderNode;
import nokogiri.internals.ReaderNode.ElementNode;
import nokogiri.internals.UncloseableInputStream;

import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.Block;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.lexer.yacc.SyntaxException;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.IOInputStream;
import org.jruby.util.ByteList;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -106,12 +107,14 @@ public void init(Ruby runtime) {
nodeQueue.add(new ReaderNode.EmptyNode(runtime));
}

private void parseRubyString(ThreadContext context, InputStream stream, IRubyObject url, Options options){
private void parseRubyString(ThreadContext context, RubyString content, IRubyObject url, Options options){
Ruby ruby = context.getRuntime();
try {
this.setState(XML_TEXTREADER_MODE_READING);
XMLReader reader = this.createReader(ruby, options);
InputSource inputSource = new InputSource(stream);
ByteList byteList = content.getByteList();
ByteArrayInputStream bais = new ByteArrayInputStream(byteList.unsafeBytes(), byteList.begin(), byteList.length());
InputSource inputSource = new InputSource(bais);
ParserContext.setUrl(context, inputSource, url);
reader.parse(inputSource);
this.setState(XML_TEXTREADER_MODE_CLOSED);
Expand Down Expand Up @@ -195,7 +198,7 @@ public static IRubyObject from_io(ThreadContext context, IRubyObject cls, IRubyO
if (args.length > 1) url = args[1];
if (args.length > 2) reader.setInstanceVariable("@encoding", args[2]);

InputStream stream = new UncloseableInputStream(new IOInputStream(args[0]));
RubyString content = RuntimeHelpers.invoke(context, args[0], "read").convertToString();

Options options;
if (args.length > 3) {
Expand All @@ -204,7 +207,7 @@ public static IRubyObject from_io(ThreadContext context, IRubyObject cls, IRubyO
// use the default options RECOVER | NONET
options = new ParserContext.Options(2048 | 1);
}
reader.parseRubyString(context, stream, url, options);
reader.parseRubyString(context, content, url, options);
return reader;
}

Expand All @@ -230,12 +233,7 @@ public static IRubyObject from_memory(ThreadContext context, IRubyObject cls, IR
// use the default options RECOVER | NONET
options = new ParserContext.Options(2048 | 1);
}

RubyClass klass = NokogiriService.nokogiriClassCache.get("StringIO");
IRubyObject stringIO = klass.newInstance(context, args[0], Block.NULL_BLOCK);
InputStream stream = new IOInputStream(stringIO);

reader.parseRubyString(context, stream, url, options);
reader.parseRubyString(context, args[0].convertToString(), url, options);
return reader;
}

Expand Down
16 changes: 0 additions & 16 deletions test/xml/test_reader_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,6 @@ def test_name
end
end

def test_reader_blocking
rd, wr = IO.pipe()
wr.puts "<foo/>" * 10000
wr.flush
t = Thread.start do
reader = Nokogiri::XML::Reader(rd, 'UTF-8')
reader.each do |node|
break
end
end
res = t.join(5) # wait 5 seconds for the thread to finish
rd.close
wr.close
refute_nil res, "Reader blocks trying to read the entire stream"
end

def test_value_lookup_segfault
skip("JRuby doesn't do GC.") if Nokogiri.jruby?
old_stress = GC.stress
Expand Down

0 comments on commit 2d7041f

Please sign in to comment.