Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / util / StringBuilderWriter.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.Writer;\r
26 \r
27 public class StringBuilderWriter extends Writer {\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 StringBuilderWriter() {\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 StringBuilderWriter(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>char</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 StringBuilderWriter(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((char) c);\r
70     }\r
71 \r
72     /**\r
73      * Write a portion of an array of characters.\r
74      *\r
75      * @param  cbuf  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     public void write(char cbuf[], int off, int len) {\r
80         if ((off < 0) || (off > cbuf.length) || (len < 0) ||\r
81             ((off + len) > cbuf.length) || ((off + len) < 0)) {\r
82             throw new IndexOutOfBoundsException();\r
83         } else if (len == 0) {\r
84             return;\r
85         }\r
86         buf.append(cbuf, off, len);\r
87     }\r
88 \r
89     /**\r
90      * Write a string.\r
91      */\r
92     public void write(String str) {\r
93     buf.append(str);\r
94     }\r
95 \r
96     /**\r
97      * Write a portion of a string.\r
98      *\r
99      * @param  str  String to be written\r
100      * @param  off  Offset from which to start writing characters\r
101      * @param  len  Number of characters to write\r
102      */\r
103     public void write(String str, int off, int len)  {\r
104         char[] chars = new char[len];\r
105         str.getChars(off, off+len, chars, 0);\r
106         buf.append(chars);\r
107     }\r
108 \r
109     public StringBuilderWriter append(CharSequence csq) {\r
110         if (csq == null) {\r
111             write("null");\r
112         } else {\r
113             buf.append(csq);\r
114         }\r
115         return this;\r
116     }\r
117 \r
118     public StringBuilderWriter append(CharSequence csq, int start, int end) {\r
119         CharSequence cs = (csq == null ? "null" : csq);\r
120         return append(cs.subSequence(start, end));\r
121     }\r
122 \r
123     /**\r
124      * Appends the specified character to this writer. \r
125      *\r
126      * <p> An invocation of this method of the form <tt>out.append(c)</tt>\r
127      * behaves in exactly the same way as the invocation\r
128      *\r
129      * <pre>\r
130      *     out.write(c) </pre>\r
131      *\r
132      * @param  c\r
133      *         The 16-bit character to append\r
134      *\r
135      * @return  This writer\r
136      *\r
137      * @since 1.5\r
138      */\r
139     public StringBuilderWriter append(char c) {\r
140         buf.append(c);\r
141         return this;\r
142     }\r
143 \r
144     /**\r
145      * Return the buffer's current value as a string.\r
146      */\r
147     public String toString() {\r
148         return buf.toString();\r
149     }\r
150 \r
151     /**\r
152      * Return the string buffer itself.\r
153      *\r
154      * @return StringBuffer holding the current buffer value.\r
155      */\r
156     public StringBuilder getBuffer() {\r
157     return buf;\r
158     }\r
159     \r
160     public void reset() {\r
161         buf.setLength(0);\r
162     }\r
163 \r
164     @Override\r
165     public void flush() throws IOException {\r
166     }\r
167 \r
168     @Override\r
169     public void close() throws IOException {\r
170     }\r
171 \r
172 }\r