Use nio Channels in OpenAPI read
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / impl / SchemaStream.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.SchemaEntity;
24
25 public final class SchemaStream extends InputStream {
26     private final Deque<SchemaEntity> stack;
27     private final OpenApiBodyWriter writer;
28
29     private Reader reader;
30     private ReadableByteChannel channel;
31
32     public SchemaStream(final Deque<SchemaEntity> schemas, final OpenApiBodyWriter writer) {
33         this.stack = schemas;
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(new InputStreamReader(new ByteArrayInputStream(writeNextEntity(stack.pop())),
44                 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(new InputStreamReader(new ByteArrayInputStream(writeNextEntity(stack.pop())),
53                 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
69         var read = channel.read(ByteBuffer.wrap(array, off, len));
70         while (read == -1) {
71             if (stack.isEmpty()) {
72                 return -1;
73             }
74             channel = Channels.newChannel(new ByteArrayInputStream(writeNextEntity(stack.pop())));
75             read = channel.read(ByteBuffer.wrap(array, off, len));
76         }
77
78         return read;
79     }
80
81     private byte[] writeNextEntity(final OpenApiEntity entity) throws IOException {
82         writer.writeTo(entity, null, null, null, null, null, null);
83         return writer.readFrom();
84     }
85 }