1
14
15 package com.liferay.portal.kernel.io.unsync;
16
17 import java.io.IOException;
18 import java.io.Reader;
19
20 import java.nio.CharBuffer;
21
22
31 public class UnsyncStringReader extends Reader {
32
33 public UnsyncStringReader(String string) {
34 this.string = string;
35 stringLength = string.length();
36 }
37
38 public void close() {
39 string = null;
40 }
41
42 public void mark(int readAheadLimit) throws IOException {
43 if (string == null) {
44 throw new IOException("String is null");
45 }
46 markIndex = index;
47 }
48
49 public boolean markSupported() {
50 return true;
51 }
52
53 public int read() throws IOException {
54 if (string == null) {
55 throw new IOException("String is null");
56 }
57
58 if (index >= stringLength) {
59 return -1;
60 }
61
62 return string.charAt(index++);
63 }
64
65 public int read(char[] charArray) throws IOException {
66 return read(charArray, 0, charArray.length);
67 }
68
69 public int read(char[] charArray, int offset, int length)
70 throws IOException {
71
72 if (string == null) {
73 throw new IOException("String is null");
74 }
75
76 if (length <= 0) {
77 return 0;
78 }
79
80 if (index >= stringLength) {
81 return -1;
82 }
83
84 int read = length;
85
86 if ((index + read) > stringLength) {
87 read = stringLength - index;
88 }
89
90 string.getChars(index, index + read, charArray, offset);
91
92 index += read;
93
94 return read;
95 }
96
97 public int read(CharBuffer charBuffer) throws IOException {
98 int remaining = charBuffer.remaining();
99
100 char[] charArray = new char[remaining];
101
102 int read = read(charArray, 0, remaining);
103
104 if (read > 0) {
105 charBuffer.put(charArray, 0, read);
106 }
107
108 return read;
109 }
110
111 public boolean ready() throws IOException {
112 if (string == null) {
113 throw new IOException("String is null");
114 }
115
116 return true;
117 }
118
119 public void reset() throws IOException {
120 if (string == null) {
121 throw new IOException("String is null");
122 }
123
124 index = markIndex;
125 }
126
127 public long skip(long skip) {
128 if (index >= stringLength) {
129 return 0;
130 }
131
132 if ((skip + index) > stringLength) {
133 skip = stringLength - index;
134 }
135
136 index += skip;
137
138 return skip;
139 }
140
141 protected int index;
142 protected int stringLength;
143 protected int markIndex;
144 protected String string;
145
146 }