Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //  The contents of this file are subject to the Mozilla Public License
  //  Version 1.1 (the "License"); you may not use this file except in
  //  compliance with the License. You may obtain a copy of the License
  //  at http://www.mozilla.org/MPL/
  //
  //  Software distributed under the License is distributed on an "AS IS"
  //  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  //  the License for the specific language governing rights and
  //  limitations under the License.
 //
 //  The Original Code is RabbitMQ.
 //
 //  The Initial Developer of the Original Code is VMware, Inc.
 //  Copyright (c) 2007-2012 VMware, Inc.  All rights reserved.
 //
 
 package com.rabbitmq.client.impl;
 
 
AMQ-specific implementation of com.rabbitmq.client.Command which accumulates method, header and body from a series of frames, unless these are supplied at construction time.

Concurrency
This class is thread-safe.

 
 public class AMQCommand implements Command {

    
EMPTY_CONTENT_BODY_FRAME_SIZE = 8 = 1 + 2 + 4 + 1
  • 1 byte of frame type
  • 2 bytes of channel number
  • 4 bytes of frame payload length
  • 1 byte of payload trailer FRAME_END byte
See checkEmptyContentBodyFrameSize(), an assertion checked at startup.
 
     private static final int EMPTY_CONTENT_BODY_FRAME_SIZE = 8;

    
The assembler for this command - synchronised on - contains all the state
 
     private final CommandAssembler assembler;

    
Construct a command ready to fill in by reading frames
 
     public AMQCommand() {
         this(nullnullnull);
     }

    
Construct a command with just a method, and without header or body.

Parameters:
method the wrapped method
 
     public AMQCommand(com.rabbitmq.client.Method method) {
         this(methodnullnull);
     }

    
Construct a command with a specified method, header and body.

Parameters:
method the wrapped method
contentHeader the wrapped content header
body the message body data
 
     public AMQCommand(com.rabbitmq.client.Method methodAMQContentHeader contentHeaderbyte[] body) {
         this. = new CommandAssembler((MethodmethodcontentHeaderbody);
     }

    
Public API -
 
     public Method getMethod() {
         return this..getMethod();
     }

    
Public API -
 
     public AMQContentHeader getContentHeader() {
         return this..getContentHeader();
     }

    
Public API -
 
     public byte[] getContentBody() {
         return this..getContentBody();
     }
 
     public boolean handleFrame(Frame fthrows IOException {
         return this..handleFrame(f);
     }

    
Sends this command down the named channel on the channel's connection, possibly in multiple frames.

Parameters:
channel the channel on which to transmit the command
Throws:
java.io.IOException if an error is encountered
 
     public void transmit(AMQChannel channelthrows IOException {
         int channelNumber = channel.getChannelNumber();
         AMQConnection connection = channel.getConnection();
 
        synchronized () {
            Method m = this..getMethod();
            connection.writeFrame(m.toFrame(channelNumber));
            if (m.hasContent()) {
                byte[] body = this..getContentBody();
                connection.writeFrame(this..getContentHeader()
                        .toFrame(channelNumberbody.length));
                int frameMax = connection.getFrameMax();
                int bodyPayloadMax = (frameMax == 0) ? body.length : frameMax
                        - ;
                for (int offset = 0; offset < body.lengthoffset += bodyPayloadMax) {
                    int remaining = body.length - offset;
                    int fragmentLength = (remaining < bodyPayloadMax) ? remaining
                            : bodyPayloadMax;
                    Frame frame = Frame.fromBodyFragment(channelNumberbody,
                            offsetfragmentLength);
                    connection.writeFrame(frame);
                }
            }
        }
    }
    @Override public String toString() {
        return toString(false);
    }
    public String toString(boolean suppressBody){
        synchronized () {
            return new StringBuilder()
                .append('{')
                .append(this..getMethod())
                .append(", ")
                .append(this..getContentHeader())
                .append(", ")
                .append(contentBodyStringBuilder(
                        this..getContentBody(), suppressBody))
                .append('}').toString();
        }
    }
    private static StringBuilder contentBodyStringBuilder(byte[] bodyboolean suppressBody) {
        try {
            if (suppressBody) {
                return new StringBuilder().append(body.length).append(" bytes of payload");
            } else {
                return new StringBuilder().append('\"').append(new String(body"UTF-8")).append('\"');
            }
        } catch (Exception e) {
            return new StringBuilder().append('|').append(body.length).append('|');
        }
    }

    
Called to check internal code assumptions.
    public static void checkPreconditions() {
    }

    
Since we're using a pre-computed value for EMPTY_CONTENT_BODY_FRAME_SIZE we check this is actually correct when run against the framing code in Frame.
    private static void checkEmptyContentBodyFrameSize() {
        Frame f = new Frame(., 0, new byte[0]);
        ByteArrayOutputStream s = new ByteArrayOutputStream();
        try {
            f.writeTo(new DataOutputStream(s));
        } catch (IOException ioe) {
            throw new AssertionError("IOException while checking EMPTY_CONTENT_BODY_FRAME_SIZE");
        }
        int actualLength = s.toByteArray().length;
        if ( != actualLength) {
            throw new AssertionError("Internal error: expected EMPTY_CONTENT_BODY_FRAME_SIZE("
                    + 
                    + ") is not equal to computed value: " + actualLength);
        }
    }
New to GrepCode? Check out our FAQ X