1
14
15 package com.liferay.portal.kernel.io.unsync;
16
17 import com.liferay.portal.kernel.nio.charset.CharsetEncoderUtil;
18 import com.liferay.portal.kernel.util.StringPool;
19
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.io.Writer;
23
24 import java.nio.ByteBuffer;
25 import java.nio.CharBuffer;
26
27
32 public class UnsyncCharArrayWriter extends Writer {
33
34 public UnsyncCharArrayWriter() {
35 this(32);
36 }
37
38 public UnsyncCharArrayWriter(int initialSize) {
39 buffer = new char[initialSize];
40 }
41
42 public Writer append(char c) {
43 write(c);
44
45 return this;
46 }
47
48 public Writer append(CharSequence charSequence) {
49 String string = null;
50
51 if (charSequence == null) {
52 string = StringPool.NULL;
53 }
54 else {
55 string = charSequence.toString();
56 }
57
58 write(string, 0, string.length());
59
60 return this;
61 }
62
63 public Writer append(CharSequence charSequence, int start, int end) {
64 String string = null;
65
66 if (charSequence == null) {
67 string = StringPool.NULL;
68 }
69 else {
70 string = charSequence.subSequence(start, end).toString();
71 }
72
73 write(string, 0, string.length());
74
75 return this;
76 }
77
78 public void close() {
79 }
80
81 public void flush() {
82 }
83
84 public void reset() {
85 index = 0;
86 }
87
88 public int size() {
89 return index;
90 }
91
92 public CharBuffer toCharBuffer() {
93 return CharBuffer.wrap(buffer, 0, index);
94 }
95
96 public String toString() {
97 return new String(buffer, 0, index);
98 }
99
100 public void write(char[] charArray) {
101 write(charArray, 0, charArray.length);
102 }
103
104 public void write(char[] charArray, int offset, int length) {
105 if (length <= 0) {
106 return;
107 }
108
109 int newIndex = index + length;
110
111 if (newIndex > buffer.length) {
112 int newBufferSize = Math.max(buffer.length << 1, newIndex);
113
114 char[] newBuffer = new char[newBufferSize];
115
116 System.arraycopy(buffer, 0, newBuffer, 0, index);
117
118 buffer = newBuffer;
119 }
120
121 System.arraycopy(charArray, offset, buffer, index, length);
122
123 index = newIndex;
124 }
125
126 public void write(int c) {
127 int newIndex = index + 1;
128
129 if (newIndex > buffer.length) {
130 int newBufferSize = Math.max(buffer.length << 1, newIndex);
131
132 char[] newBuffer = new char[newBufferSize];
133
134 System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
135
136 buffer = newBuffer;
137 }
138
139 buffer[index] = (char)c;
140
141 index = newIndex;
142 }
143
144 public void write(String string) {
145 write(string, 0, string.length());
146 }
147
148 public void write(String string, int offset, int length) {
149 if (length <= 0) {
150 return;
151 }
152
153 int newIndex = index + length;
154
155 if (newIndex > buffer.length) {
156 int newBufferSize = Math.max(buffer.length << 1, newIndex);
157
158 char[] newBuffer = new char[newBufferSize];
159
160 System.arraycopy(buffer, 0, newBuffer, 0, index);
161
162 buffer = newBuffer;
163 }
164
165 string.getChars(offset, offset + length, buffer, index);
166
167 index = newIndex;
168 }
169
170 public int writeTo(CharBuffer charBuffer) {
171 int length = charBuffer.remaining();
172
173 if (length > index) {
174 length = index;
175 }
176
177 if (length == 0) {
178 return 0;
179 }
180
181 charBuffer.put(buffer, 0, length);
182
183 return length;
184 }
185
186 public int writeTo(OutputStream outputStream, String charsetName)
187 throws IOException {
188
189 ByteBuffer byteBuffer = CharsetEncoderUtil.encode(
190 charsetName, buffer, 0, index);
191
192 int length = byteBuffer.limit();
193
194 outputStream.write(byteBuffer.array(), 0, length);
195
196 return length;
197 }
198
199 public int writeTo(Writer writer) throws IOException {
200 writer.write(buffer, 0, index);
201
202 return index;
203 }
204
205 protected char[] buffer;
206 protected int index;
207
208 }