Use nio Channels in OpenAPI read
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / impl / InfoStream.java
index e41129f9f4aed47bb7d28c8601fae5d6575da577..50eac5752a5e4b2dbc142095f3d40511a5cf288a 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.InfoEntity;
@@ -23,6 +26,7 @@ public final class InfoStream extends InputStream {
     private final OpenApiBodyWriter writer;
 
     private Reader reader;
+    private ReadableByteChannel channel;
 
     public InfoStream(final InfoEntity entity, final OpenApiBodyWriter writer) {
         this.entity = entity;
@@ -40,7 +44,10 @@ public final class InfoStream 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 {