103 |
char[] value; |
char[] value; |
104 |
|
|
105 |
/** |
/** |
|
* True if the buffer is shared with another object (StringBuilder or |
|
|
* String); this means the buffer must be copied before writing to it again. |
|
|
* Note that this has permissions set this way so that String can get the |
|
|
* value. |
|
|
* |
|
|
* @serial whether the buffer is shared |
|
|
*/ |
|
|
boolean shared; |
|
|
|
|
|
/** |
|
106 |
* The default capacity of a buffer. |
* The default capacity of a buffer. |
107 |
*/ |
*/ |
108 |
private final static int DEFAULT_CAPACITY = 16; |
private final static int DEFAULT_CAPACITY = 16; |
201 |
*/ |
*/ |
202 |
public void ensureCapacity(int minimumCapacity) |
public void ensureCapacity(int minimumCapacity) |
203 |
{ |
{ |
204 |
if (shared || minimumCapacity > value.length) |
if (minimumCapacity > value.length) |
205 |
{ |
{ |
206 |
// We don't want to make a larger vector when `shared' is |
int max = value.length * 2 + 2; |
|
// set. If we do, then setLength becomes very inefficient |
|
|
// when repeatedly reusing a StringBuilder in a loop. |
|
|
int max = (minimumCapacity > value.length |
|
|
? value.length * 2 + 2 |
|
|
: value.length); |
|
207 |
minimumCapacity = (minimumCapacity < max ? max : minimumCapacity); |
minimumCapacity = (minimumCapacity < max ? max : minimumCapacity); |
208 |
char[] nb = new char[minimumCapacity]; |
char[] nb = new char[minimumCapacity]; |
209 |
System.arraycopy(value, 0, nb, 0, count); |
System.arraycopy(value, 0, nb, 0, count); |
210 |
value = nb; |
value = nb; |
|
shared = false; |
|
211 |
} |
} |
212 |
} |
} |
213 |
|
|
645 |
throw new StringIndexOutOfBoundsException(); |
throw new StringIndexOutOfBoundsException(); |
646 |
if (len == 0) |
if (len == 0) |
647 |
return ""; |
return ""; |
648 |
// Don't copy unless substring is smaller than 1/4 of the buffer. |
return new String(value, beginIndex, len); |
|
boolean share_buffer = ((len << 2) >= value.length); |
|
|
if (share_buffer) |
|
|
this.shared = true; |
|
|
// Package constructor avoids an array copy. |
|
|
return new String(value, beginIndex, len, share_buffer); |
|
649 |
} |
} |
650 |
|
|
651 |
/** |
/** |
930 |
*/ |
*/ |
931 |
public String toString() |
public String toString() |
932 |
{ |
{ |
|
// The string will set this.shared = true. |
|
933 |
return new String(this); |
return new String(this); |
934 |
} |
} |
935 |
|
|