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