KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > nio > NIOInputStream


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.nio;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.nio.BufferUnderflowException JavaDoc;
23 import java.nio.ByteBuffer JavaDoc;
24 /**
25  * An optimized buffered input stream for Tcp
26  *
27  * @version $Revision: 1.1.1.1 $
28  */

29 public class NIOInputStream extends InputStream JavaDoc {
30     
31     protected int count;
32     protected int position;
33     private final ByteBuffer JavaDoc in;
34
35     public NIOInputStream(ByteBuffer JavaDoc in){
36         this.in = in;
37     }
38
39     public int read() throws IOException JavaDoc {
40         try {
41             int rc = in.get()& 0xff;
42             return rc;
43         } catch ( BufferUnderflowException JavaDoc e ) {
44             return -1;
45         }
46     }
47
48     public int read(byte b[],int off,int len) throws IOException JavaDoc{
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 JavaDoc{
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 JavaDoc{
65         return in.remaining();
66     }
67
68     public boolean markSupported(){
69         return false;
70     }
71
72     public void close() throws IOException JavaDoc{
73     }
74 }
75
Popular Tags