001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.transport.amqp;
018    
019    import org.fusesource.hawtbuf.Buffer;
020    
021    /**
022     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
023     */
024    public class AmqpHeader {
025    
026        static final Buffer PREFIX = new Buffer(new byte[]{
027          'A', 'M', 'Q', 'P'
028        });
029    
030        private Buffer buffer;
031    
032        public AmqpHeader(){
033            this(new Buffer(new byte[]{
034              'A', 'M', 'Q', 'P', 0, 1, 0, 0
035            }));
036        }
037    
038        public AmqpHeader(Buffer buffer){
039            setBuffer(buffer);
040        }
041    
042        public int getProtocolId() {
043            return buffer.get(4) & 0xFF;
044        }
045        public void setProtocolId(int value) {
046            buffer.data[buffer.offset+4] = (byte) value;
047        }
048    
049        public int getMajor() {
050            return buffer.get(5) & 0xFF;
051        }
052        public void setMajor(int value) {
053            buffer.data[buffer.offset+5] = (byte) value;
054        }
055    
056        public int getMinor() {
057            return buffer.get(6) & 0xFF;
058        }
059        public void setMinor(int value) {
060            buffer.data[buffer.offset+6] = (byte) value;
061        }
062    
063        public int getRevision() {
064            return buffer.get(7) & 0xFF;
065        }
066        public void setRevision(int value) {
067            buffer.data[buffer.offset+7] = (byte) value;
068        }
069    
070        public Buffer getBuffer() {
071            return buffer;
072        }
073        public void setBuffer(Buffer value) {
074            if( !value.startsWith(PREFIX) || value.length()!=8 ) {
075                throw new IllegalArgumentException("Not an AMQP header buffer");
076            }
077            buffer = value.buffer();
078        }
079    
080    
081        @Override
082        public String toString() {
083            return buffer.toString();
084        }
085    }