forked from rabbitmq/rabbitmq-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueReader.java
280 lines (252 loc) · 7.88 KB
/
ValueReader.java
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
package com.rabbitmq.client.impl;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import com.rabbitmq.client.LongString;
import com.rabbitmq.client.MalformedFrameException;
/**
* Helper class to read AMQP wire-protocol encoded values.
*/
public class ValueReader
{
private static final long INT_MASK = 0xffffffffL;
/**
* Protected API - Cast an int to a long without extending the
* sign bit of the int out into the high half of the long.
*/
private static long unsignedExtend(int value)
{
long extended = value;
return extended & INT_MASK;
}
/** The stream we are reading from. */
private final DataInputStream in;
/**
* Construct a MethodArgumentReader streaming over the given DataInputStream.
*/
public ValueReader(DataInputStream in)
{
this.in = in;
}
/** Convenience method - reads a short string from a DataInput
* Stream.
*/
private static String readShortstr(DataInputStream in)
throws IOException
{
byte [] b = new byte[in.readUnsignedByte()];
in.readFully(b);
return new String(b, "utf-8");
}
/** Public API - reads a short string. */
public final String readShortstr()
throws IOException
{
return readShortstr(this.in);
}
/** Convenience method - reads a 32-bit-length-prefix
* byte vector from a DataInputStream.
*/
private static byte[] readBytes(final DataInputStream in)
throws IOException
{
final long contentLength = unsignedExtend(in.readInt());
if(contentLength < Integer.MAX_VALUE) {
final byte [] buffer = new byte[(int)contentLength];
in.readFully(buffer);
return buffer;
} else {
throw new UnsupportedOperationException
("Very long byte vectors and strings not currently supported");
}
}
/** Convenience method - reads a long string argument
* from a DataInputStream.
*/
private static LongString readLongstr(final DataInputStream in)
throws IOException
{
return LongStringHelper.asLongString(readBytes(in));
}
/** Public API - reads a long string. */
public final LongString readLongstr()
throws IOException
{
return readLongstr(this.in);
}
/** Public API - reads a short integer. */
public final int readShort()
throws IOException
{
return in.readUnsignedShort();
}
/** Public API - reads an integer. */
public final int readLong()
throws IOException
{
return in.readInt();
}
/** Public API - reads a long integer. */
public final long readLonglong()
throws IOException
{
return in.readLong();
}
/**
* Reads a table argument from a given stream. Also
* called by {@link ContentHeaderPropertyReader}.
*/
private static Map<String, Object> readTable(DataInputStream in)
throws IOException
{
long tableLength = unsignedExtend(in.readInt());
if (tableLength == 0) return Collections.emptyMap();
Map<String, Object> table = new HashMap<String, Object>();
DataInputStream tableIn = new DataInputStream
(new TruncatedInputStream(in, tableLength));
while(tableIn.available() > 0) {
String name = readShortstr(tableIn);
Object value = readFieldValue(tableIn);
if(!table.containsKey(name))
table.put(name, value);
}
return table;
}
// package protected for testing
static Object readFieldValue(DataInputStream in)
throws IOException {
Object value = null;
switch(in.readUnsignedByte()) {
case 'S':
value = readLongstr(in);
break;
case 'I':
value = in.readInt();
break;
case 'i':
value = readUnsignedInt(in);
break;
case 'D':
int scale = in.readUnsignedByte();
byte [] unscaled = new byte[4];
in.readFully(unscaled);
value = new BigDecimal(new BigInteger(unscaled), scale);
break;
case 'T':
value = readTimestamp(in);
break;
case 'F':
value = readTable(in);
break;
case 'A':
value = readArray(in);
break;
case 'b':
value = in.readByte();
break;
case 'B':
value = in.readUnsignedByte();
break;
case 'd':
value = in.readDouble();
break;
case 'f':
value = in.readFloat();
break;
case 'l':
value = in.readLong();
break;
case 's':
value = in.readShort();
break;
case 'u':
value = in.readUnsignedShort();
break;
case 't':
value = in.readBoolean();
break;
case 'x':
value = readBytes(in);
break;
case 'V':
value = null;
break;
default:
throw new MalformedFrameException
("Unrecognised type in table");
}
return value;
}
/** Read an unsigned int */
private static long readUnsignedInt(DataInputStream in)
throws IOException
{
long ch1 = in.read();
long ch2 = in.read();
long ch3 = in.read();
long ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4);
}
/** Read a field-array */
private static List<Object> readArray(DataInputStream in)
throws IOException
{
long length = unsignedExtend(in.readInt());
DataInputStream arrayIn = new DataInputStream
(new TruncatedInputStream(in, length));
List<Object> array = new ArrayList<Object>();
while(arrayIn.available() > 0) {
Object value = readFieldValue(arrayIn);
array.add(value);
}
return array;
}
/** Public API - reads a table. */
public final Map<String, Object> readTable()
throws IOException
{
return readTable(this.in);
}
/** Public API - reads an octet. */
public final int readOctet()
throws IOException
{
return in.readUnsignedByte();
}
/** Convenience method - reads a timestamp argument from the DataInputStream. */
private static Date readTimestamp(DataInputStream in)
throws IOException
{
return new Date(in.readLong()*1000);
}
/** Public API - reads an timestamp. */
public final Date readTimestamp()
throws IOException
{
return readTimestamp(this.in);
}
}