From 6985e8150385ccb16e77549c4b1f0726839075e2 Mon Sep 17 00:00:00 2001 From: Zsombor Gegesy Date: Sun, 15 Aug 2010 14:28:09 +0200 Subject: [PATCH] add Properties.load(Reader) implementation --- classpath/java/util/Properties.java | 45 +++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/classpath/java/util/Properties.java b/classpath/java/util/Properties.java index 749869823b..1ff67dcf16 100644 --- a/classpath/java/util/Properties.java +++ b/classpath/java/util/Properties.java @@ -14,10 +14,15 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.io.IOException; +import java.io.Reader; public class Properties extends Hashtable { public void load(InputStream in) throws IOException { - new Parser().parse(in, this); + new InputStreamParser(in).parse(this); + } + + public void load(Reader reader) throws IOException { + new ReaderParser(reader).parse(this); } public void store(OutputStream out, String comment) throws IOException { @@ -53,7 +58,7 @@ public class Properties extends Hashtable { return keys(); } - private static class Parser { + private abstract static class Parser { private StringBuilder key = null; private StringBuilder value = null; private StringBuilder current = null; @@ -79,13 +84,15 @@ public class Properties extends Hashtable { key = value = current = null; } - private void parse(InputStream in, Map map) + abstract int readCharacter() throws IOException; + + void parse(Map map) throws IOException { boolean escaped = false; int c; - while ((c = in.read()) != -1) { + while ((c = readCharacter()) != -1) { if (c == '\\') { if (escaped) { escaped = false; @@ -98,7 +105,7 @@ public class Properties extends Hashtable { case '#': case '!': if (key == null) { - while ((c = in.read()) != -1 && c != '\n'); + while ((c = readCharacter()) != -1 && c != '\n'); } else { append(c); } @@ -153,4 +160,32 @@ public class Properties extends Hashtable { finishLine(map); } } + + static class InputStreamParser extends Parser { + InputStream in; + + + public InputStreamParser(InputStream in) { + this.in = in; + } + + @Override + int readCharacter() throws IOException { + return in.read(); + } + } + + static class ReaderParser extends Parser { + Reader in; + + public ReaderParser(Reader in) { + this.in = in; + } + + @Override + int readCharacter() throws IOException { + return in.read(); + } + } + }