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
|
/*
*
* 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.
*
*/
using System;
using System.Text;
using Qpid.Buffer;
namespace Qpid.Framing
{
public class EncodingUtils
{
private static readonly Encoding DEFAULT_ENCODER = Encoding.ASCII;
public static ushort EncodedShortStringLength(string s)
{
if (s == null)
{
return 1;
}
else
{
return (ushort)(1 + s.Length);
}
}
public static uint EncodedLongStringLength(string s)
{
if (s == null)
{
return 4;
}
else
{
return (uint)(4 + s.Length);
}
}
public static int EncodedLongstrLength(byte[] bytes)
{
if (bytes == null)
{
return 4;
}
else
{
return 4 + bytes.Length;
}
}
public static uint EncodedFieldTableLength(FieldTable table)
{
if (table == null)
{
// size is encoded as 4 octets
return 4;
}
else
{
// size of the table plus 4 octets for the size
return table.EncodedSize + 4;
}
}
public static void WriteShortStringBytes(ByteBuffer buffer, string s)
{
if (s != null)
{
//try
//{
//final byte[] encodedString = s.getBytes(STRING_ENCODING);
byte[] encodedString;
lock (DEFAULT_ENCODER)
{
encodedString = DEFAULT_ENCODER.GetBytes(s);
}
// TODO: check length fits in an unsigned byte
buffer.put((byte) encodedString.Length);
buffer.put(encodedString);
}
else
{
// really writing out unsigned byte
buffer.put((byte) 0);
}
}
public static void WriteLongStringBytes(ByteBuffer buffer, string s)
{
if (!(s == null || s.Length <= 0xFFFE))
{
throw new ArgumentException("String too long");
}
if (s != null)
{
buffer.put((uint)s.Length);
byte[] encodedString = null;
lock (DEFAULT_ENCODER)
{
encodedString = DEFAULT_ENCODER.GetBytes(s);
}
buffer.put(encodedString);
}
else
{
buffer.put((uint) 0);
}
}
public static void WriteFieldTableBytes(ByteBuffer buffer, FieldTable table)
{
if (table != null)
{
table.WriteToBuffer(buffer);
}
else
{
buffer.put((uint) 0);
}
}
public static void WriteBooleans(ByteBuffer buffer, bool[] values)
{
byte packedValue = 0;
for (int i = 0; i < values.Length; i++)
{
if (values[i])
{
packedValue = (byte) (packedValue | (1 << i));
}
}
buffer.put(packedValue);
}
public static void WriteLongstr(ByteBuffer buffer, byte[] data)
{
if (data != null)
{
buffer.put((uint) data.Length);
buffer.put(data);
}
else
{
buffer.put((uint) 0);
}
}
public static bool[] ReadBooleans(ByteBuffer buffer)
{
byte packedValue = buffer.get();
bool[] result = new bool[8];
for (int i = 0; i < 8; i++)
{
result[i] = ((packedValue & (1 << i)) != 0);
}
return result;
}
/// <summary>
/// Reads the field table uaing the data in the specified buffer
/// </summary>
/// <param name="buffer">The buffer to read from.</param>
/// <returns>a populated field table</returns>
/// <exception cref="AMQFrameDecodingException">if the buffer does not contain a decodable field table</exception>
public static FieldTable ReadFieldTable(ByteBuffer buffer)
{
uint length = buffer.GetUnsignedInt();
if (length == 0)
{
return null;
}
else
{
return new FieldTable(buffer, length);
}
}
/// <summary>
/// Read a short string from the buffer
/// </summary>
/// <param name="buffer">The buffer to read from.</param>
/// <returns>a string</returns>
/// <exception cref="AMQFrameDecodingException">if the buffer does not contain a decodable short string</exception>
public static string ReadShortString(ByteBuffer buffer)
{
byte length = buffer.get();
if (length == 0)
{
return null;
}
else
{
byte[] data = new byte[length];
buffer.get(data);
lock (DEFAULT_ENCODER)
{
return DEFAULT_ENCODER.GetString(data);
// return buffer.GetString(length, DEFAULT_ENCODER);
}
}
}
public static string ReadLongString(ByteBuffer buffer)
{
uint length = buffer.getUnsignedInt();
if (length == 0)
{
return null;
}
else
{
byte[] data = new byte[length];
buffer.get(data);
lock (DEFAULT_ENCODER)
{
return DEFAULT_ENCODER.GetString(data);
//return buffer.GetString(length, DEFAULT_ENCODER);
}
}
}
public static byte[] ReadLongstr(ByteBuffer buffer)
{
uint length = buffer.getUnsignedInt();
if (length == 0)
{
return null;
}
else
{
byte[] result = new byte[length];
buffer.get(result);
return result;
}
}
}
}
|