0553d0e6595d09357d23dc8688e03db5b91665f3
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / impl / OpenApiVersionStream.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.openapi.impl;
9
10 import java.io.BufferedReader;
11 import java.io.ByteArrayInputStream;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.io.Reader;
16 import java.nio.charset.StandardCharsets;
17 import org.opendaylight.restconf.openapi.jaxrs.OpenApiBodyWriter;
18 import org.opendaylight.restconf.openapi.model.OpenApiEntity;
19 import org.opendaylight.restconf.openapi.model.OpenApiVersionEntity;
20
21 public final class OpenApiVersionStream extends InputStream {
22     private final OpenApiVersionEntity entity;
23     private final OpenApiBodyWriter writer;
24
25     private Reader reader;
26
27     public OpenApiVersionStream(final OpenApiVersionEntity entity, final OpenApiBodyWriter writer) {
28         this.entity = entity;
29         this.writer = writer;
30     }
31
32     @Override
33     public int read() throws IOException {
34         if (reader == null) {
35             reader = new BufferedReader(
36                 new InputStreamReader(new ByteArrayInputStream(writeNextEntity(entity)), StandardCharsets.UTF_8));
37         }
38         return reader.read();
39     }
40
41     @Override
42     public int read(final byte[] array, final int off, final int len) throws IOException {
43         return super.read(array, off, len);
44     }
45
46     private byte[] writeNextEntity(final OpenApiEntity next) throws IOException {
47         writer.writeTo(next, null, null, null, null, null, null);
48         return writer.readFrom();
49     }
50 }