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