KickJava   Java API By Example, From Geeks To Geeks.

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


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

36 public class Jdk13ObjectReader
37 {
38     private Socket JavaDoc socket;
39     private ListenCallback callback;
40     private XByteBuffer buffer;
41
42     /**
43      * use this socket and callback to receive messages
44      * @param socket listener socket
45      * @param callback SimpleTcpCluster listener
46      * @param compress is send message data compress or flat.
47      */

48     public Jdk13ObjectReader( Socket JavaDoc socket,
49                                ListenCallback callback, boolean compress) {
50         this.socket = socket;
51         this.callback = callback;
52         this.buffer = new XByteBuffer(compress);
53     }
54
55     
56     /**
57      * Append new bytes to buffer.
58      * Is message complete receiver send message to callback
59      * @see org.apache.catalina.cluster.tcp.SimpleTcpCluster#messageDataReceived(byte[])
60      * @see XByteBuffer#doesPackageExist()
61      * @see XByteBuffer#extractPackage(boolean)
62      * @param data new transfer buffer
63      * @param off offset
64      * @param len length in buffer
65      * @return number of messages that sended to callback
66      * @throws java.io.IOException
67      */

68     public int append(byte[] data,int off,int len) throws java.io.IOException JavaDoc {
69         boolean result = false;
70         buffer.append(data,off,len);
71         int pkgCnt = 0;
72         boolean pkgExists = buffer.doesPackageExist();
73         while ( pkgExists ) {
74             byte[] b = buffer.extractPackage(true);
75             callback.messageDataReceived(b);
76             pkgCnt++;
77             pkgExists = buffer.doesPackageExist();
78         }
79         return pkgCnt;
80     }
81
82     
83     /**
84      * send message to callback
85      * @see Jdk13ObjectReader#append(byte[], int, int)
86      * @return
87      * @throws java.io.IOException
88      */

89     public int execute() throws java.io.IOException JavaDoc {
90         return append(new byte[0],0,0);
91     }
92
93     /**
94      * write data to socket (ack)
95      * @see org.apache.catalina.cluster.tcp.Jdk13ReplicationListener#sendAck
96      * @param data
97      * @return
98      * @throws java.io.IOException
99      */

100     public int write(byte[] data)
101        throws java.io.IOException JavaDoc {
102        socket.getOutputStream().write(data);
103        return 0;
104
105     }
106 }
107
Popular Tags