Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/CharArrayWriter.java
38829 views
/*1* Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627import java.util.Arrays;2829/**30* This class implements a character buffer that can be used as an Writer.31* The buffer automatically grows when data is written to the stream. The data32* can be retrieved using toCharArray() and toString().33* <P>34* Note: Invoking close() on this class has no effect, and methods35* of this class can be called after the stream has closed36* without generating an IOException.37*38* @author Herb Jellinek39* @since JDK1.140*/41public42class CharArrayWriter extends Writer {43/**44* The buffer where data is stored.45*/46protected char buf[];4748/**49* The number of chars in the buffer.50*/51protected int count;5253/**54* Creates a new CharArrayWriter.55*/56public CharArrayWriter() {57this(32);58}5960/**61* Creates a new CharArrayWriter with the specified initial size.62*63* @param initialSize an int specifying the initial buffer size.64* @exception IllegalArgumentException if initialSize is negative65*/66public CharArrayWriter(int initialSize) {67if (initialSize < 0) {68throw new IllegalArgumentException("Negative initial size: "69+ initialSize);70}71buf = new char[initialSize];72}7374/**75* Writes a character to the buffer.76*/77public void write(int c) {78synchronized (lock) {79int newcount = count + 1;80if (newcount > buf.length) {81buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));82}83buf[count] = (char)c;84count = newcount;85}86}8788/**89* Writes characters to the buffer.90* @param c the data to be written91* @param off the start offset in the data92* @param len the number of chars that are written93*/94public void write(char c[], int off, int len) {95if ((off < 0) || (off > c.length) || (len < 0) ||96((off + len) > c.length) || ((off + len) < 0)) {97throw new IndexOutOfBoundsException();98} else if (len == 0) {99return;100}101synchronized (lock) {102int newcount = count + len;103if (newcount > buf.length) {104buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));105}106System.arraycopy(c, off, buf, count, len);107count = newcount;108}109}110111/**112* Write a portion of a string to the buffer.113* @param str String to be written from114* @param off Offset from which to start reading characters115* @param len Number of characters to be written116*/117public void write(String str, int off, int len) {118synchronized (lock) {119int newcount = count + len;120if (newcount > buf.length) {121buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));122}123str.getChars(off, off + len, buf, count);124count = newcount;125}126}127128/**129* Writes the contents of the buffer to another character stream.130*131* @param out the output stream to write to132* @throws IOException If an I/O error occurs.133*/134public void writeTo(Writer out) throws IOException {135synchronized (lock) {136out.write(buf, 0, count);137}138}139140/**141* Appends the specified character sequence to this writer.142*143* <p> An invocation of this method of the form <tt>out.append(csq)</tt>144* behaves in exactly the same way as the invocation145*146* <pre>147* out.write(csq.toString()) </pre>148*149* <p> Depending on the specification of <tt>toString</tt> for the150* character sequence <tt>csq</tt>, the entire sequence may not be151* appended. For instance, invoking the <tt>toString</tt> method of a152* character buffer will return a subsequence whose content depends upon153* the buffer's position and limit.154*155* @param csq156* The character sequence to append. If <tt>csq</tt> is157* <tt>null</tt>, then the four characters <tt>"null"</tt> are158* appended to this writer.159*160* @return This writer161*162* @since 1.5163*/164public CharArrayWriter append(CharSequence csq) {165String s = (csq == null ? "null" : csq.toString());166write(s, 0, s.length());167return this;168}169170/**171* Appends a subsequence of the specified character sequence to this writer.172*173* <p> An invocation of this method of the form <tt>out.append(csq, start,174* end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in175* exactly the same way as the invocation176*177* <pre>178* out.write(csq.subSequence(start, end).toString()) </pre>179*180* @param csq181* The character sequence from which a subsequence will be182* appended. If <tt>csq</tt> is <tt>null</tt>, then characters183* will be appended as if <tt>csq</tt> contained the four184* characters <tt>"null"</tt>.185*186* @param start187* The index of the first character in the subsequence188*189* @param end190* The index of the character following the last character in the191* subsequence192*193* @return This writer194*195* @throws IndexOutOfBoundsException196* If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>197* is greater than <tt>end</tt>, or <tt>end</tt> is greater than198* <tt>csq.length()</tt>199*200* @since 1.5201*/202public CharArrayWriter append(CharSequence csq, int start, int end) {203String s = (csq == null ? "null" : csq).subSequence(start, end).toString();204write(s, 0, s.length());205return this;206}207208/**209* Appends the specified character to this writer.210*211* <p> An invocation of this method of the form <tt>out.append(c)</tt>212* behaves in exactly the same way as the invocation213*214* <pre>215* out.write(c) </pre>216*217* @param c218* The 16-bit character to append219*220* @return This writer221*222* @since 1.5223*/224public CharArrayWriter append(char c) {225write(c);226return this;227}228229/**230* Resets the buffer so that you can use it again without231* throwing away the already allocated buffer.232*/233public void reset() {234count = 0;235}236237/**238* Returns a copy of the input data.239*240* @return an array of chars copied from the input data.241*/242public char toCharArray()[] {243synchronized (lock) {244return Arrays.copyOf(buf, count);245}246}247248/**249* Returns the current size of the buffer.250*251* @return an int representing the current size of the buffer.252*/253public int size() {254return count;255}256257/**258* Converts input data to a string.259* @return the string.260*/261public String toString() {262synchronized (lock) {263return new String(buf, 0, count);264}265}266267/**268* Flush the stream.269*/270public void flush() { }271272/**273* Close the stream. This method does not release the buffer, since its274* contents might still be required. Note: Invoking this method in this class275* will have no effect.276*/277public void close() { }278279}280281282