Sonar Fixes, Formatting
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / util / JsonOutputStream.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.cadi.util;
23
24 import java.io.IOException;
25 import java.io.OutputStream;
26
27 public class JsonOutputStream extends OutputStream {
28     private static final byte[] TWO_SPACE = "  ".getBytes();
29     private OutputStream os;
30     private boolean closeable;
31     private int indent = 0;
32     private int prev,ret=0;
33
34     public JsonOutputStream(OutputStream os) {
35         // Don't close these, or dire consequences.
36         closeable = !os.equals(System.out) && !os.equals(System.err);
37         this.os = os;
38     }
39
40     @Override
41     public void write(int b) throws IOException {
42         if (ret=='\n') {
43             ret = 0;
44             if (prev!=',' || (b!='{' && b!='[')) {
45                 os.write('\n');
46                 for (int i=0;i<indent;++i) {
47                     os.write(TWO_SPACE);
48                 }
49             }
50         }
51         switch(b) {
52             case '{':
53             case '[':
54                     ret = '\n';
55                     ++indent;
56                     break;
57             case '}':
58             case ']':
59                     --indent;
60                     os.write('\n');
61                     for (int i=0;i<indent;++i) {
62                         os.write(TWO_SPACE);
63                     }
64                     break;
65             case ',':
66                     ret = '\n';
67                     break;
68
69         }
70         os.write(b);
71         prev = b;
72     }
73     public void resetIndent() {
74         indent = 1;
75     }
76
77     @Override
78     public void flush() throws IOException {
79         os.flush();
80     }
81
82     @Override
83     public void close() throws IOException {
84         if (closeable) {
85             os.close();
86         }
87     }
88
89 }