Skip to content

Commit bc6830e

Browse files
committed
reorganized MessagePacker interface to use MessagePacker
1 parent 2ce9916 commit bc6830e

File tree

14 files changed

+686
-469
lines changed

14 files changed

+686
-469
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package org.msgpack.core;
17+
18+
import org.msgpack.core.buffer.MessageBuffer;
19+
import org.msgpack.core.buffer.MessageBufferOutput;
20+
import org.msgpack.core.buffer.ArrayBufferOutput;
21+
22+
import java.io.IOException;
23+
import java.util.List;
24+
25+
public class MessageBufferPacker
26+
extends MessagePacker
27+
{
28+
public MessageBufferPacker()
29+
{
30+
this(new ArrayBufferOutput());
31+
}
32+
33+
public MessageBufferPacker(ArrayBufferOutput out)
34+
{
35+
super(out);
36+
}
37+
38+
@Override
39+
public MessageBufferPacker setSmallStringOptimizationThreshold(int bytes)
40+
{
41+
super.setSmallStringOptimizationThreshold(bytes);
42+
return this;
43+
}
44+
45+
public MessageBufferOutput reset(MessageBufferOutput out)
46+
throws IOException
47+
{
48+
if (!(out instanceof ArrayBufferOutput)) {
49+
throw new IllegalArgumentException("MessageBufferPacker accepts only ArrayBufferOutput");
50+
}
51+
return super.reset(out);
52+
}
53+
54+
public void clear()
55+
{
56+
((ArrayBufferOutput) out).clear();
57+
}
58+
59+
public byte[] toByteArray()
60+
{
61+
return ((ArrayBufferOutput) out).toByteArray();
62+
}
63+
64+
public MessageBuffer toMessageBuffer()
65+
{
66+
return ((ArrayBufferOutput) out).toMessageBuffer();
67+
}
68+
69+
public List<MessageBuffer> toBufferList()
70+
{
71+
return ((ArrayBufferOutput) out).toBufferList();
72+
}
73+
}

msgpack-core/src/main/java/org/msgpack/core/MessageFormat.java

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//
1616
package org.msgpack.core;
1717

18-
import org.msgpack.core.MessagePack.Code;
1918
import org.msgpack.core.annotations.VisibleForTesting;
2019
import org.msgpack.value.ValueType;
2120

@@ -66,6 +65,94 @@ public enum MessageFormat
6665
MAP32(ValueType.MAP),
6766
NEGFIXINT(ValueType.INTEGER);
6867

68+
/**
69+
* The prefix code set of MessagePack. See also https://github.com/msgpack/msgpack/blob/master/spec.md for details.
70+
*/
71+
public static final class Code
72+
{
73+
public static final boolean isFixInt(byte b)
74+
{
75+
int v = b & 0xFF;
76+
return v <= 0x7f || v >= 0xe0;
77+
}
78+
79+
public static final boolean isPosFixInt(byte b)
80+
{
81+
return (b & POSFIXINT_MASK) == 0;
82+
}
83+
84+
public static final boolean isNegFixInt(byte b)
85+
{
86+
return (b & NEGFIXINT_PREFIX) == NEGFIXINT_PREFIX;
87+
}
88+
89+
public static final boolean isFixStr(byte b)
90+
{
91+
return (b & (byte) 0xe0) == Code.FIXSTR_PREFIX;
92+
}
93+
94+
public static final boolean isFixedArray(byte b)
95+
{
96+
return (b & (byte) 0xf0) == Code.FIXARRAY_PREFIX;
97+
}
98+
99+
public static final boolean isFixedMap(byte b)
100+
{
101+
return (b & (byte) 0xe0) == Code.FIXMAP_PREFIX;
102+
}
103+
104+
public static final boolean isFixedRaw(byte b)
105+
{
106+
return (b & (byte) 0xe0) == Code.FIXSTR_PREFIX;
107+
}
108+
109+
public static final byte POSFIXINT_MASK = (byte) 0x80;
110+
111+
public static final byte FIXMAP_PREFIX = (byte) 0x80;
112+
public static final byte FIXARRAY_PREFIX = (byte) 0x90;
113+
public static final byte FIXSTR_PREFIX = (byte) 0xa0;
114+
115+
public static final byte NIL = (byte) 0xc0;
116+
public static final byte NEVER_USED = (byte) 0xc1;
117+
public static final byte FALSE = (byte) 0xc2;
118+
public static final byte TRUE = (byte) 0xc3;
119+
public static final byte BIN8 = (byte) 0xc4;
120+
public static final byte BIN16 = (byte) 0xc5;
121+
public static final byte BIN32 = (byte) 0xc6;
122+
public static final byte EXT8 = (byte) 0xc7;
123+
public static final byte EXT16 = (byte) 0xc8;
124+
public static final byte EXT32 = (byte) 0xc9;
125+
public static final byte FLOAT32 = (byte) 0xca;
126+
public static final byte FLOAT64 = (byte) 0xcb;
127+
public static final byte UINT8 = (byte) 0xcc;
128+
public static final byte UINT16 = (byte) 0xcd;
129+
public static final byte UINT32 = (byte) 0xce;
130+
public static final byte UINT64 = (byte) 0xcf;
131+
132+
public static final byte INT8 = (byte) 0xd0;
133+
public static final byte INT16 = (byte) 0xd1;
134+
public static final byte INT32 = (byte) 0xd2;
135+
public static final byte INT64 = (byte) 0xd3;
136+
137+
public static final byte FIXEXT1 = (byte) 0xd4;
138+
public static final byte FIXEXT2 = (byte) 0xd5;
139+
public static final byte FIXEXT4 = (byte) 0xd6;
140+
public static final byte FIXEXT8 = (byte) 0xd7;
141+
public static final byte FIXEXT16 = (byte) 0xd8;
142+
143+
public static final byte STR8 = (byte) 0xd9;
144+
public static final byte STR16 = (byte) 0xda;
145+
public static final byte STR32 = (byte) 0xdb;
146+
147+
public static final byte ARRAY16 = (byte) 0xdc;
148+
public static final byte ARRAY32 = (byte) 0xdd;
149+
150+
public static final byte MAP16 = (byte) 0xde;
151+
public static final byte MAP32 = (byte) 0xdf;
152+
153+
public static final byte NEGFIXINT_PREFIX = (byte) 0xe0;
154+
}
155+
69156
private static final MessageFormat[] formatTable = new MessageFormat[256];
70157
private final ValueType valueType;
71158

0 commit comments

Comments
 (0)