KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > TransactBuffer


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb;
18
19 import org.alfresco.filesys.util.DataBuffer;
20
21 /**
22  * Transact Buffer Class
23  * <p>
24  * Contains the parameters and data for a transaction, transaction2 or NT transaction request.
25  */

26 public class TransactBuffer
27 {
28
29     // Default buffer sizes
30

31     protected static final int DefaultSetupSize = 32;
32     protected static final int DefaultDataSize = 8192;
33     protected static final int DefaultParameterSize = 64;
34
35     // Default maximum return buffer sizes
36

37     protected static final int DefaultMaxSetupReturn = 16;
38     protected static final int DefaultMaxParameterReturn = 256;
39     protected static final int DefaultMaxDataReturn = 65000;
40
41     // Tree id, connection that the transaction is for
42

43     protected int m_treeId = -1;
44
45     // Transaction packet type and sub-function
46

47     protected int m_type;
48     protected int m_func;
49
50     // Transaction name, for Transaction2 only
51

52     protected String JavaDoc m_name;
53
54     // Setup parameters
55

56     protected DataBuffer m_setupBuf;
57
58     // Parameter block
59

60     protected DataBuffer m_paramBuf;
61
62     // Data block and read/write position
63

64     protected DataBuffer m_dataBuf;
65
66     // Flag to indicate if this is a multi-packet transaction
67

68     protected boolean m_multi;
69
70     // Unicode strings flag
71

72     protected boolean m_unicode;
73
74     // Maximum setup, parameter and data bytes to return
75

76     protected int m_maxSetup = DefaultMaxSetupReturn;
77     protected int m_maxParam = DefaultMaxParameterReturn;
78     protected int m_maxData = DefaultMaxDataReturn;
79
80     /**
81      * Default constructor
82      */

83     public TransactBuffer()
84     {
85         m_setupBuf = new DataBuffer(DefaultSetupSize);
86         m_paramBuf = new DataBuffer(DefaultParameterSize);
87         m_dataBuf = new DataBuffer(DefaultDataSize);
88     }
89
90     /**
91      * Class constructor
92      *
93      * @param scnt int
94      * @param pcnt int
95      * @param dcnt int
96      */

97     public TransactBuffer(int scnt, int pcnt, int dcnt)
98     {
99
100         // Allocate the setup parameter buffer
101

102         if (scnt > 0)
103             m_setupBuf = new DataBuffer(scnt);
104
105         // Allocate the paramater buffer
106

107         if (pcnt > 0)
108             m_paramBuf = new DataBuffer(pcnt);
109
110         // Allocate the data buffer
111

112         if (dcnt > 0)
113             m_dataBuf = new DataBuffer(dcnt);
114
115         // Multi-packet transaction
116

117         m_multi = true;
118     }
119
120     /**
121      * Class constructor
122      *
123      * @param cmd int
124      * @param scnt int
125      * @param pcnt int
126      * @param dcnt int
127      */

128     public TransactBuffer(int cmd, int scnt, int pcnt, int dcnt)
129     {
130
131         // Set the command
132

133         setType(cmd);
134
135         // Allocate the setup parameter buffer
136

137         if (scnt > 0)
138             m_setupBuf = new DataBuffer(scnt);
139
140         // Allocate the paramater buffer
141

142         if (pcnt > 0)
143             m_paramBuf = new DataBuffer(pcnt);
144
145         // Allocate the data buffer
146

147         if (dcnt > 0)
148             m_dataBuf = new DataBuffer(dcnt);
149
150         // Multi-packet transaction
151

152         m_multi = true;
153     }
154
155     /**
156      * Class constructor
157      *
158      * @param func int
159      * @param name String
160      * @param scnt int
161      * @param pcnt int
162      * @param dcnt int
163      */

164     public TransactBuffer(int func, String JavaDoc name, int scnt, int pcnt, int dcnt)
165     {
166
167         // Set the name, for Transaction2
168

169         setName(name);
170
171         // Allocate the setup parameter buffer
172

173         if (scnt > 0)
174             m_setupBuf = new DataBuffer(scnt);
175
176         // Allocate the paramater buffer
177

178         if (pcnt > 0)
179             m_paramBuf = new DataBuffer(pcnt);
180
181         // Allocate the data buffer
182

183         if (dcnt > 0)
184             m_dataBuf = new DataBuffer(dcnt);
185
186         // Set the function code
187

188         setFunction(func);
189
190         // Multi-packet transaction
191

192         m_multi = true;
193     }
194
195     /**
196      * Class constructor
197      *
198      * @param func int
199      * @param scnt int
200      * @param pcnt int
201      * @param dbuf byte[]
202      * @param doff int
203      * @param dlen int
204      */

205     public TransactBuffer(int func, int scnt, int pcnt, byte[] dbuf, int doff, int dlen)
206     {
207
208         // Allocate the setup parameter buffer
209

210         if (scnt > 0)
211             m_setupBuf = new DataBuffer(scnt);
212
213         // Allocate the paramater buffer
214

215         if (pcnt > 0)
216             m_paramBuf = new DataBuffer(pcnt);
217
218         // Allocate the data buffer
219

220         if (dbuf != null)
221             m_dataBuf = new DataBuffer(dbuf, doff, dlen);
222
223         // Set the function code
224

225         setFunction(func);
226
227         // Multi-packet transaction
228

229         m_multi = true;
230     }
231
232     /**
233      * Determine if the tree id has been set
234      *
235      * @return boolean
236      */

237     public final boolean hasTreeId()
238     {
239         return m_treeId != -1 ? true : false;
240     }
241
242     /**
243      * Return the tree id
244      *
245      * @return int
246      */

247     public final int getTreeId()
248     {
249         return m_treeId;
250     }
251
252     /**
253      * Return the transaction type (from SBMSrvPacketType, either Transaction, Transaction2 or
254      * NTTransact)
255      *
256      * @return int
257      */

258     public final int isType()
259     {
260         return m_type;
261     }
262
263     /**
264      * Return the transaction function
265      *
266      * @return int
267      */

268     public final int getFunction()
269     {
270         return m_func;
271     }
272
273     /**
274      * Determine if the transaction has a name
275      *
276      * @return boolean
277      */

278     public final boolean hasName()
279     {
280         return m_name != null ? true : false;
281     }
282
283     /**
284      * Return the transaction name
285      *
286      * @return String
287      */

288     public final String JavaDoc getName()
289     {
290         return m_name;
291     }
292
293     /**
294      * Determine if this is a multi-packet transaction
295      *
296      * @return boolean
297      */

298     public final boolean isMultiPacket()
299     {
300         return m_multi;
301     }
302
303     /**
304      * Determine if the client is using Unicode strings
305      *
306      * @return boolean
307      */

308     public final boolean isUnicode()
309     {
310         return m_unicode;
311     }
312
313     /**
314      * Determine if the transaction buffer has setup data
315      *
316      * @return boolean
317      */

318     public final boolean hasSetupBuffer()
319     {
320         return m_setupBuf != null ? true : false;
321     }
322
323     /**
324      * Return the setup parameter buffer
325      *
326      * @return DataBuffer
327      */

328     public final DataBuffer getSetupBuffer()
329     {
330         return m_setupBuf;
331     }
332
333     /**
334      * Determine if the transaction buffer has parameter data
335      *
336      * @return boolean
337      */

338     public final boolean hasParameterBuffer()
339     {
340         return m_paramBuf != null ? true : false;
341     }
342
343     /**
344      * Return the parameter buffer
345      *
346      * @return DataBuffer
347      */

348     public final DataBuffer getParameterBuffer()
349     {
350         return m_paramBuf;
351     }
352
353     /**
354      * Determine if the transaction buffer has a data block
355      *
356      * @return boolean
357      */

358     public final boolean hasDataBuffer()
359     {
360         return m_dataBuf != null ? true : false;
361     }
362
363     /**
364      * Return the data buffer
365      *
366      * @return DataBuffer
367      */

368     public final DataBuffer getDataBuffer()
369     {
370         return m_dataBuf;
371     }
372
373     /**
374      * Return the setup return data limit
375      *
376      * @return int
377      */

378     public final int getReturnSetupLimit()
379     {
380         return m_maxSetup;
381     }
382
383     /**
384      * Return the parameter return data limit
385      *
386      * @return int
387      */

388     public final int getReturnParameterLimit()
389     {
390         return m_maxParam;
391     }
392
393     /**
394      * Return the data return data limit
395      *
396      * @return int
397      */

398     public final int getReturnDataLimit()
399     {
400         return m_maxData;
401     }
402
403     /**
404      * Set the tree id
405      *
406      * @param tid int
407      */

408     public final void setTreeId(int tid)
409     {
410         m_treeId = tid;
411     }
412
413     /**
414      * Set the transaction type
415      *
416      * @param typ int
417      */

418     public final void setType(int typ)
419     {
420         m_type = typ;
421     }
422
423     /**
424      * Set the transaction function
425      *
426      * @param func int
427      */

428     public final void setFunction(int func)
429     {
430         m_func = func;
431     }
432
433     /**
434      * Set the transaction name, for Transactin2
435      *
436      * @param name String
437      */

438     public final void setName(String JavaDoc name)
439     {
440         m_name = name;
441     }
442
443     /**
444      * Set the Unicode strings flag
445      *
446      * @param uni boolean
447      */

448     public final void setUnicode(boolean uni)
449     {
450         m_unicode = uni;
451     }
452
453     /**
454      * Set the limit of returned setup bytes
455      *
456      * @param limit int
457      */

458     public final void setReturnSetupLimit(int limit)
459     {
460         m_maxSetup = limit;
461     }
462
463     /**
464      * Set the limit of returned parameter bytes
465      *
466      * @param limit int
467      */

468     public final void setReturnParameterLimit(int limit)
469     {
470         m_maxParam = limit;
471     }
472
473     /**
474      * Set the limit of returned data bytes
475      *
476      * @param limit int
477      */

478     public final void setReturnDataLimit(int limit)
479     {
480         m_maxData = limit;
481     }
482
483     /**
484      * Set the setup, parameter and data return data limits
485      *
486      * @param slimit int
487      * @param plimit int
488      * @param dlimit int
489      */

490     public final void setReturnLimits(int slimit, int plimit, int dlimit)
491     {
492         setReturnSetupLimit(slimit);
493         setReturnParameterLimit(plimit);
494         setReturnDataLimit(dlimit);
495     }
496
497     /**
498      * Set the end of buffer positions for the setup, parameter and data buffers ready for reading
499      * the data.
500      */

501     public final void setEndOfBuffer()
502     {
503
504         // Set the end of the setup buffer
505

506         if (m_setupBuf != null)
507             m_setupBuf.setEndOfBuffer();
508
509         // Set the end of the parameter buffer
510

511         if (m_paramBuf != null)
512             m_paramBuf.setEndOfBuffer();
513
514         // Set the end of the data buffer
515

516         if (m_dataBuf != null)
517             m_dataBuf.setEndOfBuffer();
518     }
519
520     /**
521      * Append setup data to the setup data buffer
522      *
523      * @param buf byte[]
524      * @param off int
525      * @param len int
526      */

527     public final void appendSetup(byte[] buf, int off, int len)
528     {
529         m_setupBuf.appendData(buf, off, len);
530     }
531
532     /**
533      * Append parameter data to the parameter data buffer
534      *
535      * @param buf byte[]
536      * @param off int
537      * @param len int
538      */

539     public final void appendParameter(byte[] buf, int off, int len)
540     {
541         m_paramBuf.appendData(buf, off, len);
542     }
543
544     /**
545      * Append data to the data buffer
546      *
547      * @param buf byte[]
548      * @param off int
549      * @param len int
550      */

551     public final void appendData(byte[] buf, int off, int len)
552     {
553         m_dataBuf.appendData(buf, off, len);
554     }
555
556     /**
557      * Return the transaction buffer details as a string
558      *
559      * @return String
560      */

561     public String JavaDoc toString()
562     {
563         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
564
565         str.append("[");
566
567         switch (isType())
568         {
569         case PacketType.Transaction:
570             str.append("Trans");
571             break;
572         case PacketType.Transaction2:
573             str.append("Trans2(");
574             str.append(getName());
575             str.append(")");
576             break;
577         case PacketType.NTTransact:
578             str.append("NTTrans");
579             break;
580         default:
581             str.append("Unknown");
582             break;
583         }
584         str.append("-0x");
585         str.append(Integer.toHexString(getFunction()));
586
587         str.append(": setup=");
588         if (m_setupBuf != null)
589             str.append(m_setupBuf);
590         else
591             str.append("none");
592
593         str.append(",param=");
594         if (m_paramBuf != null)
595             str.append(m_paramBuf);
596         else
597             str.append("none");
598
599         str.append(",data=");
600         if (m_dataBuf != null)
601             str.append(m_dataBuf);
602         else
603             str.append("none");
604         str.append("]");
605
606         str.append(",max=");
607         str.append(getReturnSetupLimit());
608
609         str.append("/");
610         str.append(getReturnParameterLimit());
611
612         str.append("/");
613         str.append(getReturnDataLimit());
614
615         return str.toString();
616     }
617 }
618
Popular Tags