From 79ac5a2ae93a62fcc4aa32f897f8b092a3935147 Mon Sep 17 00:00:00 2001 From: Bela Ban Date: Fri, 10 Jan 2025 12:16:13 +0100 Subject: [PATCH] Skip comments in TCPPING.initial_hosts_file's contents --- src/org/jgroups/protocols/TCPPING.java | 2 +- src/org/jgroups/util/Util.java | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/org/jgroups/protocols/TCPPING.java b/src/org/jgroups/protocols/TCPPING.java index b804444fa5..d42377161f 100644 --- a/src/org/jgroups/protocols/TCPPING.java +++ b/src/org/jgroups/protocols/TCPPING.java @@ -151,7 +151,7 @@ public void init() throws Exception { try(InputStream input=Util.getResourceAsStream(initial_hosts_file, getClass())) { if(input == null) throw new IllegalArgumentException(String.format("initial_hosts_file '%s' not found", initial_hosts_file)); - initial_hosts_str=Util.readContents(input); + initial_hosts_str=Util.readContents(input, '#'); } } diff --git a/src/org/jgroups/util/Util.java b/src/org/jgroups/util/Util.java index 439b374943..af36040a61 100644 --- a/src/org/jgroups/util/Util.java +++ b/src/org/jgroups/util/Util.java @@ -2054,13 +2054,22 @@ public static String readFile(String filename) throws FileNotFoundException { public static String readContents(InputStream input) { + return readContents(input, (char)0); + } + + public static String readContents(InputStream input, char comment) { StringBuilder sb=new StringBuilder(); int ch; while(true) { try { ch=input.read(); - if(ch != -1) + if(ch != -1) { + if(comment != 0 && comment == ch) { + skipUntilEndOfLine(input); + continue; + } sb.append((char)ch); + } else break; } @@ -2071,6 +2080,21 @@ public static String readContents(InputStream input) { return sb.toString(); } + protected static void skipUntilEndOfLine(InputStream input) { + for(;;) { + try { + int ch=input.read(); + if(ch == -1) + break; + if(ch == '\n' || ch == '\r') + break; + } + catch(IOException e) { + break; + } + } + } + public static byte[] readFileContents(InputStream input) throws IOException { byte[] contents=new byte[10000]; byte[] buf=new byte[1024];