1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.io.unsync;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringPool;
19  
20  import java.io.Writer;
21  
22  /**
23   * <a href="UnsyncStringWriter.java.html"><b><i>View Source</i></b></a>
24   *
25   * <p>
26   * See http://issues.liferay.com/browse/LPS-6648.
27   * </p>
28   *
29   * @author Shuyang Zhou
30   */
31  public class UnsyncStringWriter extends Writer {
32  
33      public UnsyncStringWriter() {
34          this(true);
35      }
36  
37      public UnsyncStringWriter(boolean useStringBundler) {
38          if (useStringBundler) {
39              stringBundler = new StringBundler();
40          }
41          else {
42              stringBuilder = new StringBuilder();
43          }
44      }
45  
46      public UnsyncStringWriter(boolean useStringBundler, int initialCapacity) {
47          if (useStringBundler) {
48              stringBundler = new StringBundler(initialCapacity);
49          }
50          else {
51              stringBuilder = new StringBuilder(initialCapacity);
52          }
53      }
54  
55      public UnsyncStringWriter(int initialCapacity) {
56          this(true, initialCapacity);
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             char ch = (char)c;
141 
142             if (ch <= 127) {
143                 stringBundler.append(StringPool.ASCII_TABLE[ch]);
144             }
145             else {
146                 stringBundler.append(String.valueOf(ch));
147             }
148         }
149         else {
150             stringBuilder.append((char)c);
151         }
152     }
153 
154     public void write(String string) {
155         if (stringBundler != null) {
156             stringBundler.append(string);
157         }
158         else {
159             stringBuilder.append(string);
160         }
161     }
162 
163     public void write(String string, int offset, int length) {
164         if (stringBundler != null) {
165             stringBundler.append(string.substring(offset, offset + length));
166         }
167         else {
168             stringBuilder.append(string.substring(offset, offset + length));
169         }
170     }
171 
172     protected StringBuilder stringBuilder;
173     protected StringBundler stringBundler;
174 
175 }