From 728191e896c89ded34228a0985af09eec0a83288 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 3 Jun 2020 15:52:26 +0200 Subject: [PATCH] Scanner hasNext checks --- .../fanfix/supported/BasicSupport.java | 3 ++ src/be/nikiroo/fanfix/supported/Text.java | 28 +++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/be/nikiroo/fanfix/supported/BasicSupport.java b/src/be/nikiroo/fanfix/supported/BasicSupport.java index ff9ecaf..9b56f88 100644 --- a/src/be/nikiroo/fanfix/supported/BasicSupport.java +++ b/src/be/nikiroo/fanfix/supported/BasicSupport.java @@ -354,6 +354,9 @@ public abstract class BasicSupport { Scanner scan = new Scanner(in); scan.useDelimiter("\0"); try { + if(!scan.hasNext()) + throw new IOException("The given input was empty"); + return new JSONObject(scan.next()); } catch (JSONException e) { throw new IOException(e); diff --git a/src/be/nikiroo/fanfix/supported/Text.java b/src/be/nikiroo/fanfix/supported/Text.java index 45d970a..45b761a 100644 --- a/src/be/nikiroo/fanfix/supported/Text.java +++ b/src/be/nikiroo/fanfix/supported/Text.java @@ -100,10 +100,12 @@ class Text extends BasicSupport { @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); - scan.next(); // Title - scan.next(); // Author (Date) - String chapter0 = scan.next(); // empty or Chapter 0 - while (chapter0.isEmpty()) { + if (scan.hasNext()) + scan.next(); // Title + if (scan.hasNext()) + scan.next(); // Author (Date) + String chapter0 = ""; + while (scan.hasNext() && chapter0.isEmpty()) { chapter0 = scan.next(); } @@ -126,15 +128,20 @@ class Text extends BasicSupport { @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); - return scan.next(); + if (scan.hasNext()) + return scan.next(); + return ""; } private String getAuthor() { @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); - scan.next(); - String authorDate = scan.next(); + if (scan.hasNext()) + scan.next(); + String authorDate = ""; + if (scan.hasNext()) + authorDate = scan.next(); String author = authorDate; int pos = authorDate.indexOf('('); @@ -149,8 +156,11 @@ class Text extends BasicSupport { @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); - scan.next(); - String authorDate = scan.next(); + if (scan.hasNext()) + scan.next(); + String authorDate = ""; + if (scan.hasNext()) + authorDate = scan.next(); String date = ""; int pos = authorDate.indexOf('('); -- 2.27.0