forked from isopov/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageBufferU.java
More file actions
261 lines (229 loc) · 5.75 KB
/
Copy pathMessageBufferU.java
File metadata and controls
261 lines (229 loc) · 5.75 KB
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
//
// MessagePack for Java
//
// Licensed 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.msgpack.core.buffer;
import java.nio.ByteBuffer;
import static org.msgpack.core.Preconditions.checkArgument;
/**
* Universal MessageBuffer implementation supporting Java6 and Android.
* This buffer always uses ByteBuffer-based memory access
*/
public class MessageBufferU
extends MessageBuffer
{
private final ByteBuffer wrap;
MessageBufferU(byte[] arr, int offset, int length)
{
super(arr, offset, length);
this.wrap = ByteBuffer.wrap(arr, offset, length).slice();
}
MessageBufferU(ByteBuffer bb)
{
super(bb);
this.wrap = bb.slice();
}
private MessageBufferU(Object base, long address, int length, ByteBuffer wrap)
{
super(base, address, length);
this.wrap = wrap;
}
@Override
public MessageBufferU slice(int offset, int length)
{
if (offset == 0 && length == size()) {
return this;
}
else {
checkArgument(offset + length <= size());
try {
wrap.position(offset);
wrap.limit(offset + length);
return new MessageBufferU(base, address + offset, length, wrap.slice());
}
finally {
resetBufferPosition();
}
}
}
private void resetBufferPosition()
{
wrap.position(0);
wrap.limit(size);
}
@Override
public byte getByte(int index)
{
return wrap.get(index);
}
@Override
public boolean getBoolean(int index)
{
return wrap.get(index) != 0;
}
@Override
public short getShort(int index)
{
return wrap.getShort(index);
}
@Override
public int getInt(int index)
{
return wrap.getInt(index);
}
@Override
public float getFloat(int index)
{
return wrap.getFloat(index);
}
@Override
public long getLong(int index)
{
return wrap.getLong(index);
}
@Override
public double getDouble(int index)
{
return wrap.getDouble(index);
}
@Override
public void getBytes(int index, int len, ByteBuffer dst)
{
try {
wrap.position(index);
wrap.limit(index + len);
dst.put(wrap);
}
finally {
resetBufferPosition();
}
}
@Override
public void putByte(int index, byte v)
{
wrap.put(index, v);
}
@Override
public void putBoolean(int index, boolean v)
{
wrap.put(index, v ? (byte) 1 : (byte) 0);
}
@Override
public void putShort(int index, short v)
{
wrap.putShort(index, v);
}
@Override
public void putInt(int index, int v)
{
wrap.putInt(index, v);
}
@Override
public void putFloat(int index, float v)
{
wrap.putFloat(index, v);
}
@Override
public void putLong(int index, long l)
{
wrap.putLong(index, l);
}
@Override
public void putDouble(int index, double v)
{
wrap.putDouble(index, v);
}
@Override
public ByteBuffer sliceAsByteBuffer(int index, int length)
{
try {
wrap.position(index);
wrap.limit(index + length);
return wrap.slice();
}
finally {
resetBufferPosition();
}
}
@Override
public ByteBuffer sliceAsByteBuffer()
{
return sliceAsByteBuffer(0, size);
}
@Override
public void getBytes(int index, byte[] dst, int dstOffset, int length)
{
try {
wrap.position(index);
wrap.get(dst, dstOffset, length);
}
finally {
resetBufferPosition();
}
}
@Override
public void putByteBuffer(int index, ByteBuffer src, int len)
{
assert (len <= src.remaining());
if (src.hasArray()) {
putBytes(index, src.array(), src.position() + src.arrayOffset(), len);
src.position(src.position() + len);
}
else {
int prevSrcLimit = src.limit();
try {
src.limit(src.position() + len);
wrap.position(index);
wrap.put(src);
}
finally {
src.limit(prevSrcLimit);
}
}
}
@Override
public void putBytes(int index, byte[] src, int srcOffset, int length)
{
try {
wrap.position(index);
wrap.put(src, srcOffset, length);
}
finally {
resetBufferPosition();
}
}
@Override
public void copyTo(int index, MessageBuffer dst, int offset, int length)
{
try {
wrap.position(index);
dst.putByteBuffer(offset, wrap, length);
}
finally {
resetBufferPosition();
}
}
@Override
public void putMessageBuffer(int index, MessageBuffer src, int srcOffset, int len)
{
putBytes(index, src.toByteArray(), srcOffset, len);
}
@Override
public byte[] toByteArray()
{
byte[] b = new byte[size()];
getBytes(0, b, 0, b.length);
return b;
}
}