@@ -48,47 +48,26 @@ public class AMQCommand implements Command {
4848 private final CommandAssembler assembler ;
4949 private final Lock assemblerLock = new ReentrantLock ();
5050
51+ /** Construct a command for inbound frame assembly with a max body length. */
5152 AMQCommand (int maxBodyLength ) {
52- this ( null , null , null , maxBodyLength );
53+ this . assembler = new CommandAssembler ( null , null , maxBodyLength );
5354 }
5455
55- /** Construct a command ready to fill in by reading frames */
56+ /** Construct a command ready to fill in by reading frames. */
5657 public AMQCommand () {
57- this (null , null , null , Integer .MAX_VALUE );
58+ this (Integer .MAX_VALUE );
5859 }
5960
6061 /**
6162 * Construct a command with just a method, and without header or body.
6263 * @param method the wrapped method
6364 */
6465 public AMQCommand (com .rabbitmq .client .Method method ) {
65- this ( method , null , null , Integer .MAX_VALUE );
66+ this . assembler = new CommandAssembler (( Method ) method , null , Integer .MAX_VALUE );
6667 }
6768
6869 /**
69- * Construct a command with a specified method, header and body.
70- * @param method the wrapped method
71- * @param contentHeader the wrapped content header
72- * @param body the message body data
73- */
74- public AMQCommand (com .rabbitmq .client .Method method , AMQContentHeader contentHeader , byte [] body ) {
75- this .assembler = new CommandAssembler ((Method ) method , contentHeader , body , Integer .MAX_VALUE );
76- }
77-
78- /**
79- * Construct a command with a specified method, header and body.
80- * @param method the wrapped method
81- * @param contentHeader the wrapped content header
82- * @param body the message body data
83- * @param maxBodyLength the maximum size for an inbound message body
84- */
85- public AMQCommand (com .rabbitmq .client .Method method , AMQContentHeader contentHeader , byte [] body ,
86- int maxBodyLength ) {
87- this .assembler = new CommandAssembler ((Method ) method , contentHeader , body , maxBodyLength );
88- }
89-
90- /**
91- * Construct a command with a ByteBuffer body for zero-copy transmission.
70+ * Construct a command with a ByteBuffer body for transmission.
9271 * @param method the wrapped method
9372 * @param contentHeader the wrapped content header
9473 * @param body the message body as a ByteBuffer
@@ -133,11 +112,29 @@ public void transmit(AMQChannel channel) throws IOException {
133112 try {
134113 Method m = this .assembler .getMethod ();
135114 if (m .hasContent ()) {
136- ByteBuffer bbBody = this .assembler .getByteBufferBody ();
137- if (bbBody != null ) {
138- transmitWithByteBuffer (m , bbBody , channelNumber , connection );
139- } else {
140- transmitWithByteArray (m , this .assembler .getContentBody (), channelNumber , connection );
115+ ByteBuffer body = this .assembler .getByteBufferBody ();
116+ int bodySize = body == null ? 0 : body .remaining ();
117+ Frame headerFrame = this .assembler .getContentHeader ().toFrame (channelNumber , bodySize );
118+
119+ int frameMax = connection .getFrameMax ();
120+ boolean cappedFrameMax = frameMax > 0 ;
121+ int bodyPayloadMax = cappedFrameMax ? frameMax - EMPTY_FRAME_SIZE : bodySize ;
122+
123+ if (cappedFrameMax && headerFrame .size () > frameMax ) {
124+ String msg = String .format ("Content headers exceeded max frame size: %d > %d" , headerFrame .size (), frameMax );
125+ throw new IllegalArgumentException (msg );
126+ }
127+ connection .writeFrame (m .toFrame (channelNumber ));
128+ connection .writeFrame (headerFrame );
129+
130+ if (body != null ) {
131+ int bodyPosition = body .position ();
132+ for (int offset = 0 ; offset < bodySize ; offset += bodyPayloadMax ) {
133+ int remaining = bodySize - offset ;
134+ int fragmentLength = (remaining < bodyPayloadMax ) ? remaining : bodyPayloadMax ;
135+ Frame frame = Frame .fromBodyFragment (channelNumber , body , bodyPosition + offset , fragmentLength );
136+ connection .writeFrame (frame );
137+ }
141138 }
142139 } else {
143140 connection .writeFrame (m .toFrame (channelNumber ));
@@ -149,52 +146,6 @@ public void transmit(AMQChannel channel) throws IOException {
149146 connection .flush ();
150147 }
151148
152- private void transmitWithByteArray (Method m , byte [] body , int channelNumber , AMQConnection connection ) throws IOException {
153- Frame headerFrame = this .assembler .getContentHeader ().toFrame (channelNumber , body .length );
154-
155- int frameMax = connection .getFrameMax ();
156- boolean cappedFrameMax = frameMax > 0 ;
157- int bodyPayloadMax = cappedFrameMax ? frameMax - EMPTY_FRAME_SIZE : body .length ;
158-
159- if (cappedFrameMax && headerFrame .size () > frameMax ) {
160- String msg = String .format ("Content headers exceeded max frame size: %d > %d" , headerFrame .size (), frameMax );
161- throw new IllegalArgumentException (msg );
162- }
163- connection .writeFrame (m .toFrame (channelNumber ));
164- connection .writeFrame (headerFrame );
165-
166- for (int offset = 0 ; offset < body .length ; offset += bodyPayloadMax ) {
167- int remaining = body .length - offset ;
168- int fragmentLength = (remaining < bodyPayloadMax ) ? remaining : bodyPayloadMax ;
169- Frame frame = Frame .fromBodyFragment (channelNumber , body , offset , fragmentLength );
170- connection .writeFrame (frame );
171- }
172- }
173-
174- private void transmitWithByteBuffer (Method m , ByteBuffer body , int channelNumber , AMQConnection connection ) throws IOException {
175- int bodySize = body .remaining ();
176- Frame headerFrame = this .assembler .getContentHeader ().toFrame (channelNumber , bodySize );
177-
178- int frameMax = connection .getFrameMax ();
179- boolean cappedFrameMax = frameMax > 0 ;
180- int bodyPayloadMax = cappedFrameMax ? frameMax - EMPTY_FRAME_SIZE : bodySize ;
181-
182- if (cappedFrameMax && headerFrame .size () > frameMax ) {
183- String msg = String .format ("Content headers exceeded max frame size: %d > %d" , headerFrame .size (), frameMax );
184- throw new IllegalArgumentException (msg );
185- }
186- connection .writeFrame (m .toFrame (channelNumber ));
187- connection .writeFrame (headerFrame );
188-
189- int bodyPosition = body .position ();
190- for (int offset = 0 ; offset < bodySize ; offset += bodyPayloadMax ) {
191- int remaining = bodySize - offset ;
192- int fragmentLength = (remaining < bodyPayloadMax ) ? remaining : bodyPayloadMax ;
193- Frame frame = Frame .fromBodyFragment (channelNumber , body , bodyPosition + offset , fragmentLength );
194- connection .writeFrame (frame );
195- }
196- }
197-
198149 @ Override public String toString () {
199150 return toString (false );
200151 }
0 commit comments