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            @Override
130            public void write(char[] chars, int offset, int length) {
131                    if (length <= 0) {
132                            return;
133                    }
134    
135                    if (stringBundler != null) {
136                            stringBundler.append(new String(chars, offset, length));
137                    }
138                    else {
139                            stringBuilder.append(chars, offset, length);
140                    }
141            }
142    
143            @Override
144            public void write(int c) {
145                    if (stringBundler != null) {
146                            char ch = (char)c;
147    
148                            if (ch <= 127) {
149                                    stringBundler.append(StringPool.ASCII_TABLE[ch]);
150                            }
151                            else {
152                                    stringBundler.append(String.valueOf(ch));
153                            }
154                    }
155                    else {
156                            stringBuilder.append((char)c);
157                    }
158            }
159    
160            @Override
161            public void write(String string) {
162                    if (stringBundler != null) {
163                            stringBundler.append(string);
164                    }
165                    else {
166                            stringBuilder.append(string);
167                    }
168            }
169    
170            @Override
171            public void write(String string, int offset, int length) {
172                    if ((string == null) ||
173                            ((offset == 0) && (length == string.length()))) {
174    
175                            write(string);
176                    }
177                    else if (stringBundler != null) {
178                            stringBundler.append(string.substring(offset, offset + length));
179                    }
180                    else {
181                            stringBuilder.append(string.substring(offset, offset + length));
182                    }
183            }
184    
185            protected StringBuilder stringBuilder;
186            protected StringBundler stringBundler;
187    
188    }