KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > tcp > TcpBufferedInputStream


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.tcp;
19
20 import java.io.FilterInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 /**
24  * An optimized buffered input stream for Tcp
25  *
26  * @version $Revision: 1.1.1.1 $
27  */

28 public class TcpBufferedInputStream extends FilterInputStream JavaDoc{
29     private static final int DEFAULT_BUFFER_SIZE=8192;
30     protected byte internalBuffer[];
31     protected int count;
32     protected int position;
33
34     public TcpBufferedInputStream(InputStream JavaDoc in){
35         this(in,DEFAULT_BUFFER_SIZE);
36     }
37
38     public TcpBufferedInputStream(InputStream JavaDoc in,int size){
39         super(in);
40         if(size<=0){
41             throw new IllegalArgumentException JavaDoc("Buffer size <= 0");
42         }
43         internalBuffer=new byte[size];
44     }
45
46     private void fill() throws IOException JavaDoc{
47         byte[] buffer=internalBuffer;
48         count=position=0;
49         int n=in.read(buffer,position,buffer.length-position);
50         if(n>0)
51             count=n+position;
52     }
53
54     public int read() throws IOException JavaDoc{
55         if(position>=count){
56             fill();
57             if(position>=count)
58                 return -1;
59         }
60         return internalBuffer[position++]&0xff;
61     }
62
63     private int readStream(byte[] b,int off,int len) throws IOException JavaDoc{
64         int avail=count-position;
65         if(avail<=0){
66             if(len>=internalBuffer.length){
67                 return in.read(b,off,len);
68             }
69             fill();
70             avail=count-position;
71             if(avail<=0)
72                 return -1;
73         }
74         int cnt=(avail<len)?avail:len;
75         System.arraycopy(internalBuffer,position,b,off,cnt);
76         position+=cnt;
77         return cnt;
78     }
79
80     public int read(byte b[],int off,int len) throws IOException JavaDoc{
81         if((off|len|(off+len)|(b.length-(off+len)))<0){
82             throw new IndexOutOfBoundsException JavaDoc();
83         }else if(len==0){
84             return 0;
85         }
86         int n=0;
87         for(;;){
88             int nread=readStream(b,off+n,len-n);
89             if(nread<=0)
90                 return (n==0)?nread:n;
91             n+=nread;
92             if(n>=len)
93                 return n;
94             // if not closed but no bytes available, return
95
InputStream JavaDoc input=in;
96             if(input!=null&&input.available()<=0)
97                 return n;
98         }
99     }
100
101     public long skip(long n) throws IOException JavaDoc{
102         if(n<=0){
103             return 0;
104         }
105         long avail=count-position;
106         if(avail<=0){
107             return in.skip(n);
108         }
109         long skipped=(avail<n)?avail:n;
110         position+=skipped;
111         return skipped;
112     }
113
114     public int available() throws IOException JavaDoc{
115         return in.available()+(count-position);
116     }
117
118     public boolean markSupported(){
119         return false;
120     }
121
122     public void close() throws IOException JavaDoc{
123         if(in!=null)
124             in.close();
125     }
126 }
127
Popular Tags