001    /**
002     * Copyright (c) 2000-2013 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.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.io.Writer;
021    
022    /**
023     * <p>
024     * See http://issues.liferay.com/browse/LPS-6648.
025     * </p>
026     *
027     * @author Shuyang Zhou
028     */
029    public class UnsyncStringWriter extends Writer {
030    
031            public UnsyncStringWriter() {
032                    this(true);
033            }
034    
035            public UnsyncStringWriter(boolean useStringBundler) {
036                    if (useStringBundler) {
037                            stringBundler = new StringBundler();
038                    }
039                    else {
040                            stringBuilder = new StringBuilder();
041                    }
042            }
043    
044            public UnsyncStringWriter(boolean useStringBundler, int initialCapacity) {
045                    if (useStringBundler) {
046                            stringBundler = new StringBundler(initialCapacity);
047                    }
048                    else {
049                            stringBuilder = new StringBuilder(initialCapacity);
050                    }
051            }
052    
053            public UnsyncStringWriter(int initialCapacity) {
054                    this(true, initialCapacity);
055            }
056    
057            @Override
058            public UnsyncStringWriter append(char c) {
059                    write(c);
060    
061                    return this;
062            }
063    
064            @Override
065            public UnsyncStringWriter append(CharSequence charSequence) {
066                    if (charSequence == null) {
067                            write(StringPool.NULL);
068                    }
069                    else {
070                            write(charSequence.toString());
071                    }
072    
073                    return this;
074            }
075    
076            @Override
077            public UnsyncStringWriter append(
078                    CharSequence charSequence, int start, int end) {
079    
080                    if (charSequence == null) {
081                            charSequence = StringPool.NULL;
082                    }
083    
084                    write(charSequence.subSequence(start, end).toString());
085    
086                    return this;
087            }
088    
089            @Override
090            public void close() {
091            }
092    
093            @Override
094            public void flush() {
095            }
096    
097            public StringBuilder getStringBuilder() {
098                    return stringBuilder;
099            }
100    
101            public StringBundler getStringBundler() {
102                    return stringBundler;
103            }
104    
105            public void reset() {
106                    if (stringBundler != null) {
107                            stringBundler.setIndex(0);
108                    }
109                    else {
110                            stringBuilder.setLength(0);
111                    }
112            }
113    
114            @Override
115            public String toString() {
116                    if (stringBundler != null) {
117                            return stringBundler.toString();
118                    }
119                    else {
120                            return stringBuilder.toString();
121                    }
122            }
123    
124            @Override
125            public void write(char[] chars) {
126                    write(chars, 0, chars.length);
127    
128            }
129    
130            @Override
131            public void write(char[] chars, int offset, int length) {
132                    if (length <= 0) {
133                            return;
134                    }
135    
136                    if (stringBundler != null) {
137                            stringBundler.append(new String(chars, offset, length));
138                    }
139                    else {
140                            stringBuilder.append(chars, offset, length);
141                    }
142            }
143    
144            @Override
145            public void write(int c) {
146                    if (stringBundler != null) {
147                            char ch = (char)c;
148    
149                            if (ch <= 127) {
150                                    stringBundler.append(StringPool.ASCII_TABLE[ch]);
151                            }
152                            else {
153                                    stringBundler.append(String.valueOf(ch));
154                            }
155                    }
156                    else {
157                            stringBuilder.append((char)c);
158                    }
159            }
160    
161            @Override
162            public void write(String string) {
163                    if (stringBundler != null) {
164                            stringBundler.append(string);
165                    }
166                    else {
167                            stringBuilder.append(string);
168                    }
169            }
170    
171            @Override
172            public void write(String string, int offset, int length) {
173                    if ((string == null) ||
174                            ((offset == 0) && (length == string.length()))) {
175    
176                            write(string);
177                    }
178                    else if (stringBundler != null) {
179                            stringBundler.append(string.substring(offset, offset + length));
180                    }
181                    else {
182                            stringBuilder.append(string.substring(offset, offset + length));
183                    }
184            }
185    
186            protected StringBuilder stringBuilder;
187            protected StringBundler stringBundler;
188    
189    }