KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > cs > messages > MsgD


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.cs.messages;
22
23 import java.io.IOException JavaDoc;
24
25 import com.db4o.*;
26 import com.db4o.foundation.network.YapSocket;
27
28 /**
29  * Messages with Data for Client/Server Communication
30  */

31 public class MsgD extends Msg{
32
33     YapWriter _payLoad;
34
35     MsgD() {
36         super();
37     }
38
39     MsgD(String JavaDoc aName) {
40         super(aName);
41     }
42
43     void fakePayLoad(Transaction a_trans) {
44         if (Debug.fakeServer) {
45             _payLoad.removeFirstBytes(YapConst.INT_LENGTH * 2);
46             _payLoad._offset = 0;
47             _payLoad.setTransaction(a_trans);
48         }
49     }
50
51     public YapReader getByteLoad() {
52         return _payLoad;
53     }
54
55     public final YapWriter payLoad() {
56         return _payLoad;
57     }
58     
59     public final MsgD getWriterForLength(Transaction a_trans, int length) {
60         MsgD message = (MsgD)clone(a_trans);
61         message._payLoad = new YapWriter(a_trans, length + YapConst.MESSAGE_LENGTH);
62         message.writeInt(_msgID);
63         message.writeInt(length);
64         if(a_trans.parentTransaction() == null){
65             message._payLoad.append(YapConst.SYSTEM_TRANS);
66         }else{
67             message._payLoad.append(YapConst.USER_TRANS);
68         }
69         return message;
70     }
71     
72     public final MsgD getWriter(Transaction a_trans){
73         return getWriterForLength(a_trans, 0);
74     }
75     
76     public final MsgD getWriterForInts(Transaction a_trans, int[] ints) {
77         MsgD message = getWriterForLength(a_trans, YapConst.INT_LENGTH * ints.length);
78         for (int i = 0; i < ints.length; i++) {
79             message.writeInt(ints[i]);
80         }
81         return message;
82     }
83     
84     public final MsgD getWriterForIntArray(Transaction a_trans, int[] ints, int length){
85         MsgD message = getWriterForLength(a_trans, YapConst.INT_LENGTH * (length + 1));
86         message.writeInt(length);
87         for (int i = 0; i < length; i++) {
88             message.writeInt(ints[i]);
89         }
90         return message;
91     }
92
93     public final MsgD getWriterForInt(Transaction a_trans, int id) {
94         MsgD message = getWriterForLength(a_trans, YapConst.INT_LENGTH);
95         message.writeInt(id);
96         return message;
97     }
98     
99     public final MsgD getWriterForIntString(Transaction a_trans,int anInt, String JavaDoc str) {
100         MsgD message = getWriterForLength(a_trans, YapConst.stringIO.length(str) + YapConst.INT_LENGTH * 2);
101         message.writeInt(anInt);
102         message.writeString(str);
103         return message;
104     }
105     
106     public final MsgD getWriterForLong(Transaction a_trans, long a_long){
107         MsgD message = getWriterForLength(a_trans, YapConst.LONG_LENGTH);
108         message.writeLong(a_long);
109         return message;
110     }
111     
112
113     public final MsgD getWriterForString(Transaction a_trans, String JavaDoc str) {
114         MsgD message = getWriterForLength(a_trans, YapConst.stringIO.length(str) + YapConst.INT_LENGTH);
115         message.writeString(str);
116         return message;
117     }
118
119     public MsgD getWriter(YapWriter bytes) {
120         MsgD message = getWriterForLength(bytes.getTransaction(), bytes.getLength());
121         message._payLoad.append(bytes._buffer);
122         return message;
123     }
124     
125     public byte[] readBytes(){
126         return _payLoad.readBytes(readInt());
127     }
128
129     public final int readInt() {
130         return _payLoad.readInt();
131     }
132     
133     public final long readLong(){
134         return _payLoad.readLong();
135     }
136     
137     public final boolean readBoolean() {
138         return _payLoad.readByte() != 0;
139     }
140
141     final Msg readPayLoad(Transaction a_trans, YapSocket sock, YapReader reader)
142         throws IOException JavaDoc {
143         int length = reader.readInt();
144         
145         a_trans = checkParentTransaction(a_trans, reader);
146         
147         final MsgD command = (MsgD)clone(a_trans);
148         command._payLoad = new YapWriter(a_trans, length);
149         command._payLoad.read(sock);
150         return command;
151     }
152
153     public final String JavaDoc readString() {
154         int length = readInt();
155         return YapConst.stringIO.read(_payLoad, length);
156     }
157     
158     public final void writeBytes(byte[] aBytes){
159         writeInt(aBytes.length);
160         _payLoad.append(aBytes);
161     }
162
163     final void writeInt(int aInt) {
164         _payLoad.writeInt(aInt);
165     }
166     
167     public final void writeLong(long l){
168         _payLoad.writeLong(l);
169     }
170
171     public final void writeString(String JavaDoc aStr) {
172         _payLoad.writeInt(aStr.length());
173         YapConst.stringIO.write(_payLoad, aStr);
174     }
175
176 }
177
Popular Tags