001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.io.unsync;
016    
017    import java.io.IOException;
018    import java.io.Reader;
019    
020    import java.nio.CharBuffer;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class UnsyncCharArrayReader extends Reader {
026    
027            public UnsyncCharArrayReader(char[] chars) {
028                    buffer = chars;
029                    capacity = chars.length;
030                    index = 0;
031            }
032    
033            public UnsyncCharArrayReader(char[] chars, int offset, int length) {
034                    buffer = chars;
035                    capacity = Math.min(chars.length, offset + length);
036                    index = offset;
037                    markIndex = offset;
038            }
039    
040            @Override
041            public void close() {
042                    buffer = null;
043            }
044    
045            @Override
046            public void mark(int readAheadLimit) throws IOException {
047                    if (buffer == null) {
048                            throw new IOException("Stream closed");
049                    }
050                    markIndex = index;
051            }
052    
053            @Override
054            public boolean markSupported() {
055                    return true;
056            }
057    
058            @Override
059            public int read() throws IOException {
060                    if (buffer == null) {
061                            throw new IOException("Stream closed");
062                    }
063    
064                    if (index >= capacity) {
065                            return -1;
066                    }
067                    else {
068                            return buffer[index++];
069                    }
070            }
071    
072            @Override
073            public int read(char[] chars) throws IOException {
074                    return read(chars, 0, chars.length);
075            }
076    
077            @Override
078            public int read(char[] chars, int offset, int length)
079                    throws IOException {
080    
081                    if (buffer == null) {
082                            throw new IOException("Stream closed");
083                    }
084    
085                    if (length <= 0) {
086                            return 0;
087                    }
088    
089                    if (index >= capacity) {
090                            return -1;
091                    }
092    
093                    int read = length;
094    
095                    if ((index + read) > capacity) {
096                            read = capacity - index;
097                    }
098    
099                    System.arraycopy(buffer, index, chars, offset, read);
100    
101                    index += read;
102    
103                    return read;
104            }
105    
106            @Override
107            public int read(CharBuffer charBuffer) throws IOException {
108                    if (buffer == null) {
109                            throw new IOException("Stream closed");
110                    }
111    
112                    int length = charBuffer.remaining();
113    
114                    if (length <= 0) {
115                            return 0;
116                    }
117    
118                    if (index >= capacity) {
119                            return -1;
120                    }
121    
122                    if ((index + length) > capacity) {
123                            length = capacity - index;
124                    }
125    
126                    charBuffer.put(buffer, index, length);
127    
128                    index += length;
129    
130                    return length;
131            }
132    
133            @Override
134            public boolean ready() throws IOException {
135                    if (buffer == null) {
136                            throw new IOException("Stream closed");
137                    }
138    
139                    if (capacity > index) {
140                            return true;
141                    }
142                    else {
143                            return false;
144                    }
145            }
146    
147            @Override
148            public void reset() throws IOException {
149                    if (buffer == null) {
150                            throw new IOException("Stream closed");
151                    }
152    
153                    index = markIndex;
154            }
155    
156            @Override
157            public long skip(long skip) throws IOException {
158                    if (buffer == null) {
159                            throw new IOException("Stream closed");
160                    }
161    
162                    if (skip < 0) {
163                            return 0;
164                    }
165    
166                    if (index + skip > capacity) {
167                            skip = capacity - index;
168                    }
169    
170                    index += skip;
171    
172                    return skip;
173            }
174    
175            protected char[] buffer;
176            protected int capacity;
177            protected int index;
178            protected int markIndex;
179    
180    }