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