KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > nio > TestReadChannel


1 package org.sapia.ubik.net.nio;
2
3 import java.io.IOException JavaDoc;
4 import java.nio.ByteBuffer JavaDoc;
5
6 /**
7  * @author Yanick Duchesne
8  *
9  * <dl>
10  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
11  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
12  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
13  * </dl>
14  */

15 public class TestReadChannel extends TestChannel{
16   
17   private byte[] _data, _buf;
18   private int _pos = 0;
19   
20   public TestReadChannel(byte[] data, int batchSize){
21     _data = data;
22     _buf = new byte[batchSize];
23   }
24   
25   public int read(ByteBuffer JavaDoc to) throws IOException JavaDoc{
26     if(_pos < _data.length){
27       int remaining = to.remaining();
28       int count = 0;
29       while(_pos < _data.length &&
30             count < remaining &&
31             count < _buf.length){
32         _buf[count++] = _data[_pos++];
33       }
34       to.put(_buf, 0, count);
35       return count;
36     }
37     else{
38       return 0;
39     }
40   }
41
42 }
43
Popular Tags