AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / util / StringBuilderOutputStream.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.env.util;
23
24 import java.io.IOException;
25 import java.io.OutputStream;
26
27 public class StringBuilderOutputStream extends OutputStream {
28         private StringBuilder buf;
29
30
31     /**
32      * Create a new string writer using the default initial string-buffer
33      * size.
34      */
35     public StringBuilderOutputStream() {
36         buf = new StringBuilder();
37     }
38
39     /**
40      * Create a new string writer using a passed in StringBuilder
41      * size.
42      */
43     public StringBuilderOutputStream(StringBuilder sb) {
44         buf = sb;
45     }
46
47     /**
48      * Create a new string writer using the specified initial string-buffer
49      * size.
50      *
51      * @param initialSize
52      *        The number of <tt>byte</tt> values that will fit into this buffer
53      *        before it is automatically expanded
54      *
55      * @throws IllegalArgumentException
56      *         If <tt>initialSize</tt> is negative
57      */
58     public StringBuilderOutputStream(int initialSize) {
59         if (initialSize < 0) {
60             throw new IllegalArgumentException("Negative buffer size");
61         }
62         buf = new StringBuilder(initialSize);
63     }
64
65     /**
66      * Write a single character.
67      */
68     public void write(int c) {
69         buf.append((byte) c);
70     }
71
72     /**
73      * Write a portion of an array of characters.
74      *
75      * @param  bbuf  Array of characters
76      * @param  off   Offset from which to start writing characters
77      * @param  len   Number of characters to write
78      */
79     
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) {
85             return;
86         }
87         buf.append(new String(bbuf, off, len));
88     }
89
90     @Override
91         public void write(byte[] b) throws IOException {
92                 buf.append(new String(b));
93         }
94
95         /**
96      * Write a string.
97      */
98     public void write(String str) {
99         buf.append(str);
100     }
101
102     /**
103      * Write a portion of a string.
104      *
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
108      */
109     public void write(String str, int off, int len)  {
110         buf.append(str,off,len);
111     }
112
113     public StringBuilderOutputStream append(CharSequence csq) {
114         if (csq == null) {
115                 write("null");
116         } else {
117                 for(int i = 0;i<csq.length();++i) {
118                         buf.append(csq.charAt(i));
119                 }
120         }
121         return this;
122     }
123
124     public StringBuilderOutputStream append(CharSequence csq, int start, int end) {
125                 CharSequence cs = (csq == null ? "null" : csq);
126                 return append(cs.subSequence(start, end));
127     }
128
129     /**
130      * Appends the specified character to this writer. 
131      *
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
134      *
135      * <pre>
136      *     out.write(c) </pre>
137      *
138      * @param  c
139      *         The 16-bit character to append
140      *
141      * @return  This writer
142      *
143      * @since 1.5
144      */
145     public StringBuilderOutputStream append(byte c) {
146         buf.append(c);
147         return this;
148     }
149
150     /**
151      * Return the buffer's current value as a string.
152      */
153     public String toString() {
154         return buf.toString();
155     }
156
157     /**
158      * Return the string buffer itself.
159      *
160      * @return StringBuffer holding the current buffer value.
161      */
162     public StringBuilder getBuffer() {
163         return buf;
164     }
165     
166     public void reset() {
167         buf.setLength(0);
168     }
169
170         @Override
171         public void flush() throws IOException {
172         }
173
174         @Override
175         public void close() throws IOException {
176         }
177
178 }