1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/mini/src/main/java/java/lang/StringBuffer.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,604 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +
1.32 +/**
1.33 + * A thread-safe, mutable sequence of characters.
1.34 + * A string buffer is like a {@link String}, but can be modified. At any
1.35 + * point in time it contains some particular sequence of characters, but
1.36 + * the length and content of the sequence can be changed through certain
1.37 + * method calls.
1.38 + * <p>
1.39 + * String buffers are safe for use by multiple threads. The methods
1.40 + * are synchronized where necessary so that all the operations on any
1.41 + * particular instance behave as if they occur in some serial order
1.42 + * that is consistent with the order of the method calls made by each of
1.43 + * the individual threads involved.
1.44 + * <p>
1.45 + * The principal operations on a <code>StringBuffer</code> are the
1.46 + * <code>append</code> and <code>insert</code> methods, which are
1.47 + * overloaded so as to accept data of any type. Each effectively
1.48 + * converts a given datum to a string and then appends or inserts the
1.49 + * characters of that string to the string buffer. The
1.50 + * <code>append</code> method always adds these characters at the end
1.51 + * of the buffer; the <code>insert</code> method adds the characters at
1.52 + * a specified point.
1.53 + * <p>
1.54 + * For example, if <code>z</code> refers to a string buffer object
1.55 + * whose current contents are "<code>start</code>", then
1.56 + * the method call <code>z.append("le")</code> would cause the string
1.57 + * buffer to contain "<code>startle</code>", whereas
1.58 + * <code>z.insert(4, "le")</code> would alter the string buffer to
1.59 + * contain "<code>starlet</code>".
1.60 + * <p>
1.61 + * In general, if sb refers to an instance of a <code>StringBuffer</code>,
1.62 + * then <code>sb.append(x)</code> has the same effect as
1.63 + * <code>sb.insert(sb.length(), x)</code>.
1.64 + * <p>
1.65 + * Whenever an operation occurs involving a source sequence (such as
1.66 + * appending or inserting from a source sequence) this class synchronizes
1.67 + * only on the string buffer performing the operation, not on the source.
1.68 + * <p>
1.69 + * Every string buffer has a capacity. As long as the length of the
1.70 + * character sequence contained in the string buffer does not exceed
1.71 + * the capacity, it is not necessary to allocate a new internal
1.72 + * buffer array. If the internal buffer overflows, it is
1.73 + * automatically made larger.
1.74 + *
1.75 + * As of release JDK 5, this class has been supplemented with an equivalent
1.76 + * class designed for use by a single thread, {@link StringBuilder}. The
1.77 + * <tt>StringBuilder</tt> class should generally be used in preference to
1.78 + * this one, as it supports all of the same operations but it is faster, as
1.79 + * it performs no synchronization.
1.80 + *
1.81 + * @author Arthur van Hoff
1.82 + * @see java.lang.StringBuilder
1.83 + * @see java.lang.String
1.84 + * @since JDK1.0
1.85 + */
1.86 + public final class StringBuffer
1.87 + extends AbstractStringBuilder
1.88 + implements java.io.Serializable, CharSequence
1.89 +{
1.90 +
1.91 + /** use serialVersionUID from JDK 1.0.2 for interoperability */
1.92 + static final long serialVersionUID = 3388685877147921107L;
1.93 +
1.94 + /**
1.95 + * Constructs a string buffer with no characters in it and an
1.96 + * initial capacity of 16 characters.
1.97 + */
1.98 + public StringBuffer() {
1.99 + super(16);
1.100 + }
1.101 +
1.102 + /**
1.103 + * Constructs a string buffer with no characters in it and
1.104 + * the specified initial capacity.
1.105 + *
1.106 + * @param capacity the initial capacity.
1.107 + * @exception NegativeArraySizeException if the <code>capacity</code>
1.108 + * argument is less than <code>0</code>.
1.109 + */
1.110 + public StringBuffer(int capacity) {
1.111 + super(capacity);
1.112 + }
1.113 +
1.114 + /**
1.115 + * Constructs a string buffer initialized to the contents of the
1.116 + * specified string. The initial capacity of the string buffer is
1.117 + * <code>16</code> plus the length of the string argument.
1.118 + *
1.119 + * @param str the initial contents of the buffer.
1.120 + * @exception NullPointerException if <code>str</code> is <code>null</code>
1.121 + */
1.122 + public StringBuffer(String str) {
1.123 + super(str.length() + 16);
1.124 + append(str);
1.125 + }
1.126 +
1.127 + /**
1.128 + * Constructs a string buffer that contains the same characters
1.129 + * as the specified <code>CharSequence</code>. The initial capacity of
1.130 + * the string buffer is <code>16</code> plus the length of the
1.131 + * <code>CharSequence</code> argument.
1.132 + * <p>
1.133 + * If the length of the specified <code>CharSequence</code> is
1.134 + * less than or equal to zero, then an empty buffer of capacity
1.135 + * <code>16</code> is returned.
1.136 + *
1.137 + * @param seq the sequence to copy.
1.138 + * @exception NullPointerException if <code>seq</code> is <code>null</code>
1.139 + * @since 1.5
1.140 + */
1.141 + public StringBuffer(CharSequence seq) {
1.142 + this(seq.length() + 16);
1.143 + append(seq);
1.144 + }
1.145 +
1.146 + public synchronized int length() {
1.147 + return count;
1.148 + }
1.149 +
1.150 + public synchronized int capacity() {
1.151 + return value.length;
1.152 + }
1.153 +
1.154 +
1.155 + public synchronized void ensureCapacity(int minimumCapacity) {
1.156 + if (minimumCapacity > value.length) {
1.157 + expandCapacity(minimumCapacity);
1.158 + }
1.159 + }
1.160 +
1.161 + /**
1.162 + * @since 1.5
1.163 + */
1.164 + public synchronized void trimToSize() {
1.165 + super.trimToSize();
1.166 + }
1.167 +
1.168 + /**
1.169 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.170 + * @see #length()
1.171 + */
1.172 + public synchronized void setLength(int newLength) {
1.173 + super.setLength(newLength);
1.174 + }
1.175 +
1.176 + /**
1.177 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.178 + * @see #length()
1.179 + */
1.180 + public synchronized char charAt(int index) {
1.181 + if ((index < 0) || (index >= count))
1.182 + throw new StringIndexOutOfBoundsException(index);
1.183 + return value[index];
1.184 + }
1.185 +
1.186 + /**
1.187 + * @since 1.5
1.188 + */
1.189 + public synchronized int codePointAt(int index) {
1.190 + return super.codePointAt(index);
1.191 + }
1.192 +
1.193 + /**
1.194 + * @since 1.5
1.195 + */
1.196 + public synchronized int codePointBefore(int index) {
1.197 + return super.codePointBefore(index);
1.198 + }
1.199 +
1.200 + /**
1.201 + * @since 1.5
1.202 + */
1.203 + public synchronized int codePointCount(int beginIndex, int endIndex) {
1.204 + return super.codePointCount(beginIndex, endIndex);
1.205 + }
1.206 +
1.207 + /**
1.208 + * @since 1.5
1.209 + */
1.210 + public synchronized int offsetByCodePoints(int index, int codePointOffset) {
1.211 + return super.offsetByCodePoints(index, codePointOffset);
1.212 + }
1.213 +
1.214 + /**
1.215 + * @throws NullPointerException {@inheritDoc}
1.216 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.217 + */
1.218 + public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
1.219 + int dstBegin)
1.220 + {
1.221 + super.getChars(srcBegin, srcEnd, dst, dstBegin);
1.222 + }
1.223 +
1.224 + /**
1.225 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.226 + * @see #length()
1.227 + */
1.228 + public synchronized void setCharAt(int index, char ch) {
1.229 + if ((index < 0) || (index >= count))
1.230 + throw new StringIndexOutOfBoundsException(index);
1.231 + value[index] = ch;
1.232 + }
1.233 +
1.234 + public synchronized StringBuffer append(Object obj) {
1.235 + super.append(String.valueOf(obj));
1.236 + return this;
1.237 + }
1.238 +
1.239 + public synchronized StringBuffer append(String str) {
1.240 + super.append(str);
1.241 + return this;
1.242 + }
1.243 +
1.244 + /**
1.245 + * Appends the specified <tt>StringBuffer</tt> to this sequence.
1.246 + * <p>
1.247 + * The characters of the <tt>StringBuffer</tt> argument are appended,
1.248 + * in order, to the contents of this <tt>StringBuffer</tt>, increasing the
1.249 + * length of this <tt>StringBuffer</tt> by the length of the argument.
1.250 + * If <tt>sb</tt> is <tt>null</tt>, then the four characters
1.251 + * <tt>"null"</tt> are appended to this <tt>StringBuffer</tt>.
1.252 + * <p>
1.253 + * Let <i>n</i> be the length of the old character sequence, the one
1.254 + * contained in the <tt>StringBuffer</tt> just prior to execution of the
1.255 + * <tt>append</tt> method. Then the character at index <i>k</i> in
1.256 + * the new character sequence is equal to the character at index <i>k</i>
1.257 + * in the old character sequence, if <i>k</i> is less than <i>n</i>;
1.258 + * otherwise, it is equal to the character at index <i>k-n</i> in the
1.259 + * argument <code>sb</code>.
1.260 + * <p>
1.261 + * This method synchronizes on <code>this</code> (the destination)
1.262 + * object but does not synchronize on the source (<code>sb</code>).
1.263 + *
1.264 + * @param sb the <tt>StringBuffer</tt> to append.
1.265 + * @return a reference to this object.
1.266 + * @since 1.4
1.267 + */
1.268 + public synchronized StringBuffer append(StringBuffer sb) {
1.269 + super.append(sb);
1.270 + return this;
1.271 + }
1.272 +
1.273 +
1.274 + /**
1.275 + * Appends the specified <code>CharSequence</code> to this
1.276 + * sequence.
1.277 + * <p>
1.278 + * The characters of the <code>CharSequence</code> argument are appended,
1.279 + * in order, increasing the length of this sequence by the length of the
1.280 + * argument.
1.281 + *
1.282 + * <p>The result of this method is exactly the same as if it were an
1.283 + * invocation of this.append(s, 0, s.length());
1.284 + *
1.285 + * <p>This method synchronizes on this (the destination)
1.286 + * object but does not synchronize on the source (<code>s</code>).
1.287 + *
1.288 + * <p>If <code>s</code> is <code>null</code>, then the four characters
1.289 + * <code>"null"</code> are appended.
1.290 + *
1.291 + * @param s the <code>CharSequence</code> to append.
1.292 + * @return a reference to this object.
1.293 + * @since 1.5
1.294 + */
1.295 + public StringBuffer append(CharSequence s) {
1.296 + // Note, synchronization achieved via other invocations
1.297 + if (s == null)
1.298 + s = "null";
1.299 + if (s instanceof String)
1.300 + return this.append((String)s);
1.301 + if (s instanceof StringBuffer)
1.302 + return this.append((StringBuffer)s);
1.303 + return this.append(s, 0, s.length());
1.304 + }
1.305 +
1.306 + /**
1.307 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.308 + * @since 1.5
1.309 + */
1.310 + public synchronized StringBuffer append(CharSequence s, int start, int end)
1.311 + {
1.312 + super.append(s, start, end);
1.313 + return this;
1.314 + }
1.315 +
1.316 + public synchronized StringBuffer append(char[] str) {
1.317 + super.append(str);
1.318 + return this;
1.319 + }
1.320 +
1.321 + /**
1.322 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.323 + */
1.324 + public synchronized StringBuffer append(char[] str, int offset, int len) {
1.325 + super.append(str, offset, len);
1.326 + return this;
1.327 + }
1.328 +
1.329 + public synchronized StringBuffer append(boolean b) {
1.330 + super.append(b);
1.331 + return this;
1.332 + }
1.333 +
1.334 + public synchronized StringBuffer append(char c) {
1.335 + super.append(c);
1.336 + return this;
1.337 + }
1.338 +
1.339 + public synchronized StringBuffer append(int i) {
1.340 + super.append(i);
1.341 + return this;
1.342 + }
1.343 +
1.344 + /**
1.345 + * @since 1.5
1.346 + */
1.347 + public synchronized StringBuffer appendCodePoint(int codePoint) {
1.348 + super.appendCodePoint(codePoint);
1.349 + return this;
1.350 + }
1.351 +
1.352 + public synchronized StringBuffer append(long lng) {
1.353 + super.append(lng);
1.354 + return this;
1.355 + }
1.356 +
1.357 + public synchronized StringBuffer append(float f) {
1.358 + super.append(f);
1.359 + return this;
1.360 + }
1.361 +
1.362 + public synchronized StringBuffer append(double d) {
1.363 + super.append(d);
1.364 + return this;
1.365 + }
1.366 +
1.367 + /**
1.368 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.369 + * @since 1.2
1.370 + */
1.371 + public synchronized StringBuffer delete(int start, int end) {
1.372 + super.delete(start, end);
1.373 + return this;
1.374 + }
1.375 +
1.376 + /**
1.377 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.378 + * @since 1.2
1.379 + */
1.380 + public synchronized StringBuffer deleteCharAt(int index) {
1.381 + super.deleteCharAt(index);
1.382 + return this;
1.383 + }
1.384 +
1.385 + /**
1.386 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.387 + * @since 1.2
1.388 + */
1.389 + public synchronized StringBuffer replace(int start, int end, String str) {
1.390 + super.replace(start, end, str);
1.391 + return this;
1.392 + }
1.393 +
1.394 + /**
1.395 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.396 + * @since 1.2
1.397 + */
1.398 + public synchronized String substring(int start) {
1.399 + return substring(start, count);
1.400 + }
1.401 +
1.402 + /**
1.403 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.404 + * @since 1.4
1.405 + */
1.406 + public synchronized CharSequence subSequence(int start, int end) {
1.407 + return super.substring(start, end);
1.408 + }
1.409 +
1.410 + /**
1.411 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.412 + * @since 1.2
1.413 + */
1.414 + public synchronized String substring(int start, int end) {
1.415 + return super.substring(start, end);
1.416 + }
1.417 +
1.418 + /**
1.419 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.420 + * @since 1.2
1.421 + */
1.422 + public synchronized StringBuffer insert(int index, char[] str, int offset,
1.423 + int len)
1.424 + {
1.425 + super.insert(index, str, offset, len);
1.426 + return this;
1.427 + }
1.428 +
1.429 + /**
1.430 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.431 + */
1.432 + public synchronized StringBuffer insert(int offset, Object obj) {
1.433 + super.insert(offset, String.valueOf(obj));
1.434 + return this;
1.435 + }
1.436 +
1.437 + /**
1.438 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.439 + */
1.440 + public synchronized StringBuffer insert(int offset, String str) {
1.441 + super.insert(offset, str);
1.442 + return this;
1.443 + }
1.444 +
1.445 + /**
1.446 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.447 + */
1.448 + public synchronized StringBuffer insert(int offset, char[] str) {
1.449 + super.insert(offset, str);
1.450 + return this;
1.451 + }
1.452 +
1.453 + /**
1.454 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.455 + * @since 1.5
1.456 + */
1.457 + public StringBuffer insert(int dstOffset, CharSequence s) {
1.458 + // Note, synchronization achieved via other invocations
1.459 + if (s == null)
1.460 + s = "null";
1.461 + if (s instanceof String)
1.462 + return this.insert(dstOffset, (String)s);
1.463 + return this.insert(dstOffset, s, 0, s.length());
1.464 + }
1.465 +
1.466 + /**
1.467 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.468 + * @since 1.5
1.469 + */
1.470 + public synchronized StringBuffer insert(int dstOffset, CharSequence s,
1.471 + int start, int end)
1.472 + {
1.473 + super.insert(dstOffset, s, start, end);
1.474 + return this;
1.475 + }
1.476 +
1.477 + /**
1.478 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.479 + */
1.480 + public StringBuffer insert(int offset, boolean b) {
1.481 + return insert(offset, String.valueOf(b));
1.482 + }
1.483 +
1.484 + /**
1.485 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.486 + */
1.487 + public synchronized StringBuffer insert(int offset, char c) {
1.488 + super.insert(offset, c);
1.489 + return this;
1.490 + }
1.491 +
1.492 + /**
1.493 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.494 + */
1.495 + public StringBuffer insert(int offset, int i) {
1.496 + return insert(offset, String.valueOf(i));
1.497 + }
1.498 +
1.499 + /**
1.500 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.501 + */
1.502 + public StringBuffer insert(int offset, long l) {
1.503 + return insert(offset, String.valueOf(l));
1.504 + }
1.505 +
1.506 + /**
1.507 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.508 + */
1.509 + public StringBuffer insert(int offset, float f) {
1.510 + return insert(offset, String.valueOf(f));
1.511 + }
1.512 +
1.513 + /**
1.514 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.515 + */
1.516 + public StringBuffer insert(int offset, double d) {
1.517 + return insert(offset, String.valueOf(d));
1.518 + }
1.519 +
1.520 + /**
1.521 + * @throws NullPointerException {@inheritDoc}
1.522 + * @since 1.4
1.523 + */
1.524 + public int indexOf(String str) {
1.525 + return indexOf(str, 0);
1.526 + }
1.527 +
1.528 + /**
1.529 + * @throws NullPointerException {@inheritDoc}
1.530 + * @since 1.4
1.531 + */
1.532 + public synchronized int indexOf(String str, int fromIndex) {
1.533 + return super.indexOf(str, fromIndex);
1.534 + }
1.535 +
1.536 + /**
1.537 + * @throws NullPointerException {@inheritDoc}
1.538 + * @since 1.4
1.539 + */
1.540 + public int lastIndexOf(String str) {
1.541 + // Note, synchronization achieved via other invocations
1.542 + return lastIndexOf(str, count);
1.543 + }
1.544 +
1.545 + /**
1.546 + * @throws NullPointerException {@inheritDoc}
1.547 + * @since 1.4
1.548 + */
1.549 + public synchronized int lastIndexOf(String str, int fromIndex) {
1.550 + return String.lastIndexOf(value, 0, count,
1.551 + str.toCharArray(), 0, str.length(), fromIndex);
1.552 + }
1.553 +
1.554 + /**
1.555 + * @since JDK1.0.2
1.556 + */
1.557 + public synchronized StringBuffer reverse() {
1.558 + super.reverse();
1.559 + return this;
1.560 + }
1.561 +
1.562 + public synchronized String toString() {
1.563 + return new String(value, 0, count);
1.564 + }
1.565 +
1.566 +// /**
1.567 +// * Serializable fields for StringBuffer.
1.568 +// *
1.569 +// * @serialField value char[]
1.570 +// * The backing character array of this StringBuffer.
1.571 +// * @serialField count int
1.572 +// * The number of characters in this StringBuffer.
1.573 +// * @serialField shared boolean
1.574 +// * A flag indicating whether the backing array is shared.
1.575 +// * The value is ignored upon deserialization.
1.576 +// */
1.577 +// private static final java.io.ObjectStreamField[] serialPersistentFields =
1.578 +// {
1.579 +// new java.io.ObjectStreamField("value", char[].class),
1.580 +// new java.io.ObjectStreamField("count", Integer.TYPE),
1.581 +// new java.io.ObjectStreamField("shared", Boolean.TYPE),
1.582 +// };
1.583 +//
1.584 +// /**
1.585 +// * readObject is called to restore the state of the StringBuffer from
1.586 +// * a stream.
1.587 +// */
1.588 +// private synchronized void writeObject(java.io.ObjectOutputStream s)
1.589 +// throws java.io.IOException {
1.590 +// java.io.ObjectOutputStream.PutField fields = s.putFields();
1.591 +// fields.put("value", value);
1.592 +// fields.put("count", count);
1.593 +// fields.put("shared", false);
1.594 +// s.writeFields();
1.595 +// }
1.596 +//
1.597 +// /**
1.598 +// * readObject is called to restore the state of the StringBuffer from
1.599 +// * a stream.
1.600 +// */
1.601 +// private void readObject(java.io.ObjectInputStream s)
1.602 +// throws java.io.IOException, ClassNotFoundException {
1.603 +// java.io.ObjectInputStream.GetField fields = s.readFields();
1.604 +// value = (char[])fields.get("value", null);
1.605 +// count = fields.get("count", 0);
1.606 +// }
1.607 +}