author | bpb |
Thu, 06 Jun 2019 08:11:25 -0700 | |
changeset 55259 | 61fff1345ee6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
38373
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
2 |
* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
28 |
||
29 |
/** |
|
30 |
* A character stream that collects its output in a string buffer, which can |
|
31 |
* then be used to construct a string. |
|
32 |
* <p> |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
33 |
* Closing a {@code StringWriter} has no effect. The methods in this class |
2 | 34 |
* can be called after the stream has been closed without generating an |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
35 |
* {@code IOException}. |
2 | 36 |
* |
37 |
* @author Mark Reinhold |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
5506
diff
changeset
|
38 |
* @since 1.1 |
2 | 39 |
*/ |
40 |
||
41 |
public class StringWriter extends Writer { |
|
42 |
||
43 |
private StringBuffer buf; |
|
44 |
||
45 |
/** |
|
46 |
* Create a new string writer using the default initial string-buffer |
|
47 |
* size. |
|
48 |
*/ |
|
49 |
public StringWriter() { |
|
50 |
buf = new StringBuffer(); |
|
51 |
lock = buf; |
|
52 |
} |
|
53 |
||
54 |
/** |
|
55 |
* Create a new string writer using the specified initial string-buffer |
|
56 |
* size. |
|
57 |
* |
|
58 |
* @param initialSize |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
59 |
* The number of {@code char} values that will fit into this buffer |
2 | 60 |
* before it is automatically expanded |
61 |
* |
|
62 |
* @throws IllegalArgumentException |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
63 |
* If {@code initialSize} is negative |
2 | 64 |
*/ |
65 |
public StringWriter(int initialSize) { |
|
66 |
if (initialSize < 0) { |
|
67 |
throw new IllegalArgumentException("Negative buffer size"); |
|
68 |
} |
|
69 |
buf = new StringBuffer(initialSize); |
|
70 |
lock = buf; |
|
71 |
} |
|
72 |
||
73 |
/** |
|
74 |
* Write a single character. |
|
75 |
*/ |
|
76 |
public void write(int c) { |
|
77 |
buf.append((char) c); |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Write a portion of an array of characters. |
|
82 |
* |
|
83 |
* @param cbuf Array of characters |
|
84 |
* @param off Offset from which to start writing characters |
|
85 |
* @param len Number of characters to write |
|
38373
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
86 |
* |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
87 |
* @throws IndexOutOfBoundsException |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
88 |
* If {@code off} is negative, or {@code len} is negative, |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
89 |
* or {@code off + len} is negative or greater than the length |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
90 |
* of the given array |
2 | 91 |
*/ |
92 |
public void write(char cbuf[], int off, int len) { |
|
93 |
if ((off < 0) || (off > cbuf.length) || (len < 0) || |
|
94 |
((off + len) > cbuf.length) || ((off + len) < 0)) { |
|
95 |
throw new IndexOutOfBoundsException(); |
|
96 |
} else if (len == 0) { |
|
97 |
return; |
|
98 |
} |
|
99 |
buf.append(cbuf, off, len); |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Write a string. |
|
104 |
*/ |
|
105 |
public void write(String str) { |
|
106 |
buf.append(str); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Write a portion of a string. |
|
111 |
* |
|
112 |
* @param str String to be written |
|
113 |
* @param off Offset from which to start writing characters |
|
114 |
* @param len Number of characters to write |
|
38373
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
115 |
* |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
116 |
* @throws IndexOutOfBoundsException |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
117 |
* If {@code off} is negative, or {@code len} is negative, |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
118 |
* or {@code off + len} is negative or greater than the length |
21f4f5eee7cc
8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents:
32033
diff
changeset
|
119 |
* of the given string |
2 | 120 |
*/ |
121 |
public void write(String str, int off, int len) { |
|
31471
ae27c6f1d8bf
8077242: (str) Optimize AbstractStringBuilder.append(CharSequence, int, int) for String argument
igerasim
parents:
25859
diff
changeset
|
122 |
buf.append(str, off, off + len); |
2 | 123 |
} |
124 |
||
125 |
/** |
|
126 |
* Appends the specified character sequence to this writer. |
|
127 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
128 |
* <p> An invocation of this method of the form {@code out.append(csq)} |
2 | 129 |
* behaves in exactly the same way as the invocation |
130 |
* |
|
131 |
* <pre> |
|
132 |
* out.write(csq.toString()) </pre> |
|
133 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
134 |
* <p> Depending on the specification of {@code toString} for the |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
135 |
* character sequence {@code csq}, the entire sequence may not be |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
136 |
* appended. For instance, invoking the {@code toString} method of a |
2 | 137 |
* character buffer will return a subsequence whose content depends upon |
138 |
* the buffer's position and limit. |
|
139 |
* |
|
140 |
* @param csq |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
141 |
* The character sequence to append. If {@code csq} is |
40410 | 142 |
* {@code null}, then the four characters {@code "null"} are |
2 | 143 |
* appended to this writer. |
144 |
* |
|
145 |
* @return This writer |
|
146 |
* |
|
147 |
* @since 1.5 |
|
148 |
*/ |
|
149 |
public StringWriter append(CharSequence csq) { |
|
40410 | 150 |
write(String.valueOf(csq)); |
2 | 151 |
return this; |
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Appends a subsequence of the specified character sequence to this writer. |
|
156 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
157 |
* <p> An invocation of this method of the form |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
158 |
* {@code out.append(csq, start, end)} when {@code csq} |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
159 |
* is not {@code null}, behaves in |
2 | 160 |
* exactly the same way as the invocation |
161 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
162 |
* <pre>{@code |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
163 |
* out.write(csq.subSequence(start, end).toString()) |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
164 |
* }</pre> |
2 | 165 |
* |
166 |
* @param csq |
|
167 |
* The character sequence from which a subsequence will be |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
168 |
* appended. If {@code csq} is {@code null}, then characters |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
169 |
* will be appended as if {@code csq} contained the four |
40410 | 170 |
* characters {@code "null"}. |
2 | 171 |
* |
172 |
* @param start |
|
173 |
* The index of the first character in the subsequence |
|
174 |
* |
|
175 |
* @param end |
|
176 |
* The index of the character following the last character in the |
|
177 |
* subsequence |
|
178 |
* |
|
179 |
* @return This writer |
|
180 |
* |
|
181 |
* @throws IndexOutOfBoundsException |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
182 |
* If {@code start} or {@code end} are negative, {@code start} |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
183 |
* is greater than {@code end}, or {@code end} is greater than |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
184 |
* {@code csq.length()} |
2 | 185 |
* |
186 |
* @since 1.5 |
|
187 |
*/ |
|
188 |
public StringWriter append(CharSequence csq, int start, int end) { |
|
40410 | 189 |
if (csq == null) csq = "null"; |
190 |
return append(csq.subSequence(start, end)); |
|
2 | 191 |
} |
192 |
||
193 |
/** |
|
194 |
* Appends the specified character to this writer. |
|
195 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
196 |
* <p> An invocation of this method of the form {@code out.append(c)} |
2 | 197 |
* behaves in exactly the same way as the invocation |
198 |
* |
|
199 |
* <pre> |
|
200 |
* out.write(c) </pre> |
|
201 |
* |
|
202 |
* @param c |
|
203 |
* The 16-bit character to append |
|
204 |
* |
|
205 |
* @return This writer |
|
206 |
* |
|
207 |
* @since 1.5 |
|
208 |
*/ |
|
209 |
public StringWriter append(char c) { |
|
210 |
write(c); |
|
211 |
return this; |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Return the buffer's current value as a string. |
|
216 |
*/ |
|
217 |
public String toString() { |
|
218 |
return buf.toString(); |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* Return the string buffer itself. |
|
223 |
* |
|
224 |
* @return StringBuffer holding the current buffer value. |
|
225 |
*/ |
|
226 |
public StringBuffer getBuffer() { |
|
227 |
return buf; |
|
228 |
} |
|
229 |
||
230 |
/** |
|
231 |
* Flush the stream. |
|
232 |
*/ |
|
233 |
public void flush() { |
|
234 |
} |
|
235 |
||
236 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
237 |
* Closing a {@code StringWriter} has no effect. The methods in this |
2 | 238 |
* class can be called after the stream has been closed without generating |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31471
diff
changeset
|
239 |
* an {@code IOException}. |
2 | 240 |
*/ |
241 |
public void close() throws IOException { |
|
242 |
} |
|
243 |
||
244 |
} |