2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.misc.env.util;
24 import java.io.IOException;
25 import java.io.Writer;
27 public class StringBuilderWriter extends Writer {
28 private StringBuilder buf;
32 * Create a new string writer using the default initial string-buffer
35 public StringBuilderWriter() {
36 buf = new StringBuilder();
40 * Create a new string writer using a passed in StringBuilder
43 public StringBuilderWriter(StringBuilder sb) {
48 * Create a new string writer using the specified initial string-buffer
52 * The number of <tt>char</tt> values that will fit into this buffer
53 * before it is automatically expanded
55 * @throws IllegalArgumentException
56 * If <tt>initialSize</tt> is negative
58 public StringBuilderWriter(int initialSize) {
59 if (initialSize < 0) {
60 throw new IllegalArgumentException("Negative buffer size");
62 buf = new StringBuilder(initialSize);
66 * Write a single character.
68 public void write(int c) {
73 * Write a portion of an array of characters.
75 * @param cbuf Array of characters
76 * @param off Offset from which to start writing characters
77 * @param len Number of characters to write
79 public void write(char cbuf[], int off, int len) {
80 if ((off < 0) || (off > cbuf.length) || (len < 0) ||
81 ((off + len) > cbuf.length) || ((off + len) < 0)) {
82 throw new IndexOutOfBoundsException();
83 } else if (len == 0) {
86 buf.append(cbuf, off, len);
92 public void write(String str) {
97 * Write a portion of a string.
99 * @param str String to be written
100 * @param off Offset from which to start writing characters
101 * @param len Number of characters to write
103 public void write(String str, int off, int len) {
104 char[] chars = new char[len];
105 str.getChars(off, off+len, chars, 0);
109 public StringBuilderWriter append(CharSequence csq) {
118 public StringBuilderWriter append(CharSequence csq, int start, int end) {
119 CharSequence cs = (csq == null ? "null" : csq);
120 return append(cs.subSequence(start, end));
124 * Appends the specified character to this writer.
126 * <p> An invocation of this method of the form <tt>out.append(c)</tt>
127 * behaves in exactly the same way as the invocation
130 * out.write(c) </pre>
133 * The 16-bit character to append
135 * @return This writer
139 public StringBuilderWriter append(char c) {
145 * Return the buffer's current value as a string.
147 public String toString() {
148 return buf.toString();
152 * Return the string buffer itself.
154 * @return StringBuffer holding the current buffer value.
156 public StringBuilder getBuffer() {
160 public void reset() {
165 public void flush() throws IOException {
169 public void close() throws IOException {