1
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
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 }