KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > nio > util > ReadHelper


1 package org.sapia.ubik.net.nio.util;
2
3 import java.io.EOFException JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.nio.ByteBuffer JavaDoc;
6
7 import org.sapia.ubik.net.nio.ChannelManager;
8 import org.sapia.ubik.net.nio.Cycle;
9
10 /**
11  * An instance of this class reads data from the <code>Channel</code> within
12  * a given <code>Cycle</code> and writes it to the cycle's <code>ChannelManager</code>.
13  *
14  * @see Cycle#getByteBuffer()
15  * @see Cycle#getChannel()
16  * @see Cycle#getChannelManager()
17  *
18  * @author Yanick Duchesne
19  *
20  * <dl>
21  * <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>
22  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
23  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
24  * </dl>
25  */

26 public class ReadHelper {
27   
28   public static final int DEFAULT_READ_ATTEMPTS = 2;
29   public static final int DEFAULT_READ_THRESHOLD = 1;
30   
31   private int _readAttempts = DEFAULT_READ_ATTEMPTS;
32   private int _readThreshold = DEFAULT_READ_THRESHOLD;
33   
34
35   /**
36    * @param attempts the number of times this instance should try
37    * reading if the number of read bytes is below or equal
38    * to a given threshold.
39    */

40   public void setReadAttempts(int attempts){
41     _readAttempts = attempts;
42   }
43   
44   /**
45    * @param threshHold the number of read bytes under or equal to
46    * which this instance should attempt reading again.
47    */

48   public void setReadThreshHold(int threshHold){
49     _readAttempts = threshHold;
50   }
51   
52   /***
53    * @param cycle a <code>Cycle</code>
54    * @throws HandlerException if a problem occurs while reading
55    */

56   public void read(Cycle cycle) throws IOException JavaDoc{
57     ByteBuffer JavaDoc input = cycle.getByteBuffer();
58     boolean finished = false;
59     int read;
60     ChannelManager manager = cycle.getChannelManager();
61     while(!finished){
62       read = manager.read(cycle.getChannel(), input);
63       if(read < 0){
64         throw new EOFException JavaDoc("Other endpoint probably disconnected");
65       }
66       if(read <= _readThreshold){
67         int attempt = 0;
68         int readTotal = read;
69         do{
70           readTotal = readTotal + manager.read(cycle.getChannel(), input);
71           attempt++;
72         }while(attempt < _readAttempts && readTotal <= _readThreshold);
73         if(readTotal <= _readThreshold){
74           return;
75         }
76       }
77       input.flip();
78       finished = cycle.getHandler().read(cycle);
79       while(!finished && input.hasRemaining()){
80         finished = cycle.getHandler().read(cycle);
81       }
82       if(!input.hasRemaining()){
83         input.clear();
84       }
85     }
86   }
87 }
88
Popular Tags