1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.activemq.transport.nio; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.nio.BufferUnderflowException; 22 import java.nio.ByteBuffer; 23 24 /** 25 * An optimized buffered input stream for Tcp 26 * 27 * @version $Revision: 1.1.1.1 $ 28 */ 29 public class NIOInputStream extends InputStream { 30 31 protected int count; 32 protected int position; 33 private final ByteBuffer in; 34 35 public NIOInputStream(ByteBuffer in) { 36 this.in = in; 37 } 38 39 public int read() throws IOException { 40 try { 41 int rc = in.get() & 0xff; 42 return rc; 43 } catch (BufferUnderflowException e) { 44 return -1; 45 } 46 } 47 48 public int read(byte b[], int off, int len) throws IOException { 49 if (in.hasRemaining()) { 50 int rc = Math.min(len, in.remaining()); 51 in.get(b, off, rc); 52 return rc; 53 } else { 54 return len == 0 ? 0 : -1; 55 } 56 } 57 58 public long skip(long n) throws IOException { 59 int rc = Math.min((int)n, in.remaining()); 60 in.position(in.position() + rc); 61 return rc; 62 } 63 64 public int available() throws IOException { 65 return in.remaining(); 66 } 67 68 public boolean markSupported() { 69 return false; 70 } 71 72 public void close() throws IOException { 73 } 74 }