diff options
author | Pacien TRAN-GIRARD | 2014-05-04 17:37:41 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2014-05-04 17:37:41 +0200 |
commit | 688634ae5a5aaf663159032e67d2132ea61c5d5f (patch) | |
tree | e0642498c904e8a9373df479239fbfc603a957d3 /src/rejava/io/StringReader.java | |
parent | 780bae6cd1a4d2a81b8c3ed72ee5d73cee9b5ccb (diff) | |
download | esieequest-688634ae5a5aaf663159032e67d2132ea61c5d5f.tar.gz |
Implement "save" and "load"
Diffstat (limited to 'src/rejava/io/StringReader.java')
-rw-r--r-- | src/rejava/io/StringReader.java | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/rejava/io/StringReader.java b/src/rejava/io/StringReader.java new file mode 100644 index 0000000..dc39769 --- /dev/null +++ b/src/rejava/io/StringReader.java | |||
@@ -0,0 +1,51 @@ | |||
1 | package rejava.io; | ||
2 | |||
3 | /** | ||
4 | * A minimal subset of java.io.StringReader. | ||
5 | * (http://hg.openjdk.java.net/jdk7/jdk7 | ||
6 | * /jdk/file/9b8c96f96a0f/src/share/classes/java/io/StringReader.java) | ||
7 | * | ||
8 | * @author Pacien TRAN-GIRARD | ||
9 | */ | ||
10 | public class StringReader extends Reader { | ||
11 | |||
12 | private final String str; | ||
13 | private int index; | ||
14 | |||
15 | public StringReader(final String str) { | ||
16 | this.str = str; | ||
17 | } | ||
18 | |||
19 | @Override | ||
20 | public int read() { | ||
21 | if (this.str == null) { | ||
22 | return -1; | ||
23 | } | ||
24 | if (this.index >= this.str.length()) { | ||
25 | return -1; | ||
26 | } | ||
27 | this.index++; | ||
28 | return this.str.charAt(this.index); | ||
29 | } | ||
30 | |||
31 | @Override | ||
32 | public int read(final char[] cbuf, final int off, final int len) { | ||
33 | if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) { | ||
34 | throw new IndexOutOfBoundsException(); | ||
35 | } | ||
36 | if (len == 0) { | ||
37 | return 0; | ||
38 | } | ||
39 | if (this.str == null) { | ||
40 | return -1; | ||
41 | } | ||
42 | if (this.index >= this.str.length()) { | ||
43 | return -1; | ||
44 | } | ||
45 | final int n = Math.min(this.str.length() - this.index, len); | ||
46 | this.str.getChars(this.index, this.index + n, cbuf, off); | ||
47 | this.index += n; | ||
48 | return n; | ||
49 | } | ||
50 | |||
51 | } \ No newline at end of file | ||