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