001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
028     * @author Shuyang Zhou
029     */
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            public Writer append(char c) {
041                    write(c);
042    
043                    return this;
044            }
045    
046            public Writer append(CharSequence charSequence) {
047                    String string = null;
048    
049                    if (charSequence == null) {
050                            string = StringPool.NULL;
051                    }
052                    else {
053                            string = charSequence.toString();
054                    }
055    
056                    write(string, 0, string.length());
057    
058                    return this;
059            }
060    
061            public Writer append(CharSequence charSequence, int start, int end) {
062                    String string = null;
063    
064                    if (charSequence == null) {
065                            string = StringPool.NULL;
066                    }
067                    else {
068                            string = charSequence.subSequence(start, end).toString();
069                    }
070    
071                    write(string, 0, string.length());
072    
073                    return this;
074            }
075    
076            public void close() {
077            }
078    
079            public void flush() {
080            }
081    
082            public void reset() {
083                    index = 0;
084            }
085    
086            public int size() {
087                    return index;
088            }
089    
090            public CharBuffer toCharBuffer() {
091                    return CharBuffer.wrap(buffer, 0, index);
092            }
093    
094            public String toString() {
095                    return new String(buffer, 0, index);
096            }
097    
098            public void write(char[] charArray) {
099                    write(charArray, 0, charArray.length);
100            }
101    
102            public void write(char[] charArray, int offset, int length) {
103                    if (length <= 0) {
104                            return;
105                    }
106    
107                    int newIndex = index + length;
108    
109                    if (newIndex > buffer.length) {
110                            int newBufferSize = Math.max(buffer.length << 1, newIndex);
111    
112                            char[] newBuffer = new char[newBufferSize];
113    
114                            System.arraycopy(buffer, 0, newBuffer, 0, index);
115    
116                            buffer = newBuffer;
117                    }
118    
119                    System.arraycopy(charArray, offset, buffer, index, length);
120    
121                    index = newIndex;
122            }
123    
124            public void write(int c) {
125                    int newIndex = index + 1;
126    
127                    if (newIndex > buffer.length) {
128                            int newBufferSize = Math.max(buffer.length << 1, newIndex);
129    
130                            char[] newBuffer = new char[newBufferSize];
131    
132                            System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
133    
134                            buffer = newBuffer;
135                    }
136    
137                    buffer[index] = (char)c;
138    
139                    index = newIndex;
140            }
141    
142            public void write(String string) {
143                    write(string, 0, string.length());
144            }
145    
146            public void write(String string, int offset, int length) {
147                    if (length <= 0) {
148                            return;
149                    }
150    
151                    int newIndex = index + length;
152    
153                    if (newIndex > buffer.length) {
154                            int newBufferSize = Math.max(buffer.length << 1, newIndex);
155    
156                            char[] newBuffer = new char[newBufferSize];
157    
158                            System.arraycopy(buffer, 0, newBuffer, 0, index);
159    
160                            buffer = newBuffer;
161                    }
162    
163                    string.getChars(offset, offset + length, buffer, index);
164    
165                    index = newIndex;
166            }
167    
168            public int writeTo(CharBuffer charBuffer) {
169                    int length = charBuffer.remaining();
170    
171                    if (length > index) {
172                            length = index;
173                    }
174    
175                    if (length == 0) {
176                            return 0;
177                    }
178    
179                    charBuffer.put(buffer, 0, length);
180    
181                    return length;
182            }
183    
184            public int writeTo(OutputStream outputStream, String charsetName)
185                    throws IOException {
186    
187                    ByteBuffer byteBuffer = CharsetEncoderUtil.encode(
188                            charsetName, buffer, 0, index);
189    
190                    int length = byteBuffer.limit();
191    
192                    outputStream.write(byteBuffer.array(), 0, length);
193    
194                    return length;
195            }
196    
197            public int writeTo(Writer writer) throws IOException {
198                    writer.write(buffer, 0, index);
199    
200                    return index;
201            }
202    
203            protected char[] buffer;
204            protected int index;
205    
206    }