Skip to content

Commit 420f8e9

Browse files
committed
Use simple naming for classes
1 parent c430ce3 commit 420f8e9

37 files changed

+447
-1151
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/**
1616
* MessageBuffer class is an abstraction of memory for reading/writing message packed data.
17-
* This MessageBuffers ensures integers (31-bit singed) are written in the big-endian order.
17+
* This MessageBuffers ensures short/int/float/long/double values are written in the big-endian order.
1818
*
1919
* This class is optimized for fast memory access, so many methods are
2020
* implemented without using any interface method that produces invokeinterface call in JVM.
@@ -24,7 +24,7 @@
2424
public class MessageBuffer {
2525

2626
static final Unsafe unsafe;
27-
static final Constructor byteBufferCC;
27+
static final Constructor byteBufferConstructor;
2828

2929
static {
3030
try {
@@ -44,8 +44,8 @@ public class MessageBuffer {
4444

4545
// Find the hidden constructor for DirectByteBuffer
4646
Class<?> directByteBufferClass = ClassLoader.getSystemClassLoader().loadClass("java.nio.DirectByteBuffer");
47-
byteBufferCC = directByteBufferClass.getDeclaredConstructor(long.class, int.class, Object.class);
48-
byteBufferCC.setAccessible(true);
47+
byteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class, Object.class);
48+
byteBufferConstructor.setAccessible(true);
4949

5050
// Check the endian of this CPU
5151
boolean isLittleEndian = true;
@@ -375,12 +375,18 @@ public void putByteBuffer(int index, ByteBuffer src, int len) {
375375
}
376376

377377

378+
/**
379+
* Create a ByteBuffer view of the range [index, index+length) of this memory
380+
* @param index
381+
* @param length
382+
* @return
383+
*/
378384
public ByteBuffer toByteBuffer(int index, int length) {
379385
if(base instanceof byte[]) {
380386
return ByteBuffer.wrap((byte[]) base, (int) ((address-ARRAY_BYTE_BASE_OFFSET) + index), length);
381387
}
382388
try {
383-
return (ByteBuffer) byteBufferCC.newInstance(address + index, length, reference);
389+
return (ByteBuffer) byteBufferConstructor.newInstance(address + index, length, reference);
384390
} catch(Throwable e) {
385391
// Convert checked exception to unchecked exception
386392
throw new RuntimeException(e);
@@ -391,6 +397,13 @@ public void relocate(int offset, int length, int dst) {
391397
unsafe.copyMemory(base, address + offset, base, address+dst, length);
392398
}
393399

400+
/**
401+
* Copy this buffer contents to another MessageBuffer
402+
* @param index
403+
* @param dst
404+
* @param offset
405+
* @param length
406+
*/
394407
public void copyTo(int index, MessageBuffer dst, int offset, int length) {
395408
unsafe.copyMemory(base, address + index, dst.base, dst.address + offset, length);
396409
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import java.nio.ByteBuffer;
44

55
/**
6-
* MessageBuffer class tailored to the big-endian machine. Message pack specification demands writing short/int/float/long/double values in big-endian format.
6+
* MessageBufferBE is a {@link MessageBuffer} implementation tailored to big-endian machines.
7+
* The specification of Message Pack demands writing short/int/float/long/double values in the big-endian format.
78
* In the big-endian machine, we do not need to swap the byte order.
89
*/
910
public class MessageBufferBE extends MessageBuffer {

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

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

1818
/**
19-
* When the message pack format is invalid
19+
* Thrown when the input message pack format is invalid
2020
*/
21-
public class MessageFormatException extends MessageException {
21+
public class MessageFormatException extends MessagePackException {
2222
public MessageFormatException(String message) {
2323
super(message);
2424
}

msgpack-core/src/main/java/org/msgpack/core/IntegerOverflowException.java renamed to msgpack-core/src/main/java/org/msgpack/core/MessageIntegerOverflowException.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@
1717

1818
import java.math.BigInteger;
1919

20-
public class IntegerOverflowException extends MessageTypeException {
20+
21+
/**
22+
* This error is thrown when the user tries to read an integer value
23+
* using a smaller types. For example, calling MessageUnpacker.unpackInt() for an integer value
24+
* that is larger than Integer.MAX_VALUE will cause this exception.
25+
*/
26+
public class MessageIntegerOverflowException extends MessageTypeException {
2127
private final BigInteger bigInteger;
2228

23-
public IntegerOverflowException(BigInteger bigInteger) {
29+
public MessageIntegerOverflowException(BigInteger bigInteger) {
2430
super();
2531
this.bigInteger = bigInteger;
2632
}
2733

28-
public IntegerOverflowException(String message, BigInteger bigInteger) {
34+
public MessageIntegerOverflowException(String message, BigInteger bigInteger) {
2935
super(message);
3036
this.bigInteger = bigInteger;
3137
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
import java.nio.file.Path;
1010

1111
/**
12-
* Entry point for creating MessagePacker and MessageUnpacker
12+
* Entry point for creating MessagePacker (newPacker) and MessageUnpacker (newUnpacker).
13+
*
1314
*/
1415
public class MessagePack {
1516

1617
public static Charset UTF8 = Charset.forName("UTF-8");
1718

1819
/**
19-
* The code set of MessagePack. See also https://github.com/msgpack/msgpack/blob/master/spec.md for details.
20+
* The prefix code set of MessagePack. See also https://github.com/msgpack/msgpack/blob/master/spec.md for details.
2021
*/
21-
public static class Code {
22+
public static final class Code {
2223

2324
public static final boolean isFixInt(byte b) {
2425
return isPosFixInt(b) || isNegFixInt(b);
@@ -92,6 +93,9 @@ public static final boolean isFixedRaw(byte b) {
9293
}
9394

9495

96+
/**
97+
* Header of extended type
98+
*/
9599
public static class ExtendedTypeHeader {
96100
private final int type;
97101
private final int length;
@@ -132,6 +136,11 @@ public static MessagePacker newPacker(WritableByteChannel out) {
132136
return new MessagePacker(new MessageBufferOutputChannel(out));
133137
}
134138

139+
/**
140+
* Create a new MessageUnpacker that reads the data from a given array
141+
* @param arr
142+
* @return
143+
*/
135144
public static MessageUnpacker newUnpacker(byte[] arr) {
136145
return new MessageUnpacker(new ArrayMessageBufferInput(arr));
137146
}
@@ -149,6 +158,7 @@ public static MessageUnpacker newUnpacker(File inputFile) throws IOException {
149158

150159
public static MessageUnpacker newUnpacker(InputStream in) {
151160
if(in instanceof FileInputStream) {
161+
// When the input is FileInputStream, we can use file channel that can directly read the data from a file to an off-heap buffer
152162
return newUnpacker(((FileInputStream) in).getChannel());
153163
}
154164
else {

msgpack-core/src/main/java/org/msgpack/core/MessageException.java renamed to msgpack-core/src/main/java/org/msgpack/core/MessagePackException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717

1818

1919
/**
20-
* A base class of the exceptions used in msgpack
20+
* A base class of all of the message pack exceptions.
2121
*/
22-
public class MessageException extends RuntimeException {
23-
public MessageException() {
22+
public class MessagePackException extends RuntimeException {
23+
public MessagePackException() {
2424
super();
2525
}
2626

27-
public MessageException(String message) {
27+
public MessagePackException(String message) {
2828
super(message);
2929
}
3030

31-
public MessageException(String message, Throwable cause) {
31+
public MessagePackException(String message, Throwable cause) {
3232
super(message, cause);
3333
}
3434

35-
public MessageException(Throwable cause) {
35+
public MessagePackException(Throwable cause) {
3636
super(cause);
3737
}
3838
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@
2121
import static org.msgpack.core.MessagePack.Code.*;
2222

2323
/**
24+
* Writer of message packed data.
25+
*
26+
* <p>
27+
* MessagePacker provides packXXX methods for writing values in the message pack format.
28+
* To write raw string or binary data, first use packRawStringHeader or packBinaryHeader to specify the data length,
29+
* then call writePayload(...) method.
30+
* </p>
31+
*
32+
* <p>
33+
* MessagePacker class has no guarantee to produce the correct message-pack format data if it is not used correctly:
34+
* packXXX methods of primitive values always produce the correct format, but
35+
* packXXXHeader (e.g. array, map, ext) must be followed by correct number of array/map/ext type values.
36+
* packRawStringHeader(length) and packBinaryHeader(length) must be followed by writePayload( ... length) to supply
37+
* the binary data of the specified length in the header.
38+
* </p>
2439
*
2540
*/
2641
public class MessagePacker {

msgpack-core/src/main/java/org/msgpack/core/MessageSizeLimitException.java renamed to msgpack-core/src/main/java/org/msgpack/core/MessageSizeException.java

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

18-
public class MessageSizeLimitException extends MessageException {
18+
/**
19+
* Thrown to indicate too large message size (e.g, larger than 2^31-1).
20+
*/
21+
public class MessageSizeException extends MessagePackException {
1922
private final long size;
2023

21-
public MessageSizeLimitException(long size) {
24+
public MessageSizeException(long size) {
2225
super();
2326
this.size = size;
2427
}
2528

26-
public MessageSizeLimitException(String message, long size) {
29+
public MessageSizeException(String message, long size) {
2730
super(message);
2831
this.size = size;
2932
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import java.nio.charset.CharacterCodingException;
1919

2020
/**
21-
*
21+
* Thrown to indicate an error when encoding/decoding a String value
2222
*/
23-
public class MessageStringCodingException extends MessageException {
23+
public class MessageStringCodingException extends MessagePackException {
2424
public MessageStringCodingException(String message, CharacterCodingException cause) {
2525
super(message, cause);
2626
}

0 commit comments

Comments
 (0)