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) {
47 ltr = (ltr <= bb.readableBytes()) ? ltr : bb.readableBytes();
49 bb.readBytes(b, 1, ltr);
56 public int read() throws IOException {
58 while (this.bb.readableBytes() == 0) {
61 } catch (InterruptedException e) {
62 Thread.currentThread().interrupt();
63 throw new RuntimeException(e);
66 return this.bb.readByte() & 0xFF;
71 public int available() throws IOException {
73 return this.bb.readableBytes();
77 public void channelRegistered(ChannelHandlerContext ctx) {
78 ctx.fireChannelRegistered();
81 public void channelUnregistered(ChannelHandlerContext ctx) {
82 ctx.fireChannelUnregistered();
85 public void channelActive(ChannelHandlerContext ctx) {
86 ctx.fireChannelActive();
89 public void channelInactive(ChannelHandlerContext ctx) {
90 ctx.fireChannelInactive();
93 public void channelRead(ChannelHandlerContext ctx, Object o) {
95 this.bb.discardReadBytes();
96 this.bb.writeBytes((ByteBuf) o);
101 public void channelReadComplete(ChannelHandlerContext ctx) {
102 ctx.fireChannelReadComplete();
105 public void userEventTriggered(ChannelHandlerContext ctx, Object o) {
106 ctx.fireUserEventTriggered(o);
109 public void channelWritabilityChanged(ChannelHandlerContext ctx) {
110 ctx.fireChannelWritabilityChanged();
113 public void handlerAdded(ChannelHandlerContext ctx) {
116 public void handlerRemoved(ChannelHandlerContext ctx) {
119 public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
120 ctx.fireExceptionCaught(throwable);