1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.io.unsync;
24  
25  import java.io.IOException;
26  import java.io.Reader;
27  
28  import java.nio.CharBuffer;
29  
30  /**
31   * <a href="UnsyncStringReader.java.html"><b><i>View Source</i></b></a>
32   *
33   * <p>
34   * See http://support.liferay.com/browse/LPS-6648.
35   * </p>
36   *
37   * @author Shuyang Zhou
38   */
39  public class UnsyncStringReader extends Reader {
40  
41      public UnsyncStringReader(String string) {
42          this.string = string;
43          stringLength = string.length();
44      }
45  
46      public void close() {
47          string = null;
48      }
49  
50      public void mark(int readAheadLimit) throws IOException {
51          if (string == null) {
52              throw new IOException("String is null");
53          }
54          markIndex = index;
55      }
56  
57      public boolean markSupported() {
58          return true;
59      }
60  
61      public int read() throws IOException {
62          if (string == null) {
63              throw new IOException("String is null");
64          }
65  
66          if (index >= stringLength) {
67              return -1;
68          }
69  
70          return string.charAt(index++);
71      }
72  
73      public int read(char[] charArray) throws IOException {
74          return read(charArray, 0, charArray.length);
75      }
76  
77      public int read(char[] charArray, int offset, int length)
78          throws IOException {
79  
80          if (string == null) {
81              throw new IOException("String is null");
82          }
83  
84          if (length <= 0) {
85              return 0;
86          }
87  
88          if (index >= stringLength) {
89              return -1;
90          }
91  
92          int read = length;
93  
94          if ((index + read) > stringLength) {
95              read = stringLength - index;
96          }
97  
98          string.getChars(index, index + read, charArray, offset);
99  
100         index += read;
101 
102         return read;
103     }
104 
105     public int read(CharBuffer charBuffer) throws IOException {
106         int remaining = charBuffer.remaining();
107 
108         char[] charArray = new char[remaining];
109 
110         int read = read(charArray, 0, remaining);
111 
112         if (read > 0) {
113             charBuffer.put(charArray, 0, read);
114         }
115 
116         return read;
117     }
118 
119     public boolean ready() throws IOException {
120         if (string == null) {
121             throw new IOException("String is null");
122         }
123 
124         return true;
125     }
126 
127     public void reset() throws IOException {
128         if (string == null) {
129             throw new IOException("String is null");
130         }
131 
132         index = markIndex;
133     }
134 
135     public long skip(long skip) {
136         if (index >= stringLength) {
137             return 0;
138         }
139 
140         if ((skip + index) > stringLength) {
141             skip = stringLength - index;
142         }
143 
144         index += skip;
145 
146         return skip;
147     }
148 
149     protected int index;
150     protected int stringLength;
151     protected int markIndex;
152     protected String string;
153 
154 }