Scanner hasNext checks
authorNiki Roo <niki@nikiroo.be>
Wed, 3 Jun 2020 13:52:26 +0000 (15:52 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 3 Jun 2020 13:52:26 +0000 (15:52 +0200)
src/be/nikiroo/fanfix/supported/BasicSupport.java
src/be/nikiroo/fanfix/supported/Text.java

index ff9ecaf9be3cc80666d964ee48da375d1307bae6..9b56f885d94a14ff1025b3833aa34209c48374dd 100644 (file)
@@ -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);
index 45d970a656322ef8e312f7cd6950a83123aedfe7..45b761a1f2e0fba52b91aff057a303086a40e134 100644 (file)
@@ -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('(');