Skip to content

Commit f119edd

Browse files
authored
Merge pull request #378 from msgpack/directbuf
Add ByteBuffer input support again
2 parents bdb8023 + 517e762 commit f119edd

File tree

13 files changed

+645
-188
lines changed

13 files changed

+645
-188
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.msgpack.core;
1717

1818
import org.msgpack.core.buffer.ArrayBufferInput;
19+
import org.msgpack.core.buffer.ByteBufferInput;
1920
import org.msgpack.core.buffer.ChannelBufferInput;
2021
import org.msgpack.core.buffer.ChannelBufferOutput;
2122
import org.msgpack.core.buffer.InputStreamBufferInput;
@@ -25,6 +26,7 @@
2526

2627
import java.io.InputStream;
2728
import java.io.OutputStream;
29+
import java.nio.ByteBuffer;
2830
import java.nio.channels.ReadableByteChannel;
2931
import java.nio.channels.WritableByteChannel;
3032
import java.nio.charset.Charset;
@@ -236,6 +238,17 @@ public static MessageUnpacker newDefaultUnpacker(byte[] contents, int offset, in
236238
return DEFAULT_UNPACKER_CONFIG.newUnpacker(contents, offset, length);
237239
}
238240

241+
/**
242+
* Create an unpacker that reads the data from a given ByteBuffer
243+
*
244+
* @param contents
245+
* @return
246+
*/
247+
public static MessageUnpacker newDefaultUnpacker(ByteBuffer contents)
248+
{
249+
return DEFAULT_UNPACKER_CONFIG.newUnpacker(contents);
250+
}
251+
239252
/**
240253
* MessagePacker configuration.
241254
*/
@@ -524,6 +537,17 @@ public MessageUnpacker newUnpacker(byte[] contents, int offset, int length)
524537
return newUnpacker(new ArrayBufferInput(contents, offset, length));
525538
}
526539

540+
/**
541+
* Create an unpacker that reads the data from a given ByteBuffer
542+
*
543+
* @param contents
544+
* @return
545+
*/
546+
public MessageUnpacker newUnpacker(ByteBuffer contents)
547+
{
548+
return newUnpacker(new ByteBufferInput(contents));
549+
}
550+
527551
/**
528552
* Allow unpackBinaryHeader to read str format family (default: true)
529553
*/

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,7 @@ else if (s.length() < (1 << 8)) {
532532
throw new IllegalArgumentException("Unexpected UTF-8 encoder state");
533533
}
534534
// move 1 byte backward to expand 3-byte header region to 3 bytes
535-
buffer.putBytes(position + 3,
536-
buffer.array(), buffer.arrayOffset() + position + 2, written);
535+
buffer.putMessageBuffer(position + 3, buffer, position + 2, written);
537536
// write 3-byte header
538537
buffer.putByte(position++, STR16);
539538
buffer.putShort(position, (short) written);
@@ -561,8 +560,7 @@ else if (s.length() < (1 << 16)) {
561560
throw new IllegalArgumentException("Unexpected UTF-8 encoder state");
562561
}
563562
// move 2 bytes backward to expand 3-byte header region to 5 bytes
564-
buffer.putBytes(position + 5,
565-
buffer.array(), buffer.arrayOffset() + position + 3, written);
563+
buffer.putMessageBuffer(position + 5, buffer, position + 3, written);
566564
// write 3-byte header header
567565
buffer.putByte(position++, STR32);
568566
buffer.putInt(position, written);

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,9 @@ private MessageBuffer prepareNumberBuffer(int readLength)
213213
// fill the temporary buffer from the current data fragment and
214214
// next fragment(s).
215215

216-
// TODO buffer.array() doesn't work if MessageBuffer is allocated by
217-
// newDirectBuffer. dd copy method to MessageBuffer to solve this issue.
218-
219216
int off = 0;
220217
if (remaining > 0) {
221-
numberBuffer.putBytes(0,
222-
buffer.array(), buffer.arrayOffset() + position,
223-
remaining);
218+
numberBuffer.putMessageBuffer(0, buffer, position, remaining);
224219
readLength -= remaining;
225220
off += remaining;
226221
}
@@ -229,16 +224,12 @@ private MessageBuffer prepareNumberBuffer(int readLength)
229224
nextBuffer();
230225
int nextSize = buffer.size();
231226
if (nextSize >= readLength) {
232-
numberBuffer.putBytes(off,
233-
buffer.array(), buffer.arrayOffset(),
234-
readLength);
227+
numberBuffer.putMessageBuffer(off, buffer, 0, readLength);
235228
position = readLength;
236229
break;
237230
}
238231
else {
239-
numberBuffer.putBytes(off,
240-
buffer.array(), buffer.arrayOffset(),
241-
nextSize);
232+
numberBuffer.putMessageBuffer(off, buffer, 0, nextSize);
242233
readLength -= nextSize;
243234
off += nextSize;
244235
}
@@ -1041,7 +1032,8 @@ private void handleCoderError(CoderResult cr)
10411032
private String decodeStringFastPath(int length)
10421033
{
10431034
if (actionOnMalformedString == CodingErrorAction.REPLACE &&
1044-
actionOnUnmappableString == CodingErrorAction.REPLACE) {
1035+
actionOnUnmappableString == CodingErrorAction.REPLACE &&
1036+
buffer.hasArray()) {
10451037
String s = new String(buffer.array(), buffer.arrayOffset() + position, length, MessagePack.UTF8);
10461038
position += length;
10471039
return s;

msgpack-core/src/main/java/org/msgpack/core/buffer/ArrayBufferInput.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
//
1616
package org.msgpack.core.buffer;
1717

18-
import java.io.IOException;
19-
2018
import static org.msgpack.core.Preconditions.checkNotNull;
2119

2220
/**
@@ -80,7 +78,6 @@ public void reset(byte[] arr, int offset, int len)
8078

8179
@Override
8280
public MessageBuffer next()
83-
throws IOException
8481
{
8582
if (isEmpty) {
8683
return null;
@@ -91,7 +88,6 @@ public MessageBuffer next()
9188

9289
@Override
9390
public void close()
94-
throws IOException
9591
{
9692
buffer = null;
9793
isEmpty = true;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.buffer;
17+
18+
import java.nio.ByteBuffer;
19+
20+
import static org.msgpack.core.Preconditions.checkNotNull;
21+
22+
/**
23+
* {@link MessageBufferInput} adapter for {@link java.nio.ByteBuffer}
24+
*/
25+
public class ByteBufferInput
26+
implements MessageBufferInput
27+
{
28+
private ByteBuffer input;
29+
private boolean isRead = false;
30+
31+
public ByteBufferInput(ByteBuffer input)
32+
{
33+
this.input = checkNotNull(input, "input ByteBuffer is null").slice();
34+
}
35+
36+
/**
37+
* Reset buffer.
38+
*
39+
* @param input new buffer
40+
* @return the old buffer
41+
*/
42+
public ByteBuffer reset(ByteBuffer input)
43+
{
44+
ByteBuffer old = this.input;
45+
this.input = checkNotNull(input, "input ByteBuffer is null").slice();
46+
isRead = false;
47+
return old;
48+
}
49+
50+
@Override
51+
public MessageBuffer next()
52+
{
53+
if (isRead) {
54+
return null;
55+
}
56+
57+
MessageBuffer b = MessageBuffer.wrap(input);
58+
isRead = true;
59+
return b;
60+
}
61+
62+
@Override
63+
public void close()
64+
{
65+
// Nothing to do
66+
}
67+
}

0 commit comments

Comments
 (0)