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.OutputStream;
27 public class StringBuilderOutputStream extends OutputStream {
28 private StringBuilder buf;
32 * Create a new string writer using the default initial string-buffer
35 public StringBuilderOutputStream() {
36 buf = new StringBuilder();
40 * Create a new string writer using a passed in StringBuilder
43 public StringBuilderOutputStream(StringBuilder sb) {
48 * Create a new string writer using the specified initial string-buffer
52 * The number of <tt>byte</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 StringBuilderOutputStream(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 bbuf Array of characters
76 * @param off Offset from which to start writing characters
77 * @param len Number of characters to write
80 public void write(byte bbuf[], int off, int len) {
81 if ((off < 0) || (off > bbuf.length) || (len < 0) ||
82 ((off + len) > bbuf.length) || ((off + len) < 0)) {
83 throw new IndexOutOfBoundsException();
84 } else if (len == 0) {
87 buf.append(new String(bbuf, off, len));
91 public void write(byte[] b) throws IOException {
92 buf.append(new String(b));
98 public void write(String str) {
103 * Write a portion of a string.
105 * @param str String to be written
106 * @param off Offset from which to start writing characters
107 * @param len Number of characters to write
109 public void write(String str, int off, int len) {
110 buf.append(str,off,len);
113 public StringBuilderOutputStream append(CharSequence csq) {
117 for(int i = 0;i<csq.length();++i) {
118 buf.append(csq.charAt(i));
124 public StringBuilderOutputStream append(CharSequence csq, int start, int end) {
125 CharSequence cs = (csq == null ? "null" : csq);
126 return append(cs.subSequence(start, end));
130 * Appends the specified character to this writer.
132 * <p> An invocation of this method of the form <tt>out.append(c)</tt>
133 * behaves in exactly the same way as the invocation
136 * out.write(c) </pre>
139 * The 16-bit character to append
141 * @return This writer
145 public StringBuilderOutputStream append(byte c) {
151 * Return the buffer's current value as a string.
153 public String toString() {
154 return buf.toString();
158 * Return the string buffer itself.
160 * @return StringBuffer holding the current buffer value.
162 public StringBuilder getBuffer() {
166 public void reset() {
171 public void flush() throws IOException {
175 public void close() throws IOException {