29b51ab5226039536dfc04029adbfe2813de558d
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.input;
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 /**
28  * Supplies input from a stream. Closing of the stream is responsibility of the client.
29  *
30  * @author Mark Hollmann
31  */
32 public class BufferedStreamYangInput extends ByteArrayYangInput {
33
34     public BufferedStreamYangInput(final String name, final InputStream inputStream) throws IOException {
35         this(name, inputStream, MEDIA_TYPE_YANG);
36     }
37
38     public BufferedStreamYangInput(final String name, final InputStream inputStream, final String mediaType)
39             throws IOException {
40         super(getBytes(inputStream), name, mediaType);
41     }
42
43     private static byte[] getBytes(final InputStream inputStream) throws IOException {
44         final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
45
46         int nRead;
47         byte[] data = new byte[100000];
48
49         while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
50             buffer.write(data, 0, nRead);
51         }
52
53         return buffer.toByteArray();
54     }
55 }