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