KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > StringArrayReader


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
package com.puppycrawl.tools.checkstyle;
20
21 import java.io.Reader JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 /**
25  * A Reader that reads from an underlying String array, assuming each
26  * array element corresponds to one line of text.
27  * <p>
28  * <strong>Note: This class is not thread safe, concurrent reads might produce
29  * unexpected results! </strong> Checkstyle only works with one thread so
30  * currently there is no need to introduce synchronization here.
31  * </p>
32  * @author <a HREF="mailto:lkuehne@users.sourceforge.net">Lars Kühne</a>
33  */

34 final class StringArrayReader extends Reader JavaDoc
35 {
36     /** the underlying String array */
37     private final String JavaDoc[] mUnderlyingArray;
38
39     /** array containing the length of the strings. */
40     private final int[] mLenghtArray;
41
42     /** the index of the currently read String */
43     private int mArrayIdx;
44
45     /** the index of the next character to be read */
46     private int mStringIdx;
47
48     /** flag to tell whether an implicit newline has to be reported */
49     private boolean mUnreportedNewline;
50
51     /** flag to tell if the reader has been closed */
52     private boolean mClosed;
53
54     /**
55      * Creates a new StringArrayReader.
56      *
57      * @param aUnderlyingArray the underlying String array.
58      */

59     StringArrayReader(String JavaDoc[] aUnderlyingArray)
60     {
61         final int length = aUnderlyingArray.length;
62         mUnderlyingArray = new String JavaDoc[length];
63         System.arraycopy(aUnderlyingArray, 0, mUnderlyingArray, 0, length);
64
65         //additionally store the length of the strings
66
//for performance optimization
67
mLenghtArray = new int[length];
68         for (int i = 0; i < length; i++) {
69             mLenghtArray[i] = mUnderlyingArray[i].length();
70         }
71     }
72
73     /** @see Reader */
74     public void close()
75     {
76         mClosed = true;
77     }
78
79     /** @return whether data is available that has not yet been read. */
80     private boolean dataAvailable()
81     {
82         return (mUnderlyingArray.length > mArrayIdx);
83     }
84
85     /** {@inheritDoc} */
86     public int read(char[] aCbuf, int aOff, int aLen) throws IOException JavaDoc
87     {
88         ensureOpen();
89
90         int retVal = 0;
91
92         if (!mUnreportedNewline && (mUnderlyingArray.length <= mArrayIdx)) {
93             return -1;
94         }
95
96         while ((retVal < aLen) && (mUnreportedNewline || dataAvailable())) {
97             if (mUnreportedNewline) {
98                 aCbuf[aOff + retVal] = '\n';
99                 retVal++;
100                 mUnreportedNewline = false;
101             }
102
103             if ((retVal < aLen) && dataAvailable()) {
104                 final String JavaDoc currentStr = mUnderlyingArray[mArrayIdx];
105                 final int currentLenth = mLenghtArray[mArrayIdx];
106                 final int srcEnd = Math.min(currentLenth,
107                                             mStringIdx + aLen - retVal);
108                 currentStr.getChars(mStringIdx, srcEnd, aCbuf, aOff + retVal);
109                 retVal += srcEnd - mStringIdx;
110                 mStringIdx = srcEnd;
111
112                 if (mStringIdx >= currentLenth) {
113                     mArrayIdx++;
114                     mStringIdx = 0;
115                     mUnreportedNewline = true;
116                 }
117             }
118         }
119         return retVal;
120     }
121
122     /** {@inheritDoc} */
123     public int read() throws IOException JavaDoc
124     {
125         if (mUnreportedNewline) {
126             mUnreportedNewline = false;
127             return '\n';
128         }
129
130         if ((mArrayIdx < mUnderlyingArray.length)
131             && (mStringIdx < mLenghtArray[mArrayIdx]))
132         {
133             // this is the common case,
134
// avoid char[] creation in super.read for performance
135
ensureOpen();
136             return mUnderlyingArray[mArrayIdx].charAt(mStringIdx++);
137         }
138         // don't bother duplicating the new line handling above
139
// for the uncommon case
140
return super.read();
141     }
142
143     /**
144      * Throws an IOException if the reader has already been closed.
145      *
146      * @throws IOException if the stream has been closed
147      */

148     private void ensureOpen() throws IOException JavaDoc
149     {
150         if (mClosed) {
151             throw new IOException JavaDoc("already closed");
152         }
153     }
154 }
155
Popular Tags