1
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
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 }