Merge "Statistics-Manager - Performance Improvement 1) Introduced statistics request...
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / handler / ssh / virtualsocket / ChannelInputStream.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.netconf.util.handler.ssh.virtualsocket;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelInboundHandler;
15 import java.io.IOException;
16 import java.io.InputStream;
17
18 /**
19  * Class provides {@link InputStream} functionality to users of virtual socket.
20  */
21 public class ChannelInputStream extends InputStream implements ChannelInboundHandler {
22     private final Object lock = new Object();
23     private final ByteBuf bb = Unpooled.buffer();
24
25     @Override
26     public int read(byte b[], int off, int len) throws IOException {
27         if (b == null) {
28             throw new NullPointerException();
29         } else if (off < 0 || len < 0 || len > b.length - off) {
30             throw new IndexOutOfBoundsException();
31         } else if (len == 0) {
32             return 0;
33         }
34
35         int bytesRead = 1;
36         synchronized (lock) {
37             int c = read();
38
39             b[off] = (byte)c;
40
41             if(this.bb.readableBytes() == 0) {
42                 return bytesRead;
43             }
44
45             int ltr = len-1;
46             ltr = (ltr <= bb.readableBytes()) ? ltr : bb.readableBytes();
47
48             bb.readBytes(b, 1, ltr);
49             bytesRead += ltr;
50         }
51         return bytesRead;
52     }
53
54     @Override
55     public int read() throws IOException {
56         synchronized (lock) {
57             while (this.bb.readableBytes() == 0) {
58                 try {
59                     lock.wait();
60                 } catch (InterruptedException e) {
61                     Thread.currentThread().interrupt();
62                     throw new IllegalStateException(e);
63                 }
64             }
65             return this.bb.readByte() & 0xFF;
66         }
67     }
68
69     @Override
70     public int available() throws IOException {
71         synchronized (lock) {
72             return this.bb.readableBytes();
73         }
74     }
75
76     public void channelRegistered(ChannelHandlerContext ctx) {
77         ctx.fireChannelRegistered();
78     }
79
80     public void channelUnregistered(ChannelHandlerContext ctx) {
81         ctx.fireChannelUnregistered();
82     }
83
84     public void channelActive(ChannelHandlerContext ctx) {
85         ctx.fireChannelActive();
86     }
87
88     public void channelInactive(ChannelHandlerContext ctx) {
89         ctx.fireChannelInactive();
90     }
91
92     public void channelRead(ChannelHandlerContext ctx, Object o) {
93         synchronized(lock) {
94             this.bb.discardReadBytes();
95             this.bb.writeBytes((ByteBuf) o);
96             lock.notifyAll();
97         }
98     }
99
100     public void channelReadComplete(ChannelHandlerContext ctx) {
101         ctx.fireChannelReadComplete();
102     }
103
104     public void userEventTriggered(ChannelHandlerContext ctx, Object o) {
105         ctx.fireUserEventTriggered(o);
106     }
107
108     public void channelWritabilityChanged(ChannelHandlerContext ctx) {
109         ctx.fireChannelWritabilityChanged();
110     }
111
112     public void handlerAdded(ChannelHandlerContext ctx) {
113     }
114
115     public void handlerRemoved(ChannelHandlerContext ctx) {
116     }
117
118     public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
119         ctx.fireExceptionCaught(throwable);
120     }
121 }
122