1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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(boolean useStringBundler) {
34          if (useStringBundler) {
35              stringBundler = new StringBundler();
36          }
37          else {
38              stringBuilder = new StringBuilder();
39          }
40      }
41  
42      public UnsyncStringWriter(boolean useStringBundler, int initialCapacity) {
43          if (useStringBundler) {
44              stringBundler = new StringBundler(initialCapacity);
45          }
46          else {
47              stringBuilder = new StringBuilder(initialCapacity);
48          }
49      }
50  
51      public UnsyncStringWriter append(char c) {
52          write(c);
53  
54          return this;
55      }
56  
57      public UnsyncStringWriter append(CharSequence charSequence) {
58          if (charSequence == null) {
59              write(StringPool.NULL);
60          }
61          else {
62              write(charSequence.toString());
63          }
64  
65          return this;
66      }
67  
68      public UnsyncStringWriter append(
69          CharSequence charSequence, int start, int end) {
70  
71          if (charSequence == null) {
72              charSequence = StringPool.NULL;
73          }
74  
75          write(charSequence.subSequence(start, end).toString());
76  
77          return this;
78      }
79  
80      public void close() {
81      }
82  
83      public void flush() {
84      }
85  
86      public StringBuilder getStringBuilder() {
87          return stringBuilder;
88      }
89  
90      public StringBundler getStringBundler() {
91          return stringBundler;
92      }
93  
94      public void reset() {
95          if (stringBundler != null) {
96              stringBundler.setIndex(0);
97          }
98          else {
99              stringBuilder.setLength(0);
100         }
101     }
102 
103     public String toString() {
104         if (stringBundler != null) {
105             return stringBundler.toString();
106         }
107         else {
108             return stringBuilder.toString();
109         }
110     }
111 
112     public void write(char[] charArray, int offset, int length) {
113         if (length <= 0) {
114             return;
115         }
116 
117         if (stringBundler != null) {
118             stringBundler.append(new String(charArray, offset, length));
119         }
120         else {
121             stringBuilder.append(charArray, offset, length);
122         }
123     }
124 
125     public void write(char[] charArray) {
126         write(charArray, 0, charArray.length);
127 
128     }
129 
130     public void write(int c) {
131         if (stringBundler != null) {
132             stringBundler.append(String.valueOf((char)c));
133         }
134         else {
135             stringBuilder.append((char)c);
136         }
137     }
138 
139     public void write(String string) {
140         if (stringBundler != null) {
141             stringBundler.append(string);
142         }
143         else {
144             stringBuilder.append(string);
145         }
146     }
147 
148     public void write(String string, int offset, int length) {
149         if (stringBundler != null) {
150             stringBundler.append(string.substring(offset, offset + length));
151         }
152         else {
153             stringBuilder.append(string.substring(offset, offset + length));
154         }
155     }
156 
157     protected StringBuilder stringBuilder;
158     protected StringBundler stringBundler;
159 
160 }