Path: blob/master/sourcetools/com.ibm.jpp.preprocessor/com/ibm/jpp/xml/XMLStringBuffer.java
6004 views
/*******************************************************************************1* Copyright (c) 1999, 2017 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.jpp.xml;2223import java.text.NumberFormat;2425public final class XMLStringBuffer {2627private int count;28private boolean shared;29char[] value;3031public XMLStringBuffer() {32this(16);33}3435public XMLStringBuffer(int capacity) {36count = 0;37shared = false;38value = new char[capacity];39}4041public XMLStringBuffer(String string) {42count = string.length();43shared = false;44value = new char[count + 16];45string.getChars(0, count, value, 0);46}4748public XMLStringBuffer append(int value) {49return append((long) value);50}5152public XMLStringBuffer append(long value) {53return append(NumberFormat.getNumberInstance().format(value));54}5556public XMLStringBuffer append(Object value) {57return append(String.valueOf(value));58}5960public XMLStringBuffer append(boolean value) {61return append(String.valueOf(value));62}6364public XMLStringBuffer append(char value) {65return append(String.valueOf(value));66}6768public XMLStringBuffer append(String string) {69if (string == null) {70string = String.valueOf(string);71}72int adding = string.length();73int newSize = count + adding;74if (newSize > value.length) {75ensureCapacityImpl(newSize);76} else if (shared) {77char[] copyValue = new char[value.length];78System.arraycopy(value, 0, copyValue, 0, copyValue.length);79value = copyValue;80shared = false;81}82string.getChars(0, adding, value, count);83count = newSize;84return this;85}8687public int capacity() {88return value.length;89}9091public synchronized char charAt(int index) {92if (index < count) {93return value[index];94}95throw new IndexOutOfBoundsException();96}9798public synchronized void ensureCapacity(int min) {99if (min > value.length) {100ensureCapacityImpl(min);101}102}103104private void ensureCapacityImpl(int min) {105int twice = (value.length << 1) + 2;106char[] newData = new char[Math.max(min, twice)];107System.arraycopy(value, 0, newData, 0, count);108value = newData;109shared = false;110}111112public synchronized void getChars(int start, int end, char[] buffer, int index) {113// NOTE last character not copied!114if (start < count && end <= count) {115System.arraycopy(value, start, buffer, index, end - start);116return;117}118throw new IndexOutOfBoundsException();119}120121public synchronized XMLStringBuffer insert(int index, char[] chars) {122move(chars.length, index);123System.arraycopy(chars, 0, value, index, chars.length);124count += chars.length;125return this;126}127128public synchronized XMLStringBuffer insert(int index, char ch) {129move(1, index);130value[index] = ch;131count++;132return this;133}134135public XMLStringBuffer insert(int index, int value) {136return insert(index, (long) value);137}138139public XMLStringBuffer insert(int index, long value) {140return insert(index, NumberFormat.getNumberInstance().format(value));141}142143public XMLStringBuffer insert(int index, Object value) {144return insert(index, String.valueOf(value));145}146147public synchronized XMLStringBuffer insert(int index, String string) {148if (string == null) {149string = String.valueOf(string);150}151int min = string.length();152move(min, index);153string.getChars(0, min, value, index);154count += min;155return this;156}157158public XMLStringBuffer insert(int index, boolean value) {159return insert(index, String.valueOf(value));160}161162public int length() {163return count;164}165166private void move(int size, int index) {167if (0 <= index && index <= count) {168int newSize;169if (value.length - count >= size) {170if (!shared) {171System.arraycopy(value, index, value, index + size, count - index); // index == count case is no-op172return;173}174newSize = value.length;175} else {176int a = count + size;177int b = (value.length << 1) + 2;178newSize = Math.max(a, b);179}180char[] newData = new char[newSize];181System.arraycopy(value, 0, newData, 0, index);182System.arraycopy(value, index, newData, index + size, count - index); // index == count case is no-op183value = newData;184shared = false;185} else {186throw new IndexOutOfBoundsException();187}188}189190public synchronized void setLength(int length) {191if (length > value.length) {192ensureCapacityImpl(length);193}194if (count > length) {195if (shared) {196char[] newData = new char[value.length];197System.arraycopy(value, 0, newData, 0, length);198value = newData;199shared = false;200} else {201// NOTE: delete & replace do not void characters orphaned at the end202try {203for (int i = length; i < count; i++) {204value[i] = 0;205}206} catch (IndexOutOfBoundsException e) {207throw new IndexOutOfBoundsException();208}209}210}211count = length;212}213214public synchronized boolean endsWith(String suffix) {215int suffixLength = suffix.length();216217if (suffixLength < 0 || count < suffixLength) {218return false;219}220221int localIndex = count - suffixLength;222int stringIndex = 0;223224while (suffixLength-- > 0) {225if (charAt(localIndex++) != suffix.charAt(stringIndex++)) {226return false;227}228}229230return true;231}232233@Override234public String toString() {235shared = true;236return new String(value, 0, count);237}238239}240241242