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() {
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             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 }