[AAF-21] Initial code import
[aaf/cadi.git] / core / src / main / java / com / att / cadi / util / JsonOutputStream.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.util;\r
25 \r
26 import java.io.IOException;\r
27 import java.io.OutputStream;\r
28 \r
29 public class JsonOutputStream extends OutputStream {\r
30         private static final byte[] TWO_SPACE = "  ".getBytes();\r
31         private OutputStream os;\r
32         private boolean closeable;\r
33         private int indent = 0;\r
34         private int prev,ret=0;\r
35 \r
36         public JsonOutputStream(OutputStream os) {\r
37                 // Don't close these, or dire consequences.\r
38                 closeable = !os.equals(System.out) && !os.equals(System.err);\r
39                 this.os = os;\r
40         }\r
41 \r
42         @Override\r
43         public void write(int b) throws IOException {\r
44                 if(ret=='\n') {\r
45                         ret = 0;\r
46                         if(prev!=',' || (b!='{' && b!='[')) {\r
47                                 os.write('\n');\r
48                                 for(int i=0;i<indent;++i) {\r
49                                         os.write(TWO_SPACE);\r
50                                 }\r
51                         }\r
52                 }\r
53                 switch(b) {\r
54                         case '{':\r
55                         case '[':       \r
56                                         ret = '\n';\r
57                                         ++indent;\r
58                                         break;\r
59                         case '}':\r
60                         case ']': \r
61                                         --indent;\r
62                                         os.write('\n');\r
63                                         for(int i=0;i<indent;++i) {\r
64                                                 os.write(TWO_SPACE);\r
65                                         }\r
66                                         break;\r
67                         case ',':\r
68                                         ret = '\n';\r
69                                         break;\r
70                                         \r
71                 }\r
72                 os.write(b);\r
73                 prev = b;\r
74         }\r
75         public void resetIndent() {\r
76                 indent = 1;\r
77         }\r
78 \r
79         @Override\r
80         public void flush() throws IOException {\r
81                 os.flush();\r
82         }\r
83 \r
84         @Override\r
85         public void close() throws IOException {\r
86                 if(closeable) {\r
87                         os.close();\r
88                 }\r
89         }\r
90 \r
91 }\r