001
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
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
051 markIndex = index;
052 }
053
054 @Override
055 public boolean markSupported() {
056 return true;
057 }
058
059 @Override
060 public int read() throws IOException {
061 if (buffer == null) {
062 throw new IOException("Stream closed");
063 }
064
065 if (index >= capacity) {
066 return -1;
067 }
068 else {
069 return buffer[index++];
070 }
071 }
072
073 @Override
074 public int read(char[] chars) throws IOException {
075 return read(chars, 0, chars.length);
076 }
077
078 @Override
079 public int read(char[] chars, int offset, int length) throws IOException {
080 if (buffer == null) {
081 throw new IOException("Stream closed");
082 }
083
084 if (length <= 0) {
085 return 0;
086 }
087
088 if (index >= capacity) {
089 return -1;
090 }
091
092 int read = length;
093
094 if ((index + read) > capacity) {
095 read = capacity - index;
096 }
097
098 System.arraycopy(buffer, index, chars, offset, read);
099
100 index += read;
101
102 return read;
103 }
104
105 @Override
106 public int read(CharBuffer charBuffer) throws IOException {
107 if (buffer == null) {
108 throw new IOException("Stream closed");
109 }
110
111 int length = charBuffer.remaining();
112
113 if (length <= 0) {
114 return 0;
115 }
116
117 if (index >= capacity) {
118 return -1;
119 }
120
121 if ((index + length) > capacity) {
122 length = capacity - index;
123 }
124
125 charBuffer.put(buffer, index, length);
126
127 index += length;
128
129 return length;
130 }
131
132 @Override
133 public boolean ready() throws IOException {
134 if (buffer == null) {
135 throw new IOException("Stream closed");
136 }
137
138 if (capacity > index) {
139 return true;
140 }
141 else {
142 return false;
143 }
144 }
145
146 @Override
147 public void reset() throws IOException {
148 if (buffer == null) {
149 throw new IOException("Stream closed");
150 }
151
152 index = markIndex;
153 }
154
155 @Override
156 public long skip(long skip) throws IOException {
157 if (buffer == null) {
158 throw new IOException("Stream closed");
159 }
160
161 if (skip < 0) {
162 return 0;
163 }
164
165 if ((index + skip) > capacity) {
166 skip = capacity - index;
167 }
168
169 index += skip;
170
171 return skip;
172 }
173
174 protected char[] buffer;
175 protected int capacity;
176 protected int index;
177 protected int markIndex;
178
179 }