1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.io.unsync;
24  
25  import com.liferay.portal.kernel.util.StringBundler;
26  import com.liferay.portal.kernel.util.StringPool;
27  
28  import java.io.Writer;
29  
30  /**
31   * <a href="UnsyncStringWriter.java.html"><b><i>View Source</i></b></a>
32   *
33   * <p>
34   * See http://support.liferay.com/browse/LPS-6648.
35   * </p>
36   *
37   * @author Shuyang Zhou
38   */
39  public class UnsyncStringWriter extends Writer {
40  
41      public UnsyncStringWriter(boolean useStringBundler) {
42          if (useStringBundler) {
43              stringBundler = new StringBundler();
44          }
45          else {
46              stringBuilder = new StringBuilder();
47          }
48      }
49  
50      public UnsyncStringWriter(boolean useStringBundler, int initialCapacity) {
51          if (useStringBundler) {
52              stringBundler = new StringBundler(initialCapacity);
53          }
54          else {
55              stringBuilder = new StringBuilder(initialCapacity);
56          }
57      }
58  
59      public UnsyncStringWriter append(char c) {
60          write(c);
61  
62          return this;
63      }
64  
65      public UnsyncStringWriter append(CharSequence charSequence) {
66          if (charSequence == null) {
67              write(StringPool.NULL);
68          }
69          else {
70              write(charSequence.toString());
71          }
72  
73          return this;
74      }
75  
76      public UnsyncStringWriter append(
77          CharSequence charSequence, int start, int end) {
78  
79          if (charSequence == null) {
80              charSequence = StringPool.NULL;
81          }
82  
83          write(charSequence.subSequence(start, end).toString());
84  
85          return this;
86      }
87  
88      public void close() {
89      }
90  
91      public void flush() {
92      }
93  
94      public StringBuilder getStringBuilder() {
95          return stringBuilder;
96      }
97  
98      public StringBundler getStringBundler() {
99          return stringBundler;
100     }
101 
102     public void reset() {
103         if (stringBundler != null) {
104             stringBundler.setIndex(0);
105         }
106         else {
107             stringBuilder.setLength(0);
108         }
109     }
110 
111     public String toString() {
112         if (stringBundler != null) {
113             return stringBundler.toString();
114         }
115         else {
116             return stringBuilder.toString();
117         }
118     }
119 
120     public void write(char[] charArray, int offset, int length) {
121         if (length <= 0) {
122             return;
123         }
124 
125         if (stringBundler != null) {
126             stringBundler.append(new String(charArray, offset, length));
127         }
128         else {
129             stringBuilder.append(charArray, offset, length);
130         }
131     }
132 
133     public void write(char[] charArray) {
134         write(charArray, 0, charArray.length);
135 
136     }
137 
138     public void write(int c) {
139         if (stringBundler != null) {
140             stringBundler.append(String.valueOf((char)c));
141         }
142         else {
143             stringBuilder.append((char)c);
144         }
145     }
146 
147     public void write(String string) {
148         if (stringBundler != null) {
149             stringBundler.append(string);
150         }
151         else {
152             stringBuilder.append(string);
153         }
154     }
155 
156     public void write(String string, int offset, int length) {
157         if (stringBundler != null) {
158             stringBundler.append(string.substring(offset, offset + length));
159         }
160         else {
161             stringBuilder.append(string.substring(offset, offset + length));
162         }
163     }
164 
165     protected StringBuilder stringBuilder;
166     protected StringBundler stringBundler;
167 
168 }