AMQDataBlock.java
01 /*
02  *
03  * Licensed to the Apache Software Foundation (ASF) under one
04  * or more contributor license agreements.  See the NOTICE file
05  * distributed with this work for additional information
06  * regarding copyright ownership.  The ASF licenses this file
07  * to you under the Apache License, Version 2.0 (the
08  * "License"); you may not use this file except in compliance
09  * with the License.  You may obtain a copy of the License at
10  
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  */
21 package org.apache.qpid.framing;
22 
23 import org.apache.mina.common.ByteBuffer;
24 
25 /**
26  * A data block represents something that has a size in bytes and the ability to write itself to a byte
27  * buffer (similar to a byte array).
28  */
29 public abstract class AMQDataBlock implements EncodableAMQDataBlock
30 {
31     /**
32      * Get the size of buffer needed to store the byte representation of this
33      * frame.
34      @return unsigned integer
35      */
36     public abstract long getSize();
37 
38     /**
39      * Writes the datablock to the specified buffer.
40      @param buffer
41      */
42     public abstract void writePayload(ByteBuffer buffer);
43 
44     public ByteBuffer toByteBuffer()
45     {
46         final ByteBuffer buffer = ByteBuffer.allocate((int)getSize());
47 
48         writePayload(buffer);    
49         buffer.flip();
50         return buffer;
51     }
52 
53     public java.nio.ByteBuffer toNioByteBuffer()
54     {
55         final java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate((intgetSize());
56 
57         ByteBuffer buf = ByteBuffer.wrap(buffer);
58         writePayload(buf);    
59         buffer.flip();
60         return buffer;
61     }
62 
63 }