2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.openapi.impl;
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 java.util.Deque;
18 import org.opendaylight.restconf.openapi.jaxrs.OpenApiBodyWriter;
19 import org.opendaylight.restconf.openapi.model.OpenApiEntity;
20 import org.opendaylight.restconf.openapi.model.SchemaEntity;
22 public final class SchemaStream extends InputStream {
23 private final Deque<SchemaEntity> stack;
24 private final OpenApiBodyWriter writer;
26 private Reader reader;
28 public SchemaStream(final Deque<SchemaEntity> schemas, final OpenApiBodyWriter writer) {
34 public int read() throws IOException {
36 if (stack.isEmpty()) {
39 reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(writeNextEntity(stack.pop())),
40 StandardCharsets.UTF_8));
43 var read = reader.read();
45 if (stack.isEmpty()) {
48 reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(writeNextEntity(stack.pop())),
49 StandardCharsets.UTF_8));
57 public int read(final byte[] array, final int off, final int len) throws IOException {
58 return super.read(array, off, len);
61 private byte[] writeNextEntity(final OpenApiEntity entity) throws IOException {
62 writer.writeTo(entity, null, null, null, null, null, null);
63 return writer.readFrom();