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 org.opendaylight.restconf.openapi.jaxrs.OpenApiBodyWriter;
21 import org.opendaylight.restconf.openapi.model.OpenApiEntity;
22 import org.opendaylight.restconf.openapi.model.OpenApiVersionEntity;
24 public final class OpenApiVersionStream extends InputStream {
25 private final OpenApiVersionEntity entity;
26 private final OpenApiBodyWriter writer;
28 private Reader reader;
29 private ReadableByteChannel channel;
31 public OpenApiVersionStream(final OpenApiVersionEntity entity, final OpenApiBodyWriter writer) {
37 public int read() throws IOException {
39 reader = new BufferedReader(
40 new InputStreamReader(new ByteArrayInputStream(writeNextEntity(entity)), StandardCharsets.UTF_8));
46 public int read(final byte[] array, final int off, final int len) throws IOException {
47 if (channel == null) {
48 channel = Channels.newChannel(new ByteArrayInputStream(writeNextEntity(entity)));
50 return channel.read(ByteBuffer.wrap(array, off, len));
53 private byte[] writeNextEntity(final OpenApiEntity next) throws IOException {
54 writer.writeTo(next, null, null, null, null, null, null);
55 return writer.readFrom();