2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.util.handler.ssh.virtualsocket;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelInboundHandler;
16 import java.io.IOException;
17 import java.io.InputStream;
20 * Class provides {@link InputStream} functionality to users of virtual socket.
22 public class ChannelInputStream extends InputStream implements ChannelInboundHandler {
23 private final Object lock = new Object();
24 private final ByteBuf bb = Unpooled.buffer();
27 public int read(byte b[], int off, int len) throws IOException {
29 throw new NullPointerException();
30 } else if (off < 0 || len < 0 || len > b.length - off) {
31 throw new IndexOutOfBoundsException();
32 } else if (len == 0) {
42 if(this.bb.readableBytes() == 0) return bytesRead;
45 ltr = (ltr <= bb.readableBytes()) ? ltr : bb.readableBytes();
47 bb.readBytes(b, 1, ltr);
54 public int read() throws IOException {
56 while (this.bb.readableBytes() == 0) {
59 } catch (InterruptedException e) {
60 throw new RuntimeException(e);
63 return this.bb.readByte() & 0xFF;
68 public int available() throws IOException {
70 return this.bb.readableBytes();
74 public void channelRegistered(ChannelHandlerContext ctx)
76 ctx.fireChannelRegistered();
79 public void channelUnregistered(ChannelHandlerContext ctx)
81 ctx.fireChannelUnregistered();
84 public void channelActive(ChannelHandlerContext ctx)
86 ctx.fireChannelActive();
89 public void channelInactive(ChannelHandlerContext ctx)
91 ctx.fireChannelInactive();
94 public void channelRead(ChannelHandlerContext ctx, Object o)
97 this.bb.discardReadBytes();
98 this.bb.writeBytes((ByteBuf) o);
103 public void channelReadComplete(ChannelHandlerContext ctx)
105 ctx.fireChannelReadComplete();
108 public void userEventTriggered(ChannelHandlerContext ctx, Object o)
110 ctx.fireUserEventTriggered(o);
113 public void channelWritabilityChanged(ChannelHandlerContext ctx)
115 ctx.fireChannelWritabilityChanged();
118 public void handlerAdded(ChannelHandlerContext ctx)
122 public void handlerRemoved(ChannelHandlerContext ctx)
126 public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable)
128 ctx.fireExceptionCaught(throwable);