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