KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > susebox > jtopas > CharArraySource


1 /*
2  * CharArraySource.java: TokenizerSource implementation for character buffers.
3  *
4  * Copyright (C) 2003 Heiko Blau
5  *
6  * This file belongs to the JTopas Library.
7  * JTopas is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or (at your
10  * option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License along
18  * with JTopas. If not, write to the
19  *
20  * Free Software Foundation, Inc.
21  * 59 Temple Place, Suite 330,
22  * Boston, MA 02111-1307
23  * USA
24  *
25  * or check the Internet: http://www.fsf.org
26  *
27  * Contact:
28  * email: heiko@susebox.de
29  */

30
31 package de.susebox.jtopas;
32
33
34 //-----------------------------------------------------------------------------
35
// Class CharArraySource
36
//
37

38 /**
39  * Implementation of the {@link TokenizerSource} and its extension
40  * {@link CharSequenceTokenizerSource} for character arrays. It is a shortcut for:
41  *<block><pre>
42  * char[] myData = { ... };
43  * TokenizerSource source = new ReaderSource(new CharArrayReader(myData));
44  *</pre></block>
45  * The class also provides a faster access to the data in a character array
46  * through the methods of the {@link java.lang.CharSequence} interface.
47  * Since this interface was only introduced with Java 1.4, this class can only
48  * be used with 1.4 or higher Java versions.
49  *
50  * @see TokenizerSource
51  * @see CharSequenceTokenizerSource
52  * @see StringSource
53  * @author Heiko Blau
54  */

55 public class CharArraySource implements CharSequenceTokenizerSource {
56   
57   //---------------------------------------------------------------------------
58
// Constructors
59
//
60

61   /**
62    * Constructing a <code>CharArraySource</code> instance using the
63    * given character buffer starting with offset 0 and the full length of the
64    * array.
65    *
66    * @param data the character data to tokenize
67    */

68   public CharArraySource(char[] data) {
69     this(data, 0, (data != null) ? data.length : -1);
70   }
71   
72   /**
73    * Constructing an <code>CharArraySource</code> instance using the
74    * given character buffer starting with the given offset 0 and the given length.
75    *
76    * @param data the character data to tokenize
77    * @param offset there to start in the given data
78    * @param length count of characters to tokenize
79    */

80   public CharArraySource(char[] data, int offset, int length) {
81     _offset = offset;
82     if (data == null || length < 0) {
83       _data = null;
84       _length = -1;
85     } else {
86       _data = data;
87       _length = length;
88     }
89   }
90   
91
92   //---------------------------------------------------------------------------
93
// Methods of the TokenizerSource interface
94
//
95

96   /**
97    * This method copies the available data into the given buffer according to
98    * the given offset and maximum character count. See {@link TokenizerSource#read}
99    * for details.
100    *
101    * @param cbuf buffer to receive data
102    * @param offset position from where the data should be inserted in <code>cbuf</code>
103    * @param maxChars maximum number of characters to be read into <code>cbuf</code>
104    * @return actually read characters or -1 on an end-of-file condition
105    */

106   public int read(char[] cbuf, final int offset, final int maxChars) throws Exception JavaDoc {
107     int left = Math.min(_length - _readOffset, maxChars);
108       
109     if (left > 0) {
110       System.arraycopy(_data, _offset + _readOffset, cbuf, offset, left);
111       _readOffset += left;
112       return left;
113     } else {
114       return -1;
115     }
116   }
117
118   //---------------------------------------------------------------------------
119
// Methods of the CharSequence interface
120
//
121

122   /**
123    * Implements {@link java.lang.CharSequence#charAt} for this class. Note that
124    * the given index is relative to the offset that was passed when this instance
125    * was constructed ({@link #CharArraySource(char[], int, int)})
126    *
127    * @param index which character to retrieve
128    * @return the character at the given index
129    */

130   public char charAt(int index) {
131     return _data[_offset + index];
132   }
133
134   /**
135    * Implements {@link java.lang.CharSequence#length} for this class.
136    *
137    * @return the number of available characters
138    */

139   public int length() {
140     return _length;
141   }
142   
143   /**
144    * Implements {@link java.lang.CharSequence#subSequence} for this class.
145    *
146    * @param start the new <code>CharSequence</code> contains the characters
147    * from and including this position
148    * @param end the new <code>CharSequence</code> contains the characters
149    * up to and excluding this position
150    * @return a part of this <code>CharSequence</code>
151    */

152   public CharSequence JavaDoc subSequence(int start, int end) {
153     return new CharArraySource(_data, _offset + start, end - start);
154   }
155   
156   /**
157    * Returns the string representation of this <code>CharArraySource</code> according
158    * to the {@link java.lang.CharSequence} interface contract.
159    *
160    * @return a string containing all characters of this <code>CharArraySource</code>
161    */

162   public String JavaDoc toString() {
163     return new String JavaDoc(_data, _offset, _length);
164   }
165
166   
167   //---------------------------------------------------------------------------
168
// Members
169
//
170
private char[] _data = null;
171   private int _offset = 0;
172   private int _length = -1;
173   private int _readOffset = 0;
174 }
175
Popular Tags