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.ByteBuffer;
17 import java.nio.channels.Channels;
18 import java.nio.channels.ReadableByteChannel;
19 import java.nio.charset.StandardCharsets;
20 import java.util.Deque;
21 import org.opendaylight.restconf.openapi.jaxrs.OpenApiBodyWriter;
22 import org.opendaylight.restconf.openapi.model.OpenApiEntity;
23 import org.opendaylight.restconf.openapi.model.PathEntity;
25 public final class PathStream extends InputStream {
26 private final Deque<PathEntity> stack;
27 private final OpenApiBodyWriter writer;
29 private Reader reader;
30 private ReadableByteChannel channel;
32 public PathStream(final Deque<PathEntity> paths, final OpenApiBodyWriter writer) {
38 public int read() throws IOException {
40 if (stack.isEmpty()) {
43 reader = new BufferedReader(
44 new InputStreamReader(new ByteArrayInputStream(writeNextEntity(stack.pop())), StandardCharsets.UTF_8));
47 var read = reader.read();
49 if (stack.isEmpty()) {
52 reader = new BufferedReader(
53 new InputStreamReader(new ByteArrayInputStream(writeNextEntity(stack.pop())), StandardCharsets.UTF_8));
61 public int read(final byte[] array, final int off, final int len) throws IOException {
62 if (channel == null) {
63 if (stack.isEmpty()) {
66 channel = Channels.newChannel(new ByteArrayInputStream(writeNextEntity(stack.pop())));
68 var read = channel.read(ByteBuffer.wrap(array, off, len));
70 if (stack.isEmpty()) {
73 channel = Channels.newChannel(new ByteArrayInputStream(writeNextEntity(stack.pop())));
74 read = channel.read(ByteBuffer.wrap(array, off, len));
79 private byte[] writeNextEntity(final OpenApiEntity entity) throws IOException {
80 writer.writeTo(entity, null, null, null, null, null, null);
81 return writer.readFrom();