Path: blob/master/src/java.base/share/classes/java/io/CharArrayWriter.java
41152 views
/*1* Copyright (c) 1996, 2021, 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 1.140*/41public class CharArrayWriter extends Writer {42/**43* The buffer where data is stored.44*/45protected char buf[];4647/**48* The number of chars in the buffer.49*/50protected int count;5152/**53* Creates a new CharArrayWriter.54*/55public CharArrayWriter() {56this(32);57}5859/**60* Creates a new CharArrayWriter with the specified initial size.61*62* @param initialSize an int specifying the initial buffer size.63* @throws IllegalArgumentException if initialSize is negative64*/65public CharArrayWriter(int initialSize) {66if (initialSize < 0) {67throw new IllegalArgumentException("Negative initial size: "68+ initialSize);69}70buf = new char[initialSize];71}7273/**74* Writes a character to the buffer.75*/76public void write(int c) {77synchronized (lock) {78int newcount = count + 1;79if (newcount > buf.length) {80buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));81}82buf[count] = (char)c;83count = newcount;84}85}8687/**88* Writes characters to the buffer.89* @param c the data to be written90* @param off the start offset in the data91* @param len the number of chars that are written92*93* @throws IndexOutOfBoundsException94* If {@code off} is negative, or {@code len} is negative,95* or {@code off + len} is negative or greater than the length96* of the given array97*/98public void write(char c[], int off, int len) {99if ((off < 0) || (off > c.length) || (len < 0) ||100((off + len) > c.length) || ((off + len) < 0)) {101throw new IndexOutOfBoundsException();102} else if (len == 0) {103return;104}105synchronized (lock) {106int newcount = count + len;107if (newcount > buf.length) {108buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));109}110System.arraycopy(c, off, buf, count, len);111count = newcount;112}113}114115/**116* Write a portion of a string to the buffer.117* @param str String to be written from118* @param off Offset from which to start reading characters119* @param len Number of characters to be written120*121* @throws IndexOutOfBoundsException122* If {@code off} is negative, or {@code len} is negative,123* or {@code off + len} is negative or greater than the length124* of the given string125*/126public void write(String str, int off, int len) {127synchronized (lock) {128int newcount = count + len;129if (newcount > buf.length) {130buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));131}132str.getChars(off, off + len, buf, count);133count = newcount;134}135}136137/**138* Writes the contents of the buffer to another character stream.139*140* @param out the output stream to write to141* @throws IOException If an I/O error occurs.142*/143public void writeTo(Writer out) throws IOException {144synchronized (lock) {145out.write(buf, 0, count);146}147}148149/**150* Appends the specified character sequence to this writer.151*152* <p> An invocation of this method of the form {@code out.append(csq)}153* behaves in exactly the same way as the invocation154*155* <pre>156* out.write(csq.toString()) </pre>157*158* <p> Depending on the specification of {@code toString} for the159* character sequence {@code csq}, the entire sequence may not be160* appended. For instance, invoking the {@code toString} method of a161* character buffer will return a subsequence whose content depends upon162* the buffer's position and limit.163*164* @param csq165* The character sequence to append. If {@code csq} is166* {@code null}, then the four characters {@code "null"} are167* appended to this writer.168*169* @return This writer170*171* @since 1.5172*/173public CharArrayWriter append(CharSequence csq) {174String s = String.valueOf(csq);175write(s, 0, s.length());176return this;177}178179/**180* Appends a subsequence of the specified character sequence to this writer.181*182* <p> An invocation of this method of the form183* {@code out.append(csq, start, end)} when184* {@code csq} is not {@code null}, behaves in185* exactly the same way as the invocation186*187* <pre>188* out.write(csq.subSequence(start, end).toString()) </pre>189*190* @param csq191* The character sequence from which a subsequence will be192* appended. If {@code csq} is {@code null}, then characters193* will be appended as if {@code csq} contained the four194* characters {@code "null"}.195*196* @param start197* The index of the first character in the subsequence198*199* @param end200* The index of the character following the last character in the201* subsequence202*203* @return This writer204*205* @throws IndexOutOfBoundsException206* If {@code start} or {@code end} are negative, {@code start}207* is greater than {@code end}, or {@code end} is greater than208* {@code csq.length()}209*210* @since 1.5211*/212public CharArrayWriter append(CharSequence csq, int start, int end) {213if (csq == null) csq = "null";214return append(csq.subSequence(start, end));215}216217/**218* Appends the specified character to this writer.219*220* <p> An invocation of this method of the form {@code out.append(c)}221* behaves in exactly the same way as the invocation222*223* <pre>224* out.write(c) </pre>225*226* @param c227* The 16-bit character to append228*229* @return This writer230*231* @since 1.5232*/233public CharArrayWriter append(char c) {234write(c);235return this;236}237238/**239* Resets the buffer so that you can use it again without240* throwing away the already allocated buffer.241*/242public void reset() {243count = 0;244}245246/**247* Returns a copy of the input data.248*249* @return an array of chars copied from the input data.250*/251public char[] toCharArray() {252synchronized (lock) {253return Arrays.copyOf(buf, count);254}255}256257/**258* Returns the current size of the buffer.259*260* @return an int representing the current size of the buffer.261*/262public int size() {263return count;264}265266/**267* Converts input data to a string.268* @return the string.269*/270public String toString() {271synchronized (lock) {272return new String(buf, 0, count);273}274}275276/**277* Flush the stream.278*279* <p> The {@code flush} method of {@code CharArrayWriter} does nothing.280*/281public void flush() { }282283/**284* Close the stream. This method does not release the buffer, since its285* contents might still be required. Note: Invoking this method in this class286* will have no effect.287*/288public void close() { }289290}291292293