AMQFrame.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.framing;
022 
023 import org.apache.mina.common.ByteBuffer;
024 
025 public class AMQFrame extends AMQDataBlock implements EncodableAMQDataBlock
026 {
027     private final int _channel;
028 
029     private final AMQBody _bodyFrame;
030     public static final byte FRAME_END_BYTE = (byte0xCE;
031 
032 
033     public AMQFrame(final int channel, final AMQBody bodyFrame)
034     {
035         _channel = channel;
036         _bodyFrame = bodyFrame;
037     }
038 
039     public AMQFrame(final ByteBuffer in, final int channel, final long bodySize, final BodyFactory bodyFactorythrows AMQFrameDecodingException
040     {
041         this._channel = channel;
042         this._bodyFrame = bodyFactory.createBody(in,bodySize);
043     }
044 
045     public long getSize()
046     {
047         return + _bodyFrame.getSize() 1;
048     }
049 
050     public static final int getFrameOverhead()
051     {
052         return 1;
053     }
054 
055 
056     public void writePayload(ByteBuffer buffer)
057     {
058         buffer.put(_bodyFrame.getFrameType());
059         EncodingUtils.writeUnsignedShort(buffer, _channel);
060         EncodingUtils.writeUnsignedInteger(buffer, _bodyFrame.getSize());
061         _bodyFrame.writePayload(buffer);
062         buffer.put(FRAME_END_BYTE);
063     }
064 
065     public final int getChannel()
066     {
067         return _channel;
068     }
069 
070     public final AMQBody getBodyFrame()
071     {
072         return _bodyFrame;
073     }
074 
075     public String toString()
076     {
077         return "Frame channelId: " + _channel + ", bodyFrame: " + String.valueOf(_bodyFrame);
078     }
079 
080     public static void writeFrame(ByteBuffer buffer, final int channel, AMQBody body)
081     {
082         buffer.put(body.getFrameType());
083         EncodingUtils.writeUnsignedShort(buffer, channel);
084         EncodingUtils.writeUnsignedInteger(buffer, body.getSize());
085         body.writePayload(buffer);
086         buffer.put(FRAME_END_BYTE);
087 
088     }
089 
090     public static void writeFrames(ByteBuffer buffer, final int channel, AMQBody body1, AMQBody body2)
091     {
092         buffer.put(body1.getFrameType());
093         EncodingUtils.writeUnsignedShort(buffer, channel);
094         EncodingUtils.writeUnsignedInteger(buffer, body1.getSize());
095         body1.writePayload(buffer);
096         buffer.put(FRAME_END_BYTE);
097         buffer.put(body2.getFrameType());
098         EncodingUtils.writeUnsignedShort(buffer, channel);
099         EncodingUtils.writeUnsignedInteger(buffer, body2.getSize());
100         body2.writePayload(buffer);
101         buffer.put(FRAME_END_BYTE);
102 
103     }
104 
105     public static void writeFrames(ByteBuffer buffer, final int channel, AMQBody body1, AMQBody body2, AMQBody body3)
106     {
107         buffer.put(body1.getFrameType());
108         EncodingUtils.writeUnsignedShort(buffer, channel);
109         EncodingUtils.writeUnsignedInteger(buffer, body1.getSize());
110         body1.writePayload(buffer);
111         buffer.put(FRAME_END_BYTE);
112         buffer.put(body2.getFrameType());
113         EncodingUtils.writeUnsignedShort(buffer, channel);
114         EncodingUtils.writeUnsignedInteger(buffer, body2.getSize());
115         body2.writePayload(buffer);
116         buffer.put(FRAME_END_BYTE);
117         buffer.put(body3.getFrameType());
118         EncodingUtils.writeUnsignedShort(buffer, channel);
119         EncodingUtils.writeUnsignedInteger(buffer, body3.getSize());
120         body3.writePayload(buffer);
121         buffer.put(FRAME_END_BYTE);
122 
123     }
124 
125 }