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.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            public UnsyncStringWriter append(char c) {
058                    write(c);
059    
060                    return this;
061            }
062    
063            public UnsyncStringWriter append(CharSequence charSequence) {
064                    if (charSequence == null) {
065                            write(StringPool.NULL);
066                    }
067                    else {
068                            write(charSequence.toString());
069                    }
070    
071                    return this;
072            }
073    
074            public UnsyncStringWriter append(
075                    CharSequence charSequence, int start, int end) {
076    
077                    if (charSequence == null) {
078                            charSequence = StringPool.NULL;
079                    }
080    
081                    write(charSequence.subSequence(start, end).toString());
082    
083                    return this;
084            }
085    
086            public void close() {
087            }
088    
089            public void flush() {
090            }
091    
092            public StringBuilder getStringBuilder() {
093                    return stringBuilder;
094            }
095    
096            public StringBundler getStringBundler() {
097                    return stringBundler;
098            }
099    
100            public void reset() {
101                    if (stringBundler != null) {
102                            stringBundler.setIndex(0);
103                    }
104                    else {
105                            stringBuilder.setLength(0);
106                    }
107            }
108    
109            public String toString() {
110                    if (stringBundler != null) {
111                            return stringBundler.toString();
112                    }
113                    else {
114                            return stringBuilder.toString();
115                    }
116            }
117    
118            public void write(char[] charArray, int offset, int length) {
119                    if (length <= 0) {
120                            return;
121                    }
122    
123                    if (stringBundler != null) {
124                            stringBundler.append(new String(charArray, offset, length));
125                    }
126                    else {
127                            stringBuilder.append(charArray, offset, length);
128                    }
129            }
130    
131            public void write(char[] charArray) {
132                    write(charArray, 0, charArray.length);
133    
134            }
135    
136            public void write(int c) {
137                    if (stringBundler != null) {
138                            stringBundler.append(String.valueOf((char)c));
139                    }
140                    else {
141                            stringBuilder.append((char)c);
142                    }
143            }
144    
145            public void write(String string) {
146                    if (stringBundler != null) {
147                            stringBundler.append(string);
148                    }
149                    else {
150                            stringBuilder.append(string);
151                    }
152            }
153    
154            public void write(String string, int offset, int length) {
155                    if (stringBundler != null) {
156                            stringBundler.append(string.substring(offset, offset + length));
157                    }
158                    else {
159                            stringBuilder.append(string.substring(offset, offset + length));
160                    }
161            }
162    
163            protected StringBuilder stringBuilder;
164            protected StringBundler stringBundler;
165    
166    }