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 Thread.currentThread().interrupt();
61 throw new RuntimeException(e);
64 return this.bb.readByte() & 0xFF;
69 public int available() throws IOException {
71 return this.bb.readableBytes();
75 public void channelRegistered(ChannelHandlerContext ctx)
77 ctx.fireChannelRegistered();
80 public void channelUnregistered(ChannelHandlerContext ctx)
82 ctx.fireChannelUnregistered();
85 public void channelActive(ChannelHandlerContext ctx)
87 ctx.fireChannelActive();
90 public void channelInactive(ChannelHandlerContext ctx)
92 ctx.fireChannelInactive();
95 public void channelRead(ChannelHandlerContext ctx, Object o)
98 this.bb.discardReadBytes();
99 this.bb.writeBytes((ByteBuf) o);
104 public void channelReadComplete(ChannelHandlerContext ctx)
106 ctx.fireChannelReadComplete();
109 public void userEventTriggered(ChannelHandlerContext ctx, Object o)
111 ctx.fireUserEventTriggered(o);
114 public void channelWritabilityChanged(ChannelHandlerContext ctx)
116 ctx.fireChannelWritabilityChanged();
119 public void handlerAdded(ChannelHandlerContext ctx)
123 public void handlerRemoved(ChannelHandlerContext ctx)
127 public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable)
129 ctx.fireExceptionCaught(throwable);