01: /*
02: * Copyright 1999,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: * The object reader object is an object used in conjunction with
21: * java.nio TCP messages. This object stores the message bytes in a
22: * <code>XByteBuffer</code> until a full package has been received.
23: * When a full package has been received, the append method will call messageDataReceived
24: * on the callback object associated with this object reader.<BR>
25: * This object uses an XByteBuffer which is an extendable object buffer that also allows
26: * for message encoding and decoding.
27: *
28: * @author Filip Hanik
29: * @version $Revision: 1.3 $, $Date: 2004/02/27 14:58:55 $
30: */
31:
32: import java.net.Socket;
33: import java.nio.ByteBuffer;
34: import java.io.IOException;
35: import org.apache.catalina.cluster.io.XByteBuffer;
36:
37: public class Jdk13ObjectReader {
38: private Socket socket;
39: private ListenCallback callback;
40: private XByteBuffer buffer;
41:
42: public Jdk13ObjectReader(Socket socket, ListenCallback callback) {
43: this .socket = socket;
44: this .callback = callback;
45: this .buffer = new XByteBuffer();
46: }
47:
48: public int append(byte[] data, int off, int len)
49: throws java.io.IOException {
50: boolean result = false;
51: buffer.append(data, off, len);
52: int pkgCnt = 0;
53: boolean pkgExists = buffer.doesPackageExist();
54: while (pkgExists) {
55: byte[] b = buffer.extractPackage(true);
56: callback.messageDataReceived(b);
57: pkgCnt++;
58: pkgExists = buffer.doesPackageExist();
59: }//end if
60: return pkgCnt;
61: }
62:
63: public int execute() throws java.io.IOException {
64: return append(new byte[0], 0, 0);
65: }
66:
67: public int write(byte[] data) throws java.io.IOException {
68: socket.getOutputStream().write(data);
69: return 0;
70:
71: }
72:
73: }
|