1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections.primitives.adapters.io;
18
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.util.NoSuchElementException;
22
23 import org.apache.commons.collections.primitives.CharIterator;
24
25
26
27
28
29
30
31 public class ReaderCharIterator implements CharIterator {
32
33 public ReaderCharIterator(Reader in) {
34 this.reader = in;
35 }
36
37 public static CharIterator adapt(Reader in) {
38 return null == in ? null : new ReaderCharIterator(in);
39 }
40
41 public boolean hasNext() {
42 ensureNextAvailable();
43 return (-1 != next);
44 }
45
46 public char next() {
47 if(!hasNext()) {
48 throw new NoSuchElementException("No next element");
49 } else {
50 nextAvailable = false;
51 return (char)next;
52 }
53 }
54
55
56
57
58
59 public void remove() throws UnsupportedOperationException {
60 throw new UnsupportedOperationException("remove() is not supported here");
61 }
62
63 private void ensureNextAvailable() {
64 if(!nextAvailable) {
65 readNext();
66 }
67 }
68
69 private void readNext() {
70 try {
71 next = reader.read();
72 nextAvailable = true;
73 } catch(IOException e) {
74
75
76 throw new RuntimeException(e.toString());
77 }
78 }
79
80 private Reader reader = null;
81 private boolean nextAvailable = false;
82 private int next;
83
84 }