📄 stringbuffer.java
字号:
/************************************************************************This file is part of java core libraries for the simpleRTJ virtual machine.This file is covered by the GNU GPL with the following exception: As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGESOR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OROTHER DEALINGS IN THE SOFTWARE.Created/modified by: RTJ Computing***********************************************************************/package java.lang;/** * Implements string manipulation methods. * * For detailed description of this class see the Java language documentation. */final public class StringBuffer{ private char[] buffer; private int used; final private int SPARECAPACITY = 16; public StringBuffer() { buffer = new char[SPARECAPACITY]; } public StringBuffer(int length) { if (length < 0) { throw new NegativeArraySizeException(); } buffer = new char[length]; } public StringBuffer(String str) { if (str == null) { str = String.valueOf(str); } used = str.length(); buffer = new char[used+SPARECAPACITY]; System.arraycopy(str.toCharArray(), 0, buffer, 0, used); } public synchronized StringBuffer append (String str) { if (str == null) { str = String.valueOf(str); } return append(str.value, str.offset, str.count); } public synchronized StringBuffer append(Object obj) { return append(String.valueOf(obj)); } public StringBuffer append(boolean b) { return append(String.valueOf(b)); } public StringBuffer append(float f) { return append(String.valueOf(f)); } public StringBuffer append(int i) { return append(String.valueOf(i)); } public synchronized StringBuffer append(char c) { if (used + 1 > buffer.length) { ensureCapacity(used+1); } buffer[used] = c; used += 1; return this; } public synchronized StringBuffer append(char str[]) { return append(str, 0, str.length); } public synchronized StringBuffer append(char str[], int offset, int len) { if (used + len > buffer.length) { ensureCapacity(used+len); } System.arraycopy(str, offset, buffer, used, len); used += len; return this; } public synchronized StringBuffer append (byte str[], int offset, int len) { if (used + len > buffer.length) ensureCapacity(used+len); for (int i=0; i < len; i++) buffer[used+i] = (char)str[offset+i]; used += len; return this; } public int capacity() { return buffer.length; } public synchronized char charAt(int index) { checkIndex(index); return buffer[index]; } private synchronized void checkIndex(int index) throws StringIndexOutOfBoundsException { if (index < 0 || index >= used) { throw new StringIndexOutOfBoundsException(/*"index = " + index + ", used = " + used*/); } } public synchronized void ensureCapacity (int minimumCapacity) { int n; char[] oldBuffer; if ((minimumCapacity < 0) || (buffer.length > minimumCapacity)) { return; } n = buffer.length*2; if (minimumCapacity > n) { n = minimumCapacity; } oldBuffer = buffer; buffer = new char[n]; System.arraycopy(oldBuffer, 0, buffer, 0, used); } public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { checkIndex(srcBegin); checkIndex(srcEnd-1); System.arraycopy(buffer, srcBegin, dst, dstBegin, srcEnd-srcBegin); } public synchronized StringBuffer insert(int offset, Object obj) { return insert(offset, String.valueOf(obj)); } public synchronized StringBuffer insert(int offset, String str) { if (str == null) { str = String.valueOf(str); } return insert(offset, str.toCharArray()); } public StringBuffer insert(int offset, boolean b) { return insert(offset, String.valueOf(b)); } public synchronized StringBuffer insert(int offset, char c) { return insert(offset, String.valueOf(c)); } public StringBuffer insert(int offset, float f) { return insert(offset, String.valueOf(f)); } public StringBuffer insert(int offset, int i) { return insert(offset, String.valueOf(i)); } public synchronized StringBuffer insert(int offset, char str[]) { if ((offset < 0) || (offset > used)) { throw new StringIndexOutOfBoundsException(); } ensureCapacity(used + str.length); // Copy buffer up to make space. System.arraycopy(buffer, offset, buffer, offset+str.length, used-offset); // Now, copy insert into place System.arraycopy(str, 0, buffer, offset, str.length); // Update used count used += str.length; return this; } public int length() { return used; } public synchronized StringBuffer reverse() { for (int pos = used/2 - 1; pos >= 0; pos--) { char ch = buffer[pos]; buffer[pos] = buffer[used-pos-1]; buffer[used-pos-1] = ch; } return this; } public synchronized void setCharAt(int index, char ch) { checkIndex(index); buffer[index]=ch; } public synchronized void setLength(int newLength) { char oldBuffer[] = buffer; buffer = new char[newLength]; System.arraycopy(oldBuffer, 0, buffer, 0, newLength); // Pad buffer if (newLength > used) { for (int pos = used; pos < newLength; pos++) { buffer[pos]='\u0000'; } } used = newLength; } public String toString() { return new String(this); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -