+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.util;\r
-\r
-import java.io.IOException;\r
-import java.io.OutputStream;\r
-\r
-public class JsonOutputStream extends OutputStream {\r
- private static final byte[] TWO_SPACE = " ".getBytes();\r
- private OutputStream os;\r
- private boolean closeable;\r
- private int indent = 0;\r
- private int prev,ret=0;\r
-\r
- public JsonOutputStream(OutputStream os) {\r
- // Don't close these, or dire consequences.\r
- closeable = !os.equals(System.out) && !os.equals(System.err);\r
- this.os = os;\r
- }\r
-\r
- @Override\r
- public void write(int b) throws IOException {\r
- if(ret=='\n') {\r
- ret = 0;\r
- if(prev!=',' || (b!='{' && b!='[')) {\r
- os.write('\n');\r
- for(int i=0;i<indent;++i) {\r
- os.write(TWO_SPACE);\r
- }\r
- }\r
- }\r
- switch(b) {\r
- case '{':\r
- case '[': \r
- ret = '\n';\r
- ++indent;\r
- break;\r
- case '}':\r
- case ']': \r
- --indent;\r
- os.write('\n');\r
- for(int i=0;i<indent;++i) {\r
- os.write(TWO_SPACE);\r
- }\r
- break;\r
- case ',':\r
- ret = '\n';\r
- break;\r
- \r
- }\r
- os.write(b);\r
- prev = b;\r
- }\r
- public void resetIndent() {\r
- indent = 1;\r
- }\r
-\r
- @Override\r
- public void flush() throws IOException {\r
- os.flush();\r
- }\r
-\r
- @Override\r
- public void close() throws IOException {\r
- if(closeable) {\r
- os.close();\r
- }\r
- }\r
-\r
-}\r