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.nettyutil.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;
15 import java.io.IOException;
16 import java.io.InputStream;
19 * Class provides {@link InputStream} functionality to users of virtual socket.
21 public class ChannelInputStream extends InputStream implements ChannelInboundHandler {
22 private final Object lock = new Object();
23 private final ByteBuf bb = Unpooled.buffer();
26 public int read(byte b[], int off, int len) throws IOException {
28 throw new NullPointerException();
29 } else if (off < 0 || len < 0 || len > b.length - off) {
30 throw new IndexOutOfBoundsException();
31 } else if (len == 0) {
41 if(this.bb.readableBytes() == 0) {
46 ltr = (ltr <= bb.readableBytes()) ? ltr : bb.readableBytes();
48 bb.readBytes(b, 1, ltr);
55 public int read() throws IOException {
57 while (this.bb.readableBytes() == 0) {
60 } catch (InterruptedException e) {
61 Thread.currentThread().interrupt();
62 throw new IllegalStateException(e);
65 return this.bb.readByte() & 0xFF;
70 public int available() throws IOException {
72 return this.bb.readableBytes();
76 public void channelRegistered(ChannelHandlerContext ctx) {
77 ctx.fireChannelRegistered();
80 public void channelUnregistered(ChannelHandlerContext ctx) {
81 ctx.fireChannelUnregistered();
84 public void channelActive(ChannelHandlerContext ctx) {
85 ctx.fireChannelActive();
88 public void channelInactive(ChannelHandlerContext ctx) {
89 ctx.fireChannelInactive();
92 public void channelRead(ChannelHandlerContext ctx, Object o) {
94 this.bb.discardReadBytes();
95 this.bb.writeBytes((ByteBuf) o);
96 ((ByteBuf) o).release();
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);