KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > util > Util1_4


1 package org.jgroups.util;
2
3 import java.io.IOException JavaDoc;
4 import java.nio.ByteBuffer JavaDoc;
5 import java.nio.channels.WritableByteChannel JavaDoc;
6
7 /**
8  * Util1_4
9  *
10  * @author abollu
11  */

12 public class Util1_4 extends Util
13 {
14
15     /* double writes are not required.*/
16     public static void doubleWriteBuffer(
17         ByteBuffer JavaDoc buf,
18         WritableByteChannel JavaDoc out)
19         throws Exception JavaDoc
20     {
21         if (buf.limit() > 1)
22         {
23             int actualLimit = buf.limit();
24             buf.limit(1);
25             writeFully(buf,out);
26             buf.limit(actualLimit);
27             writeFully(buf,out);
28         }
29         else
30         {
31             buf.limit(0);
32             writeFully(buf,out);
33             buf.limit(1);
34             writeFully(buf,out);
35         }
36     }
37
38     /*
39      * if we were to register for OP_WRITE and send the remaining data on
40      * readyOps for this channel we have to either block the caller thread or
41      * queue the message buffers that may arrive while waiting for OP_WRITE.
42      * Instead of the above approach this method will continuously write to the
43      * channel until the buffer sent fully.
44      */

45     public static void writeFully(ByteBuffer JavaDoc buf, WritableByteChannel JavaDoc out)
46         throws IOException JavaDoc
47     {
48         int written = 0;
49         int toWrite = buf.limit();
50         while (written < toWrite)
51         {
52             written += out.write(buf);
53         }
54     }
55 }
56
Popular Tags