From 543be31ee2b4dd734fe222ecdf94744caf244e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Sowi=C5=84ski?= Date: Sun, 14 Jul 2024 20:32:49 +0200 Subject: [PATCH] Fix native parsing from Java InputStream (#114) We checked if more bytes are available in the InputStream using .available()... which absolutely not how Java byte streams work, I have no idea how I made this mistake. Anyway, the proto lib checks this for us, so we can also simplify the code a bit. --- .../jelly/convert/jena/riot/JellyReader.scala | 20 +++++++++---------- .../jelly/convert/rdf4j/rio/JellyParser.scala | 18 ++++++++--------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/jena/src/main/scala/eu/ostrzyciel/jelly/convert/jena/riot/JellyReader.scala b/jena/src/main/scala/eu/ostrzyciel/jelly/convert/jena/riot/JellyReader.scala index f5972543..3dc2495b 100644 --- a/jena/src/main/scala/eu/ostrzyciel/jelly/convert/jena/riot/JellyReader.scala +++ b/jena/src/main/scala/eu/ostrzyciel/jelly/convert/jena/riot/JellyReader.scala @@ -31,16 +31,16 @@ object JellyReader extends ReaderRIOT: val decoder = JenaConverterFactory.anyStatementDecoder(Some(supportedOptions)) output.start() try { - while in.available() > 0 do - val frame = RdfStreamFrame.parseDelimitedFrom(in) - frame match - case Some(f) => - for row <- f.rows do - decoder.ingestRow(row) match - case Some(st: Triple) => output.triple(st) - case Some(st: Quad) => output.quad(st) - case None => () - case None => () + Iterator.continually(RdfStreamFrame.parseDelimitedFrom(in)) + .takeWhile(_.isDefined) + .foreach { maybeFrame => + val frame = maybeFrame.get + for row <- frame.rows do + decoder.ingestRow(row) match + case Some(st: Triple) => output.triple(st) + case Some(st: Quad) => output.quad(st) + case None => () + } } finally { output.finish() diff --git a/rdf4j/src/main/scala/eu/ostrzyciel/jelly/convert/rdf4j/rio/JellyParser.scala b/rdf4j/src/main/scala/eu/ostrzyciel/jelly/convert/rdf4j/rio/JellyParser.scala index 3f663187..53677ca9 100644 --- a/rdf4j/src/main/scala/eu/ostrzyciel/jelly/convert/rdf4j/rio/JellyParser.scala +++ b/rdf4j/src/main/scala/eu/ostrzyciel/jelly/convert/rdf4j/rio/JellyParser.scala @@ -38,15 +38,15 @@ final class JellyParser extends AbstractRDFParser: rdfHandler.startRDF() try { - while in.available() > 0 do - val frame = RdfStreamFrame.parseDelimitedFrom(in) - frame match - case Some(f) => - for row <- f.rows do - decoder.ingestRow(row) match - case Some(st) => rdfHandler.handleStatement(st) - case None => () - case None => () + Iterator.continually(RdfStreamFrame.parseDelimitedFrom(in)) + .takeWhile(_.isDefined) + .foreach { maybeFrame => + val frame = maybeFrame.get + for row <- frame.rows do + decoder.ingestRow(row) match + case Some(st) => rdfHandler.handleStatement(st) + case None => () + } } finally { rdfHandler.endRDF()