1 18 package org.apache.activemq.transport.nio; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.nio.BufferUnderflowException ; 23 import java.nio.ByteBuffer ; 24 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 } 75 | Popular Tags |