Use nio Channels in OpenAPI read
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / impl / SecurityStream.java
index 0cea95ff8442d1ea3fa3d6da8753415b963816fe..f3d16505fa145f2ee845ff35e270cc50baf50267 100644 (file)
@@ -13,6 +13,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
 import java.nio.charset.StandardCharsets;
 import org.opendaylight.restconf.openapi.jaxrs.OpenApiBodyWriter;
 import org.opendaylight.restconf.openapi.model.OpenApiEntity;
@@ -23,6 +26,7 @@ public final class SecurityStream extends InputStream {
     private final SecurityEntity entity;
 
     private Reader reader;
+    private ReadableByteChannel channel;
 
     public SecurityStream(final OpenApiBodyWriter writer, final SecurityEntity entity) {
         this.writer = writer;
@@ -40,7 +44,10 @@ public final class SecurityStream extends InputStream {
 
     @Override
     public int read(final byte[] array, final int off, final int len) throws IOException {
-        return super.read(array, off, len);
+        if (channel == null) {
+            channel = Channels.newChannel(new ByteArrayInputStream(writeNextEntity(entity)));
+        }
+        return channel.read(ByteBuffer.wrap(array, off, len));
     }
 
     private byte[] writeNextEntity(final OpenApiEntity next) throws IOException {