KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > io > ObjectReader


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

16 package org.apache.catalina.cluster.io;
17
18 import java.nio.ByteBuffer JavaDoc;
19 import java.nio.channels.Selector JavaDoc;
20 import java.nio.channels.SocketChannel JavaDoc;
21
22 /**
23  * The object reader object is an object used in conjunction with
24  * java.nio TCP messages. This object stores the message bytes in a
25  * <code>XByteBuffer</code> until a full package has been received.
26  * When a full package has been received, the append method will call messageDataReceived
27  * on the callback object associated with this object reader.<BR>
28  * This object uses an XByteBuffer which is an extendable object buffer that also allows
29  * for message encoding and decoding.
30  *
31  * @author Filip Hanik
32  * @author Peter Rossbach
33  * @version $Revision: 1.7 $, $Date: 2005/03/25 22:21:26 $
34  */

35 public class ObjectReader {
36
37     private SocketChannel JavaDoc channel;
38
39     private Selector JavaDoc selector;
40
41     private ListenCallback callback;
42
43     private XByteBuffer buffer;
44
45     /**
46      * Create XByteBuffer and store parameter
47      * @param channel
48      * @param selector
49      * @param callback
50      */

51     public ObjectReader(SocketChannel JavaDoc channel, Selector JavaDoc selector, ListenCallback callback, boolean isCompressed) {
52         this.channel = channel;
53         this.selector = selector;
54         this.callback = callback;
55         this.buffer = new XByteBuffer(isCompressed);
56     }
57
58     /**
59      * get the current SimpleTcpCluster
60      * @return Returns the callback.
61      */

62     public ListenCallback getCallback() {
63         return callback;
64     }
65
66     /**
67      * Get underlying NIO channel
68      * @return
69      */

70     public SocketChannel JavaDoc getChannel() {
71         return this.channel;
72     }
73
74     /**
75      * Append new bytes to buffer.
76      * @see XByteBuffer#countPackages()
77      * @param data new transfer buffer
78      * @param off offset
79      * @param len length in buffer
80      * @return number of messages that sended to callback
81      * @throws java.io.IOException
82      */

83      public int append(byte[] data,int off,int len) throws java.io.IOException JavaDoc {
84         buffer.append(data,off,len);
85         int pkgCnt = buffer.countPackages();
86         return pkgCnt;
87     }
88
89     /**
90      * Send buffer to cluster listener (callback)
91      * Is message complete receiver send message to callback
92      * @see org.apache.catalina.cluster.tcp.SimpleTcpCluster#messageDataReceived(byte[])
93      * @see XByteBuffer#doesPackageExist()
94      * @see XByteBuffer#extractPackage()
95      * @return number of received packages/messages
96      * @throws java.io.IOException
97      */

98     public int execute() throws java.io.IOException JavaDoc {
99         int pkgCnt = 0;
100         boolean pkgExists = buffer.doesPackageExist();
101         while ( pkgExists ) {
102             byte[] b = buffer.extractPackage(true);
103             getCallback().messageDataReceived(b);
104             pkgCnt++;
105             pkgExists = buffer.doesPackageExist();
106         }
107         return pkgCnt;
108     }
109     
110     /**
111      * Write Ack to sender
112      * @param buf
113      * @return
114      * @throws java.io.IOException
115      */

116     public int write(ByteBuffer JavaDoc buf) throws java.io.IOException JavaDoc {
117         return getChannel().write(buf);
118     }
119
120 }
121
Popular Tags