diff --git a/ext/java/nokogiri/NokogiriService.java b/ext/java/nokogiri/NokogiriService.java index 1ee24341f4f..e68f65e20ba 100644 --- a/ext/java/nokogiri/NokogiriService.java +++ b/ext/java/nokogiri/NokogiriService.java @@ -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) { diff --git a/ext/java/nokogiri/XmlReader.java b/ext/java/nokogiri/XmlReader.java index 76f538ee4ca..ea4a64a5aa7 100644 --- a/ext/java/nokogiri/XmlReader.java +++ b/ext/java/nokogiri/XmlReader.java @@ -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; @@ -45,7 +45,6 @@ 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; @@ -53,13 +52,15 @@ 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; @@ -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); @@ -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) { @@ -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; } @@ -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; } diff --git a/test/xml/test_reader_encoding.rb b/test/xml/test_reader_encoding.rb index 4a9dd30db36..92e99d32e04 100644 --- a/test/xml/test_reader_encoding.rb +++ b/test/xml/test_reader_encoding.rb @@ -121,22 +121,6 @@ def test_name end end - def test_reader_blocking - rd, wr = IO.pipe() - wr.puts "" * 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