001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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     * <p>
024     * See http://issues.liferay.com/browse/LPS-6648.
025     * </p>
026     *
027     * @author Shuyang Zhou
028     */
029    public class UnsyncStringReader extends Reader {
030    
031            public UnsyncStringReader(String string) {
032                    this.string = string;
033                    stringLength = string.length();
034            }
035    
036            @Override
037            public void close() {
038                    string = null;
039            }
040    
041            @Override
042            public void mark(int readAheadLimit) throws IOException {
043                    if (string == null) {
044                            throw new IOException("String is null");
045                    }
046                    markIndex = index;
047            }
048    
049            @Override
050            public boolean markSupported() {
051                    return true;
052            }
053    
054            @Override
055            public int read() throws IOException {
056                    if (string == null) {
057                            throw new IOException("String is null");
058                    }
059    
060                    if (index >= stringLength) {
061                            return -1;
062                    }
063    
064                    return string.charAt(index++);
065            }
066    
067            @Override
068            public int read(char[] chars) throws IOException {
069                    return read(chars, 0, chars.length);
070            }
071    
072            @Override
073            public int read(char[] chars, int offset, int length)
074                    throws IOException {
075    
076                    if (string == null) {
077                            throw new IOException("String is null");
078                    }
079    
080                    if (length <= 0) {
081                            return 0;
082                    }
083    
084                    if (index >= stringLength) {
085                            return -1;
086                    }
087    
088                    int read = length;
089    
090                    if ((index + read) > stringLength) {
091                            read = stringLength - index;
092                    }
093    
094                    string.getChars(index, index + read, chars, offset);
095    
096                    index += read;
097    
098                    return read;
099            }
100    
101            @Override
102            public int read(CharBuffer charBuffer) throws IOException {
103                    int remaining = charBuffer.remaining();
104    
105                    char[] chars = new char[remaining];
106    
107                    int read = read(chars, 0, remaining);
108    
109                    if (read > 0) {
110                            charBuffer.put(chars, 0, read);
111                    }
112    
113                    return read;
114            }
115    
116            @Override
117            public boolean ready() throws IOException {
118                    if (string == null) {
119                            throw new IOException("String is null");
120                    }
121    
122                    return true;
123            }
124    
125            @Override
126            public void reset() throws IOException {
127                    if (string == null) {
128                            throw new IOException("String is null");
129                    }
130    
131                    index = markIndex;
132            }
133    
134            @Override
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    }