Iterate over models in PathsStream
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / impl / PathStream.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.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;
24
25 public final class PathStream extends InputStream {
26     private final Deque<PathEntity> stack;
27     private final OpenApiBodyWriter writer;
28
29     private Reader reader;
30     private ReadableByteChannel channel;
31
32     public PathStream(final Deque<PathEntity> paths, final OpenApiBodyWriter writer) {
33         this.stack = paths;
34         this.writer = writer;
35     }
36
37     @Override
38     public int read() throws IOException {
39         if (reader == null) {
40             if (stack.isEmpty()) {
41                 return -1;
42             }
43             reader = new BufferedReader(
44                 new InputStreamReader(new ByteArrayInputStream(writeNextEntity(stack.pop())), StandardCharsets.UTF_8));
45         }
46
47         var read = reader.read();
48         while (read == -1) {
49             if (stack.isEmpty()) {
50                 return -1;
51             }
52             reader = new BufferedReader(
53                 new InputStreamReader(new ByteArrayInputStream(writeNextEntity(stack.pop())), StandardCharsets.UTF_8));
54             read = reader.read();
55         }
56
57         return read;
58     }
59
60     @Override
61     public int read(final byte[] array, final int off, final int len) throws IOException {
62         if (channel == null) {
63             if (stack.isEmpty()) {
64                 return -1;
65             }
66             channel = Channels.newChannel(new ByteArrayInputStream(writeNextEntity(stack.pop())));
67         }
68         var read = channel.read(ByteBuffer.wrap(array, off, len));
69         while (read == -1) {
70             if (stack.isEmpty()) {
71                 return -1;
72             }
73             channel = Channels.newChannel(new ByteArrayInputStream(writeNextEntity(stack.pop())));
74             read = channel.read(ByteBuffer.wrap(array, off, len));
75         }
76         return read;
77     }
78
79     private byte[] writeNextEntity(final OpenApiEntity entity) throws IOException {
80         writer.writeTo(entity, null, null, null, null, null, null);
81         return writer.readFrom();
82     }
83 }