summaryrefslogtreecommitdiff
path: root/M4-RCs/qpid/java/management/client/src/main/java/org/apache/qpid/management/messages/AmqpCoDec.java
blob: 4d1dfe796af54e1be54e82b23857056cf59328b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package org.apache.qpid.management.messages;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

/**
 * AMQP Management messages codec.
 * 
 * @author Andrea Gazzarini
 */
public class AmqpCoDec
{
    private byte [] _buffer;
    private int _position;
        
    /**
     * Builds a new codec.
     */
    AmqpCoDec()
    {
        _buffer = new byte [1000];
        _buffer[0] = 'A';
        _buffer[1] = 'M';
        _buffer[2] = '2';
        _position = 3;
    }
    
    
    /**
     * Int32-to-4 byte array marshalling.
     * Marshalles an integer using four bytes.
     * 
     * @param data the result array.
     * @param pos the starting position of the array to be filled.
     * @param value the value to be marshalled.
     */
    public final void pack32(int value) {
        _buffer[_position++] = (byte) (value >> 24 & 0xff);
        _buffer[_position++] = (byte) (value >> 16 & 0xff);
        _buffer[_position++] = (byte) (value >> 8 & 0xff);
        _buffer[_position++] = (byte) (value & 0xff);
    }

    /**
     * Int32-to-4 byte array marshalling.
     * Marshalles an integer using four bytes.
     * 
     * @param data the result array.
     * @param pos the starting position of the array to be filled.
     * @param value the value to be marshalled.
     */
    public final void pack16(int value) {
        _buffer[_position++] = (byte) (value >> 8 & 0xff);
        _buffer[_position++] = (byte) (value & 0xff);
    }
    
    /**
     * Int32-to-4 byte array marshalling.
     * Marshalles an integer using four bytes.
     * 
     * @param data the result array.
     * @param pos the starting position of the array to be filled.
     * @param value the value to be marshalled.
     */
    public final void pack64(long value) {
        _buffer[_position++] = (byte) (value >> 56 & 0xff);
        _buffer[_position++] = (byte) (value >> 48 & 0xff);
        _buffer[_position++] = (byte) (value >> 40 & 0xff);
        _buffer[_position++] = (byte) (value >> 32 & 0xff);
        _buffer[_position++] = (byte) (value >> 24 & 0xff);
        _buffer[_position++] = (byte) (value >> 16 & 0xff);
        _buffer[_position++] = (byte) (value >> 8 & 0xff);
        _buffer[_position++] = (byte) (value & 0xff);
    }
    
    /**
     * Int32-to-byte array marshalling.
     * Marshalles an integer using two bytes.
     * 
     * @param data the result array.
     * @param pos the starting position of the array to be filled.
     * @param value the value to be marshalled.
     */
    public final void pack24(int value) {
        _buffer[_position++] = (byte) (value >> 16 & 0xff);
        _buffer[_position++] = (byte) (value >> 8 & 0xff);
        _buffer[_position++] = (byte) (value & 0xff);
    }    

    public final void pack8(int value) {
        _buffer[_position++] = (byte) (value & 0xff);
    }    

    public void pack8 (byte aByte)
    {
        _buffer[_position++] = aByte;
    }    

    public void packStr8(String aString)
    {
        try
        {
            byte [] toBytes = aString.getBytes("UTF-8");
            int length = toBytes.length;
            pack8(length);
            System.arraycopy(toBytes, 0, _buffer, _position, length);  
            _position+=length;
        } catch (UnsupportedEncodingException exception)
        {
            throw new RuntimeException(exception);
        }
    }
    
    public void packStr16(String aString)
    {
        try
        {
            byte [] toBytes = aString.getBytes("UTF-8");
            int length = toBytes.length;
            pack16(length);
            System.arraycopy(toBytes, 0, _buffer, _position, length);  
            _position+=length;
        } catch (UnsupportedEncodingException exception)
        {
            throw new RuntimeException(exception);
        }
    }

	public static final long unpack64(byte data[]) {
		return (
				((long) (data[0] & 0xff) << 56) | 
				((long)(data[1] & 0xff) << 48) | 
				((long)(data[2] & 0xff) << 40) | 
				((long)(data[3] & 0xff) << 32) | 
				((long)(data[4] & 0xff) << 24) | 
				((long)(data[5] & 0xff) << 16) | 
				((long)(data[6] & 0xff) << 8) | 
				(long) data[7] & 0xff);		
	}

    
    public void pack (byte[] bytes)
    {
        System.arraycopy(bytes, 0, _buffer, _position, bytes.length);
        _position+=bytes.length;
    }
    
    /**
     * Retruns the byte buffer that is wrapping the backing array of this codec.
     * 
     * @return the byte buffer that is wrapping the backing array of this codec.
     */
    public ByteBuffer getEncodedBuffer ()
    {
        return ByteBuffer.wrap(_buffer,0,_position);
    }
}