001
014
015 package com.liferay.portal.kernel.io.unsync;
016
017 import com.liferay.portal.kernel.nio.charset.CharsetEncoderUtil;
018 import com.liferay.portal.kernel.util.StringPool;
019
020 import java.io.IOException;
021 import java.io.OutputStream;
022 import java.io.Writer;
023
024 import java.nio.ByteBuffer;
025 import java.nio.CharBuffer;
026
027
030 public class UnsyncCharArrayWriter extends Writer {
031
032 public UnsyncCharArrayWriter() {
033 this(32);
034 }
035
036 public UnsyncCharArrayWriter(int initialSize) {
037 buffer = new char[initialSize];
038 }
039
040 @Override
041 public Writer append(char c) {
042 write(c);
043
044 return this;
045 }
046
047 @Override
048 public Writer append(CharSequence charSequence) {
049 String string = null;
050
051 if (charSequence == null) {
052 string = StringPool.NULL;
053 }
054 else {
055 string = charSequence.toString();
056 }
057
058 write(string, 0, string.length());
059
060 return this;
061 }
062
063 @Override
064 public Writer append(CharSequence charSequence, int start, int end) {
065 String string = null;
066
067 if (charSequence == null) {
068 string = StringPool.NULL;
069 }
070 else {
071 string = charSequence.subSequence(start, end).toString();
072 }
073
074 write(string, 0, string.length());
075
076 return this;
077 }
078
079 @Override
080 public void close() {
081 }
082
083 @Override
084 public void flush() {
085 }
086
087 public void reset() {
088 index = 0;
089 }
090
091 public int size() {
092 return index;
093 }
094
095 public CharBuffer toCharBuffer() {
096 return CharBuffer.wrap(buffer, 0, index);
097 }
098
099 @Override
100 public String toString() {
101 return new String(buffer, 0, index);
102 }
103
104 @Override
105 public void write(char[] chars) {
106 write(chars, 0, chars.length);
107 }
108
109 @Override
110 public void write(char[] chars, int offset, int length) {
111 if (length <= 0) {
112 return;
113 }
114
115 int newIndex = index + length;
116
117 if (newIndex > buffer.length) {
118 int newBufferSize = Math.max(buffer.length << 1, newIndex);
119
120 char[] newBuffer = new char[newBufferSize];
121
122 System.arraycopy(buffer, 0, newBuffer, 0, index);
123
124 buffer = newBuffer;
125 }
126
127 System.arraycopy(chars, offset, buffer, index, length);
128
129 index = newIndex;
130 }
131
132 @Override
133 public void write(int c) {
134 int newIndex = index + 1;
135
136 if (newIndex > buffer.length) {
137 int newBufferSize = Math.max(buffer.length << 1, newIndex);
138
139 char[] newBuffer = new char[newBufferSize];
140
141 System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
142
143 buffer = newBuffer;
144 }
145
146 buffer[index] = (char)c;
147
148 index = newIndex;
149 }
150
151 @Override
152 public void write(String string) {
153 write(string, 0, string.length());
154 }
155
156 @Override
157 public void write(String string, int offset, int length) {
158 if (length <= 0) {
159 return;
160 }
161
162 int newIndex = index + length;
163
164 if (newIndex > buffer.length) {
165 int newBufferSize = Math.max(buffer.length << 1, newIndex);
166
167 char[] newBuffer = new char[newBufferSize];
168
169 System.arraycopy(buffer, 0, newBuffer, 0, index);
170
171 buffer = newBuffer;
172 }
173
174 string.getChars(offset, offset + length, buffer, index);
175
176 index = newIndex;
177 }
178
179 public int writeTo(CharBuffer charBuffer) {
180 int length = charBuffer.remaining();
181
182 if (length > index) {
183 length = index;
184 }
185
186 if (length == 0) {
187 return 0;
188 }
189
190 charBuffer.put(buffer, 0, length);
191
192 return length;
193 }
194
195 public int writeTo(OutputStream outputStream, String charsetName)
196 throws IOException {
197
198 ByteBuffer byteBuffer = CharsetEncoderUtil.encode(
199 charsetName, buffer, 0, index);
200
201 int length = byteBuffer.limit();
202
203 outputStream.write(byteBuffer.array(), 0, length);
204
205 return length;
206 }
207
208 public int writeTo(Writer writer) throws IOException {
209 writer.write(buffer, 0, index);
210
211 return index;
212 }
213
214 protected char[] buffer;
215 protected int index;
216
217 }