KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > io > CharSequenceReader


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.io;
10
11 import j2me.lang.CharSequence;
12 import j2me.lang.IllegalStateException;
13 import java.io.IOException JavaDoc;
14 import java.io.Reader JavaDoc;
15 import javolution.lang.MathLib;
16 import javolution.lang.Reusable;
17 import javolution.text.Appendable;
18 import javolution.text.CharArray;
19 import javolution.text.Text;
20 import javolution.text.TextBuilder;
21
22 /**
23  * <p> This class allows any <code>CharSequence</code> to be used as
24  * a reader.</p>
25  *
26  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
27  * @version 3.8, May 8, 2004
28  */

29 public final class CharSequenceReader extends Reader JavaDoc implements Reusable {
30
31     /**
32      * Holds the character sequence input.
33      */

34     private CharSequence JavaDoc _input;
35
36     /**
37      * Holds the current index into the character sequence.
38      */

39     private int _index;
40
41     /**
42      * Creates a new character sequence reader for which the character
43      * sequence input is not set.
44      *
45      * @see #setInput
46      */

47     public CharSequenceReader() {
48     }
49
50     /**
51      * Sets the character sequence to use for reading.
52      *
53      * @param charSequence the character sequence to be read.
54      * @return this reader.
55      * @throws IllegalStateException if this reader is being reused and
56      * it has not been {@link #close closed} or {@link #reset reset}.
57      */

58     public CharSequenceReader setInput(CharSequence JavaDoc charSequence) {
59         if (_input != null)
60             throw new IllegalStateException JavaDoc("Reader not closed or reset");
61         _input = charSequence;
62         return this;
63     }
64
65     /**
66      * Indicates if this stream is ready to be read.
67      *
68      * @return <code>true</code> if this reader has remaining characters to
69      * read; <code>false</code> otherwise.
70      * @throws IOException if an I/O error occurs.
71      */

72     public boolean ready() throws IOException JavaDoc {
73         if (_input == null)
74             throw new IOException JavaDoc("Reader closed");
75         return true;
76     }
77
78     /**
79      * Closes and {@link #reset resets} this reader for reuse.
80      *
81      * @throws IOException if an I/O error occurs.
82      */

83     public void close() throws IOException JavaDoc {
84         if (_input != null) {
85             reset();
86         }
87     }
88
89     /**
90      * Reads a single character. This method does not block, <code>-1</code>
91      * is returned if the end of the character sequence input has been reached.
92      *
93      * @return the 31-bits Unicode of the character read, or -1 if there is
94      * no more remaining bytes to be read.
95      * @throws IOException if an I/O error occurs (e.g. incomplete
96      * character sequence being read).
97      */

98     public int read() throws IOException JavaDoc {
99         if (_input == null)
100             throw new IOException JavaDoc("Reader closed");
101         return (_index < _input.length()) ? _input.charAt(_index++) : -1;
102     }
103
104     /**
105      * Reads characters into a portion of an array. This method does not
106      * block.
107      *
108      * @param cbuf the destination buffer.
109      * @param off the offset at which to start storing characters.
110      * @param len the maximum number of characters to read
111      * @return the number of characters read, or -1 if there is no more
112      * character to be read.
113      * @throws IOException if an I/O error occurs.
114      */

115     public int read(char cbuf[], int off, int len) throws IOException JavaDoc {
116         if (_input == null)
117             throw new IOException JavaDoc("Reader closed");
118         final int inputLength = _input.length();
119         if (_index >= inputLength)
120             return -1;
121         final int count = MathLib.min(inputLength - _index, len);
122         final Object JavaDoc csq = _input;
123         if (csq instanceof String JavaDoc) {
124             String JavaDoc str = (String JavaDoc) csq;
125             str.getChars(_index, _index + count, cbuf, off);
126         } else if (csq instanceof Text) {
127             Text txt = (Text) csq;
128             txt.getChars(_index, _index + count, cbuf, off);
129         } else if (csq instanceof TextBuilder) {
130             TextBuilder tb = (TextBuilder) csq;
131             tb.getChars(_index, _index + count, cbuf, off);
132         } else if (csq instanceof CharArray) {
133             CharArray ca = (CharArray) csq;
134             System
135                     .arraycopy(ca.array(), _index + ca.offset(), cbuf, off,
136                             count);
137         } else { // Generic CharSequence.
138
for (int i = off, n = off + count, j = _index; i < n;) {
139                 cbuf[i++] = _input.charAt(j++);
140             }
141         }
142         _index += count;
143         return count;
144     }
145
146     /**
147      * Reads characters into the specified appendable. This method does not
148      * block.
149      *
150      * @param dest the destination buffer.
151      * @throws IOException if an I/O error occurs.
152      */

153     public void read(Appendable JavaDoc dest) throws IOException JavaDoc {
154         if (_input == null)
155             throw new IOException JavaDoc("Reader closed");
156         dest.append(_input);
157     }
158
159     // Implements Reusable.
160
public void reset() {
161         _index = 0;
162         _input = null;
163     }
164
165 }
Popular Tags