-
Notifications
You must be signed in to change notification settings - Fork 33
/
Packet.java
2148 lines (1782 loc) · 68.1 KB
/
Packet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.io;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;
/**
*
* Encapsulation of an iMQ packet. A packet consists of three parts:
*
* 1) A fixed sized header (size HEADER_SIZE bytes) 2) A variable sized header (for holding strings, and optional packet
* fields) 3) A serialized java.util.Hashtable (for holding message properties) 4) the packet body (a bag of bytes)
*
* This class directly manages #1, uses PacketVariableHeader to manage #2, and uses PacketPayload to manage #3 and #4.
*
* This class also employs the use of a buffer pool to manager reuse of direct ByteBuffers. Direct ByteBuffers are an
* order of magnitude more expensive to allocate than normal ByteBuffers, so keeping a pool of them around is a big win
* (which is not typically the case for memory allocated off of the Java heap).
*
*/
public class Packet implements JMSPacket {
protected boolean destroyed = false;
// Packet magic number. NEVER change this
public static final int MAGIC = 469754818;
// Packet version numbers
public static final short VERSION1 = 103;
public static final short VERSION2 = 200;
public static final short VERSION3 = 301;
public static final short CURRENT_VERSION = VERSION3;
static short defaultVersion = CURRENT_VERSION;
// Number of bytes in fixed header portion of packet
public static final int HEADER_SIZE = 72;
public static final int DEFAULT_POOL_SIZE = 1024 * 1024;
public static final int DEFAULT_POOL_BLOCKSIZE = 128;
// Global sequence number (optionally) tagged onto packets
private static int sequenceNumber = 0;
// A global pool of ByteBuffers. This is used to reuse direct
// ByteBuffers which are expensive to allocate and deallocate
// Note: the pool is only used if the packet is using direct ByteBuffers.
protected static final ByteBufferPool bbPool;
// Maximum size a packet can be, and the lower and upper bounds
// this value can be. We put a relatively high lower bound
// so that control packets are not impacted.
private static long SIZE_LOWER_BOUND = 512 * 1024;
protected static final long SIZE_UPPER_BOUND = Integer.MAX_VALUE;
private static long maxPacketSize = SIZE_UPPER_BOUND;
// true to automatically generate timestamps
protected boolean genTimestamp = true;
// true to automatically generate sequence numbers
protected boolean genSequenceNumber = true;
// Buffer to hold fixed portion of header
protected ByteBuffer fixedBuf = null;
// Dirty flag. This is true when the fixedBuf is out-of-date
// with respect to the state of this object.
protected boolean bufferDirty = false;
// True to use direct ByteBuffers, else false.
protected boolean useDirect = false;
// Buffers used for reading packet off of wire. We keep these around just
// in case we want to reuse them for subsequent reads if the packet
// is reused
protected ByteBuffer varBuf = null;
protected ByteBuffer propBuf = null;
protected ByteBuffer bodyBuf = null;
// Array used to hold buffers during reads and writes.
protected ByteBuffer[] writeBufs = new ByteBuffer[4];
protected ByteBuffer[] readBufs = new ByteBuffer[4];
// XXX 1/24/2002 dipol: Needed to work around nio bug (imq 4627557)
protected int[] readBufsLimits = new int[4];
protected int nBufs = 0;
// A list of all buffers allocated by this packet. We track this
// so we can easily return them to the pool when the packet is destroyed
protected ArrayList allocatedBuffers = new ArrayList(8);
// Values in fixed header
protected short version = defaultVersion;
protected int magic = MAGIC;
protected short packetType = 0;
protected int packetSize = 0;
protected long expiration = 0;
protected int propertyOffset = 0;
protected int propertySize = 0;
protected byte encryption = 0;
protected long transactionID = 0;
protected byte priority = 5;
protected int bitFlags = 0;
protected long consumerID = 0;
// Holds sequence, IPaddr, port and timestamp
protected SysMessageID sysMessageID = new SysMessageID();
// Classes that handle the variable portion of the packet and the payload
protected PacketVariableHeader packetVariableHeader = null;
protected PacketPayload packetPayload = null;
// Used to track state of in-progress non-blocking I/O
protected boolean readInProgress = false;
protected boolean writeInProgress = false;
protected boolean versionMismatch = false;
protected int headerBytesRead = 0;
protected int ropBytesRead = 0;
protected int bytesWritten = 0;
static {
// XXX - the buffer pool should be tunable through properties
bbPool = new ByteBufferPool(DEFAULT_POOL_SIZE, true);
bbPool.setBlockSize(HEADER_SIZE);
}
// Maximum size packet we should read. This let's the broker
// protect against denial of service attacks if an overly
// large packet is sent.
/**
* Set the max packet size that should be read. If a packet is larger than this the packet read methods will skip the
* bytes and throw an exception.
*
* There is a lower bound on the smallest value you can set the max packet size to. So the actual value set may be
* different than the value passed. This method returns the actual value set.
*/
public static synchronized long setMaxPacketSize(long n) {
if (n < SIZE_LOWER_BOUND) {
maxPacketSize = SIZE_LOWER_BOUND;
} else {
maxPacketSize = n;
}
return maxPacketSize;
}
public static synchronized long getMaxPacketSize() {
return maxPacketSize;
}
public static void setSizeLowerBound(long n) {
SIZE_LOWER_BOUND = n;
}
public static long getSizeLowerBound() {
return SIZE_LOWER_BOUND;
}
public static synchronized ByteBufferPool getBufferPool() {
return bbPool;
}
/**
* Constructs an empty packet that will use direct buffers
*/
public Packet() {
this(false);
}
/**
* Construct a packet indicating whether or not to use direct ByteBuffers.
*
* If you plan on reading and writing packets using the methods that take a channel as a parameter then you should
* specify useDirect=true.
*
* If you plan on reading and writing packets using the methods that take an IO Stream as a parameter then you should
* specify useDirect = false.
*
* Default is to use direct buffers.
*
*/
public Packet(boolean useDirect) {
this.useDirect = useDirect;
this.reset();
}
/**
* Fill this packet with the contents of sourcePacket. The fixed and variable headers are copied. The payload
* (properties and body) are shared. This method turns off timestamp and sequence number generation since we assume you
* want the copied header contents to remain unchanged.
*/
public synchronized void fill(Packet sourcePacket) throws IOException {
fill(sourcePacket, false);
}
public synchronized void fill(Packet sourcePacket, boolean deep) throws IOException {
this.reset();
synchronized (sourcePacket) {
sourcePacket.updateBuffers();
// Copy fixed header from source packet
ByteBuffer buf = sourcePacket.getHeaderBytes();
buf.rewind();
fixedBuf.rewind();
fixedBuf.put(buf);
buf.rewind();
fixedBuf.rewind();
// Copy variable header from source packet. Note this IS a copy.
try {
buf = sourcePacket.getPacketVariableHeader().getBytes();
} catch (IOException e) {
// Should never happen
System.out.println("Could not get variable header" + e);
return;
}
if (buf != null) {
ByteBuffer newBuf = packetVariableHeader.getBytes();
if (newBuf == null || newBuf.capacity() < buf.limit()) {
// Need a new buffer
newBuf = allocateBuffer(buf.limit());
} else {
// Can reuse existing buffer
newBuf.limit(buf.limit());
newBuf.rewind();
}
newBuf.put(buf);
newBuf.rewind();
buf.rewind();
packetVariableHeader.setBytes(newBuf);
}
// Get payload from source packet. Note this is NOT a copy
PacketPayload sourcePayload = sourcePacket.getPacketPayload();
if (sourcePayload != null) {
// If not deep: set our payload buffers to be those of the source packet.
// The packets will share payload content, but each must have
// their own position, mark and limit.
ByteBuffer b = sourcePayload.getPropertiesBytes(version);
if (b != null) {
if (deep) { // copy everything
ByteBuffer newb = allocateBuffer(b.limit());
newb.put(b);
newb.rewind();
packetPayload.setPropertiesBytes(newb, version);
} else { // shallow - use the same buffer
packetPayload.setPropertiesBytes(b.duplicate().rewind(), version);
}
}
b = sourcePayload.getBodyBytes();
if (b != null) {
if (deep) { // copy everything
ByteBuffer newb = allocateBuffer(b.limit());
newb.put(b);
newb.rewind();
packetPayload.setBody(newb);
} else {
packetPayload.setBody(b.duplicate().rewind());
}
b.rewind();
}
}
} // end synchronized
// Populate packet with the new info
parseFixedBuffer(fixedBuf);
// Forces transaction ID to be populated if VERSION2
getTransactionID();
// Since a filled packet clones the header of another packet we
// turn off sequence and timestamp generation.
generateSequenceNumber(false);
generateTimestamp(false);
return;
}
/**
* Parse the fixed packet header into instance variables.
*/
protected void parseFixedBuffer(ByteBuffer buf) throws StreamCorruptedException, IOException {
buf.rewind();
magic = buf.getInt();
if (magic != MAGIC) {
throw new StreamCorruptedException("Bad packet magic number: " + magic + ". Expecting: " + MAGIC);
}
version = buf.getShort();
packetType = buf.getShort();
packetSize = buf.getInt();
// Up to this point the packet header stays the same between
// versions. After this point it may be different depending
// on the version. If the version is something we don't understand
// then the following will generate garbage values, but we will
// catch the version error later.
// In VERSION1 the transactionID is a 32 bit value in the header.
// In VERSION2 It became a 64 bit value in the variable portion
// of the packet.
if (version == VERSION1) {
transactionID = buf.getInt();
}
expiration = buf.getLong();
// Reads timestamp, source IP address, source port and sequence number
// Hack to get a data input stream for SysMessageID.
try (InputStream is = new JMQByteBufferInputStream(buf)) {
try (DataInputStream dis = new DataInputStream(is)) {
sysMessageID.readID(dis);
}
}
propertyOffset = buf.getInt();
propertySize = buf.getInt();
priority = buf.get();
encryption = buf.get();
bitFlags = (buf.getShort());
// In VERSION2 the consumerID is a long
if (version == VERSION1) {
consumerID = buf.getInt();
// There was a bug in the 2.0 packet code that left cruft in the
// uppper 8 bits of the bitFlags. The upper 8 bits was never
// used in 2.0 so this problem was not noticed until later.
// See bug 4948563 for more info.
// Zero out the upper 8 bits to clear the garbage.
bitFlags = bitFlags & 0x00FF;
} else {
consumerID = buf.getLong();
}
buf.rewind();
return;
}
public boolean getFlag(int flag) {
return ((bitFlags & flag) == flag);
}
public int getVersion() {
return version;
}
public int getMagic() {
return magic;
}
public int getPacketType() {
return packetType;
}
/**
* Return whether this is a reply packet
*
* By convention, a reply packet has a packetType which is odd and is {@literal >=} 9
*
* In addition, a AUTHENTICATE_REQUEST is considered a reply despite having an odd packetType
*
* @return whether this is a reply packet
*/
public boolean isReply() {
return (((packetType >= 9) && (packetType % 2 != 0)) || (packetType == PacketType.AUTHENTICATE_REQUEST));
}
public synchronized int getPacketSize() {
// Make sure value is correct. If packet is not dirty the update is
// a no-op
try {
updateBuffers();
} catch (IOException e) {
}
return packetSize;
}
public synchronized long getTransactionID() {
// In VERSION1 the transactionID was in the fixed header. It moved
// to the variable header in VERSION2.
if (version >= VERSION2) {
transactionID = packetVariableHeader.getLongField(PacketString.TRANSACTIONID);
}
return transactionID;
}
public synchronized long getProducerID() {
return packetVariableHeader.getLongField(PacketString.PRODUCERID);
}
public synchronized long getDeliveryTime() {
return packetVariableHeader.getLongField(PacketString.DELIVERY_TIME);
}
public synchronized int getDeliveryCount() {
return packetVariableHeader.getIntField(PacketString.DELIVERY_COUNT);
}
public synchronized long getTimestamp() {
return sysMessageID.timestamp;
}
public synchronized long getExpiration() {
return expiration;
}
public int getPort() {
return sysMessageID.port;
}
public synchronized String getIPString() {
return sysMessageID.toString();
}
public synchronized byte[] getIP() {
return sysMessageID.getIPAddress();
}
public int getSequence() {
return sysMessageID.sequence;
}
public synchronized int getPropertyOffset() {
try {
updateBuffers();
} catch (IOException e) {
}
return propertyOffset;
}
public synchronized int getPropertySize() {
try {
updateBuffers();
} catch (IOException e) {
}
return propertySize;
}
public int getEncryption() {
return encryption;
}
public int getPriority() {
return priority;
}
public synchronized long getConsumerID() {
return consumerID;
}
public boolean getPersistent() {
return getFlag(PacketFlag.P_FLAG);
}
public boolean getRedelivered() {
return getFlag(PacketFlag.R_FLAG);
}
public boolean getIsQueue() {
return getFlag(PacketFlag.Q_FLAG);
}
public boolean getSelectorsProcessed() {
return getFlag(PacketFlag.S_FLAG);
}
public boolean getSendAcknowledge() {
return getFlag(PacketFlag.A_FLAG);
}
public boolean getIsLast() {
return getFlag(PacketFlag.L_FLAG);
}
public boolean getFlowPaused() {
return getFlag(PacketFlag.F_FLAG);
}
public boolean getIsTransacted() {
return getFlag(PacketFlag.T_FLAG);
}
public boolean getConsumerFlow() {
return getFlag(PacketFlag.C_FLAG);
}
public boolean getIndempotent() {
return getFlag(PacketFlag.I_FLAG);
}
public boolean getWildcard() {
return getFlag(PacketFlag.W_FLAG);
}
/**
* Get the MessageID for the packet. If the client has set a MessageID then that is what is returned. Otherwise the
* system message ID is returned (see getSysMessageID())
*
* @return The packet's MessageID
*/
public synchronized String getMessageID() {
String messageID = packetVariableHeader.getStringField(PacketString.MESSAGEID);
if (messageID == null) {
return sysMessageID.toString();
} else {
return messageID;
}
}
public synchronized void setMessageID(String messageID) {
// sets the MESSAGEID string
packetVariableHeader.setStringField(PacketString.MESSAGEID, messageID);
}
/**
* Get the system message ID. Note that this is not the JMS MessageID set by the client. Rather this is a system-wide
* unique message ID generated from the timestamp, sequence number, port number and IP address of the packet.
* <P>
* WARNING! This returns a references to the Packet's SysMessageID not a copy.
*
* @return The packet's system MessageID
*
*/
public synchronized SysMessageID getSysMessageID() {
return sysMessageID;
}
public synchronized String getDestination() {
return packetVariableHeader.getStringField(PacketString.DESTINATION);
}
public synchronized String getDestinationClass() {
return packetVariableHeader.getStringField(PacketString.DESTINATION_CLASS);
}
public synchronized String getCorrelationID() {
return packetVariableHeader.getStringField(PacketString.CORRELATIONID);
}
public synchronized String getReplyTo() {
return packetVariableHeader.getStringField(PacketString.REPLYTO);
}
public synchronized String getReplyToClass() {
return packetVariableHeader.getStringField(PacketString.REPLYTO_CLASS);
}
public synchronized String getMessageType() {
return packetVariableHeader.getStringField(PacketString.TYPE);
}
/**
* Return the property hashtable for this packet.
*
* WARNING! This method emphasizes performance over safety. The HashTable object returned is a reference to the
* HashTable object in the object -- it is NOT a copy. Modifying the contents of the HashTable will have
* non-deterministic results so don't do it!
*/
public synchronized Hashtable getProperties() throws IOException, ClassNotFoundException {
return packetPayload.getProperties();
}
/**
* Return the size of the message body in bytes
*
* @return Size of message body in bytes
*/
public synchronized int getMessageBodySize() {
return packetPayload.getBodySize();
}
/**
* Return the message body as a ByteBuffer.
*
*/
public synchronized ByteBuffer getMessageBodyByteBuffer() {
return packetPayload.getBodyBytes();
}
/**
* Return the message body as a byte array
*/
public synchronized byte[] getMessageBodyByteArray() {
ByteBuffer bb = packetPayload.getBodyBytes();
if (bb != null && bb.hasArray()) {
return bb.array();
}
return null;
}
/**
* Clear the message body of this packate
*/
public synchronized void clearMessageBody() {
if (packetPayload != null) {
packetPayload.setBody(null);
}
}
/**
* Return an InputStream that contains the contents of the message body.
*
* @return An InputStream from which the message body can be read from. Or null if no message body.
*/
public synchronized InputStream getMessageBodyStream() {
return packetPayload.getBodyStream();
}
/**
* Reset packet to initial values. This does not free buffers just in case we can reused them.
*/
public synchronized void reset() {
version = VERSION3;
magic = MAGIC;
packetType = 0;
packetSize = 0;
expiration = 0;
propertyOffset = 0;
propertySize = 0;
encryption = 0;
priority = 5;
bitFlags = 0;
consumerID = 0;
transactionID = 0;
readInProgress = false;
headerBytesRead = 0;
ropBytesRead = 0;
bufferDirty = false;
sysMessageID.clear();
if (fixedBuf != null) {
fixedBuf.clear();
} else {
fixedBuf = allocateBuffer(HEADER_SIZE);
}
if (varBuf != null) {
varBuf.clear();
}
if (propBuf != null) {
propBuf.clear();
}
if (bodyBuf != null) {
bodyBuf.clear();
}
if (packetVariableHeader != null) {
packetVariableHeader.reset();
} else {
packetVariableHeader = new PacketVariableHeader();
}
if (packetPayload != null) {
packetPayload.reset();
} else {
packetPayload = new PacketPayload();
}
bigPacketEx = null;
}
/**
* Update all buffers to reflect the current state of the object.
*/
public void updateBuffers() throws IOException {
if (!bufferDirty) {
return;
}
// Forces variable header and payload to update their buffers.
// Then we can compute sizes correctly
ByteBuffer varBytes;
if (version == VERSION1) {
// The 2.0 packet code has a bug where it requires something
// in the variable portion of the header, even if it is just
// the Null list terminator. getBytes2() addresses this.
varBytes = packetVariableHeader.getBytes2();
} else {
varBytes = packetVariableHeader.getBytes();
}
ByteBuffer propBytes = packetPayload.getPropertiesBytes(version);
ByteBuffer bodyBytes = packetPayload.getBodyBytes();
propertySize = ((propBytes == null) ? 0 : propBytes.limit());
int varSize = ((varBytes == null) ? 0 : varBytes.limit());
int bodySize = ((bodyBytes == null) ? 0 : bodyBytes.limit());
propertyOffset = HEADER_SIZE + varSize;
packetSize = propertyOffset + propertySize + bodySize;
fixedBuf.rewind();
// Write data into fixed header
fixedBuf.putInt(MAGIC);
fixedBuf.putShort(version);
fixedBuf.putShort(packetType);
fixedBuf.putInt(packetSize);
// In VERSION1 transactionID was a 32bit value in the header
if (version == VERSION1) {
fixedBuf.putInt((int) transactionID);
}
fixedBuf.putLong(expiration);
// Writes timestamp, source IP addr, source port, and sequence number
// Hack to get a data output stream for SysMessageID.
try (OutputStream os = new JMQByteBufferOutputStream(fixedBuf)) {
try (DataOutputStream dos = new DataOutputStream(os)) {
sysMessageID.writeID(dos);
}
}
fixedBuf.putInt(propertyOffset);
fixedBuf.putInt(propertySize);
fixedBuf.put(priority);
fixedBuf.put(encryption);
fixedBuf.putShort((short) bitFlags);
// In VERSION1 consumerID is 32bits. In VERSION2 it is 64bits
if (version == VERSION1) {
fixedBuf.putInt((int) consumerID);
} else {
fixedBuf.putLong(consumerID);
}
bufferDirty = false;
fixedBuf.rewind();
}
/**
* Set the packet type.
*
* @param pType The type of packet
*/
public synchronized void setPacketType(int pType) {
packetType = (short) pType;
bufferDirty = true;
}
public synchronized void setTimestamp(long t) {
sysMessageID.setTimestamp(t);
bufferDirty = true;
}
public synchronized void setExpiration(long e) {
expiration = e;
bufferDirty = true;
}
public synchronized void setPort(int p) {
sysMessageID.port = p;
bufferDirty = true;
}
public synchronized void setIP(byte[] ip) {
sysMessageID.setIPAddress(ip);
bufferDirty = true;
}
public synchronized void setIP(byte[] ip, byte[] mac) {
sysMessageID.setIPAddress(ip, mac);
bufferDirty = true;
}
public synchronized void setSequence(int n) {
sysMessageID.setSequence(n);
bufferDirty = true;
}
// Version should be VERSION1, VERSION2 or VERSION3. Default is VERSION3
public synchronized void setVersion(int n) {
if (version != (short) n) {
version = (short) n;
// We are going from VERSION1 to VERSION2 packet. We explicitly
// set the transactionID to make sure it gets set in the
// variable portion of the packet.
if (version >= VERSION2) {
setTransactionID(transactionID);
}
bufferDirty = true;
}
}
public synchronized void setTransactionID(long n) {
transactionID = n;
bufferDirty = true;
// For version2 packets value is in the varible header. We save
// it in the fixed header as well -- just in case somebody changes
// the version later.
if (version >= VERSION2) {
packetVariableHeader.setLongField(PacketString.TRANSACTIONID, n);
}
}
public synchronized void setProducerID(long n) {
packetVariableHeader.setLongField(PacketString.PRODUCERID, n);
bufferDirty = true;
}
public synchronized void setDeliveryCount(int n) {
packetVariableHeader.setIntField(PacketString.DELIVERY_COUNT, n);
bufferDirty = true;
}
public synchronized void setDeliveryTime(long n) {
packetVariableHeader.setLongField(PacketString.DELIVERY_TIME, n);
bufferDirty = true;
}
public synchronized void setEncryption(int e) {
encryption = (byte) e;
bufferDirty = true;
}
public synchronized void setPriority(int p) {
priority = (byte) p;
bufferDirty = true;
}
public synchronized void setConsumerID(long n) {
consumerID = n;
bufferDirty = true;
}
public void setPersistent(boolean b) {
setFlag(PacketFlag.P_FLAG, b);
}
public void setRedelivered(boolean b) {
setFlag(PacketFlag.R_FLAG, b);
}
public void setIsQueue(boolean b) {
setFlag(PacketFlag.Q_FLAG, b);
}
public void setSelectorsProcessed(boolean b) {
setFlag(PacketFlag.S_FLAG, b);
}
public void setSendAcknowledge(boolean b) {
setFlag(PacketFlag.A_FLAG, b);
}
public void setIsLast(boolean b) {
setFlag(PacketFlag.L_FLAG, b);
}
public void setFlowPaused(boolean b) {
setFlag(PacketFlag.F_FLAG, b);
}
public void setIsTransacted(boolean b) {
setFlag(PacketFlag.T_FLAG, b);
}
public void setConsumerFlow(boolean b) {
setFlag(PacketFlag.C_FLAG, b);
}
public void setIndempotent(boolean b) {
setFlag(PacketFlag.I_FLAG, b);
}
public void setWildcard(boolean b) {
setFlag(PacketFlag.W_FLAG, b);
}
public synchronized void setFlag(int flag, boolean on) {
if (on) {
bitFlags = bitFlags | flag;
} else {
bitFlags = bitFlags & ~flag;
}
bufferDirty = true;
}
public synchronized void setDestination(String s) {
packetVariableHeader.setStringField(PacketString.DESTINATION, s);
bufferDirty = true;
}
public synchronized void setDestinationClass(String s) {
packetVariableHeader.setStringField(PacketString.DESTINATION_CLASS, s);
bufferDirty = true;
}
public synchronized void setCorrelationID(String s) {
packetVariableHeader.setStringField(PacketString.CORRELATIONID, s);
bufferDirty = true;
}
public synchronized void setReplyTo(String s) {
packetVariableHeader.setStringField(PacketString.REPLYTO, s);
bufferDirty = true;
}
public synchronized void setReplyToClass(String s) {
packetVariableHeader.setStringField(PacketString.REPLYTO_CLASS, s);
bufferDirty = true;
}
public synchronized void setMessageType(String s) {
packetVariableHeader.setStringField(PacketString.TYPE, s);
bufferDirty = true;
}
/**
* Set the message properties. WARNING! The Hashtable is NOT copied.
*
* @param props The message properties.
*/
public synchronized void setProperties(Hashtable props) {
packetPayload.setProperties(props);
bufferDirty = true;
}
/**
* Set the message body. 'body' is sliced to derive the message body so be careful what the buffers position is! Note:
* If you allocate a direct ByteBuffer and pass it here you will get better performance than passing a byte[]
*
* @param body The message body.
*/
public synchronized void setMessageBody(ByteBuffer body) {
packetPayload.setBody(body.slice());
bufferDirty = true;
}
/**
* Set the message body. WARNING! The byte array is NOT copied.
*
* @param body The message body.
*/
public synchronized void setMessageBody(byte[] body) {
// ByteBuffer buf = ByteBuffer.wrap(body);
ByteBuffer buf = null;
// check if body is null
if (body != null) {
buf = ByteBuffer.wrap(body);
}
packetPayload.setBody(buf);
bufferDirty = true;
}
/**
* Set the message body. Specify offset and length of where to take data from buffer. WARNING! The byte array is NOT
* copied.
*
* @param body The message body.
* @param off Offset into body that data starts
* @param len Size of message body
*/
public synchronized void setMessageBody(byte[] body, int off, int len) {
ByteBuffer buf = ByteBuffer.wrap(body, off, len);
packetPayload.setBody(buf);