| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,7 +23,11 @@ | |||
| 23 | 23 | import java.util.List; | |
| 24 | 24 | ||
| 25 | 25 | /** | |
| 26 | - * MessagePacker that is useful to produce byte array output | ||
| 26 | + * MessagePacker that is useful to produce byte array output. | ||
| 27 | + * <p> | ||
| 28 | + * This class allocates a new buffer instead of resizing the buffer when data doesn't fit in the initial capacity. | ||
| 29 | + * This is faster than ByteArrayOutputStream especially when size of written bytes is large because resizing a buffer | ||
| 30 | + * usually needs to copy contents of the buffer. | ||
| 27 | 31 | */ | |
| 28 | 32 | public class MessageBufferPacker | |
| 29 | 33 | extends MessagePacker | |
@@ -52,11 +56,22 @@ private ArrayBufferOutput getArrayBufferOut() | |||
| 52 | 56 | return (ArrayBufferOutput) out; | |
| 53 | 57 | } | |
| 54 | 58 | ||
| 59 | + /** | ||
| 60 | + * Clears the written data. | ||
| 61 | + */ | ||
| 55 | 62 | public void clear() | |
| 56 | 63 | { | |
| 57 | 64 | getArrayBufferOut().clear(); | |
| 58 | 65 | } | |
| 59 | 66 | ||
| 67 | + /** | ||
| 68 | + * Gets copy of the written data as a byte array. | ||
| 69 | + * <p> | ||
| 70 | + * If your application needs better performance and smaller memory consumption, you may prefer | ||
| 71 | + * {@link #toMessageBuffer()} or {@link #toBufferList()} to avoid copying. | ||
| 72 | + * | ||
| 73 | + * @return the byte array | ||
| 74 | + */ | ||
| 60 | 75 | public byte[] toByteArray() | |
| 61 | 76 | { | |
| 62 | 77 | try { | |
@@ -69,6 +84,14 @@ public byte[] toByteArray() | |||
| 69 | 84 | return getArrayBufferOut().toByteArray(); | |
| 70 | 85 | } | |
| 71 | 86 | ||
| 87 | + /** | ||
| 88 | + * Gets the written data as a MessageBuffer. | ||
| 89 | + * <p> | ||
| 90 | + * Unlike {@link #toByteArray()}, this method omits copy of the contents if size of the written data is smaller | ||
| 91 | + * than a single buffer capacity. | ||
| 92 | + * | ||
| 93 | + * @return the MessageBuffer instance | ||
| 94 | + */ | ||
| 72 | 95 | public MessageBuffer toMessageBuffer() | |
| 73 | 96 | { | |
| 74 | 97 | try { | |
@@ -81,6 +104,14 @@ public MessageBuffer toMessageBuffer() | |||
| 81 | 104 | return getArrayBufferOut().toMessageBuffer(); | |
| 82 | 105 | } | |
| 83 | 106 | ||
| 107 | + /** | ||
| 108 | + * Returns the written data as a list of MessageBuffer. | ||
| 109 | + * <p> | ||
| 110 | + * Unlike {@link #toByteArray()} or {@link #toMessageBuffer()}, this is the fastest method that doesn't | ||
| 111 | + * copy contents in any cases. | ||
| 112 | + * | ||
| 113 | + * @return the list of MessageBuffer instances | ||
| 114 | + */ | ||
| 84 | 115 | public List<MessageBuffer> toBufferList() | |
| 85 | 116 | { | |
| 86 | 117 | try { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,7 +35,7 @@ | |||
| 35 | 35 | /** | |
| 36 | 36 | * Convenience class to build packer and unpacker classes. | |
| 37 | 37 | * | |
| 38 | - * You may choose factory method as following | ||
| 38 | + * You can select an appropriate factory method as following. | ||
| 39 | 39 | * | |
| 40 | 40 | * <p> | |
| 41 | 41 | * Deserializing objects from binary: | |
@@ -70,12 +70,12 @@ public class MessagePack | |||
| 70 | 70 | public static final Charset UTF8 = Charset.forName("UTF-8"); | |
| 71 | 71 | ||
| 72 | 72 | /** | |
| 73 | - * Configuration of a {@link MessagePacker} created by {@link #newDefaultPacker(MessageBufferOutput)} and {@link #newDefaultBufferPacker()} methods. | ||
| 73 | + * Configuration of a {@link MessagePacker} used by {@link #newDefaultPacker(MessageBufferOutput)} and {@link #newDefaultBufferPacker()} methods. | ||
| 74 | 74 | */ | |
| 75 | 75 | public static final PackerConfig DEFAULT_PACKER_CONFIG = new PackerConfig(); | |
| 76 | 76 | ||
| 77 | 77 | /** | |
| 78 | - * Configuration of a {@link MessageUnpacker} created by {@link #newDefaultUnpacker(MessageBufferInput)} methods. | ||
| 78 | + * Configuration of a {@link MessageUnpacker} used by {@link #newDefaultUnpacker(MessageBufferInput)} methods. | ||
| 79 | 79 | */ | |
| 80 | 80 | public static final UnpackerConfig DEFAULT_UNPACKER_CONFIG = new UnpackerConfig(); | |
| 81 | 81 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -72,7 +72,7 @@ | |||
| 72 | 72 | * an instance. | |
| 73 | 73 | * <p> | |
| 74 | 74 | * This class provides following primitive methods to write MessagePack values. These primitive methods write | |
| 75 | - * short bytes (1 to 7 bytes) to the internal buffer at once. There also some complex methods for convenience. | ||
| 75 | + * short bytes (1 to 7 bytes) to the internal buffer at once. There are also some complex methods for convenience. | ||
| 76 | 76 | * <p> | |
| 77 | 77 | * Primitive methods: | |
| 78 | 78 | * | |
@@ -110,13 +110,13 @@ | |||
| 110 | 110 | * | |
| 111 | 111 | * <p> | |
| 112 | 112 | * To write a List, Collection or array, first you call {@link #packArrayHeader(int)} method with the number of | |
| 113 | - * elements. Then, you call packer methods for each element. You don't have to call anything at the end of | ||
| 113 | + * elements. Then, you call packer methods for each element. | ||
| 114 | 114 | * iteration. | |
| 115 | 115 | * | |
| 116 | 116 | * <p> | |
| 117 | 117 | * To write a Map, first you call {@link #packMapHeader(int)} method with size of the map. Then, for each pair, | |
| 118 | 118 | * you call packer methods for key first, and then value. You will call packer methods twice as many time as the | |
| 119 | - * size of the map. You don't have to call anything at the end of iteration. | ||
| 119 | + * size of the map. | ||
| 120 | 120 | * | |
| 121 | 121 | * <p> | |
| 122 | 122 | * Note that packXxxHeader methods don't validate number of elements. You must call packer methods for correct | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -139,12 +139,12 @@ | |||
| 139 | 139 | * | |
| 140 | 140 | * <p> | |
| 141 | 141 | * To read an Array type, first you call {@link #unpackArrayHeader()} method to get number of elements. Then, | |
| 142 | - * you call unpacker methods for each element. You don't have to call anything at the end of iteration. | ||
| 142 | + * you call unpacker methods for each element. | ||
| 143 | 143 | * | |
| 144 | 144 | * <p> | |
| 145 | 145 | * To read a Map, first you call {@link #unpackMapHeader(int)} method to get number of pairs of the map. Then, | |
| 146 | 146 | * for each pair, you call unpacker methods for key first, and then value. You will call unpacker methods twice | |
| 147 | - * as many time as the returned count. You don't have to call anything at the end of iteration. | ||
| 147 | + * as many time as the returned count. | ||
| 148 | 148 | * | |
| 149 | 149 | */ | |
| 150 | 150 | public class MessageUnpacker | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,11 @@ | |||
| 19 | 19 | import java.util.ArrayList; | |
| 20 | 20 | ||
| 21 | 21 | /** | |
| 22 | - * MessageBufferOutput adapter that packs data into list of byte arrays. | ||
| 22 | + * MessageBufferOutput adapter that writes data into a list of byte arrays. | ||
| 23 | + * <p> | ||
| 24 | + * This class allocates a new buffer instead of resizing the buffer when data doesn't fit in the initial capacity. | ||
| 25 | + * This is faster than ByteArrayOutputStream especially when size of written bytes is large because resizing a buffer | ||
| 26 | + * usually needs to copy contents of the buffer. | ||
| 23 | 27 | */ | |
| 24 | 28 | public class ArrayBufferOutput | |
| 25 | 29 | implements MessageBufferOutput | |
@@ -39,6 +43,11 @@ public ArrayBufferOutput(int bufferSize) | |||
| 39 | 43 | this.list = new ArrayList<MessageBuffer>(); | |
| 40 | 44 | } | |
| 41 | 45 | ||
| 46 | + /** | ||
| 47 | + * Gets size of the written data. | ||
| 48 | + * | ||
| 49 | + * @return number of bytes | ||
| 50 | + */ | ||
| 42 | 51 | public int getSize() | |
| 43 | 52 | { | |
| 44 | 53 | int size = 0; | |
@@ -48,6 +57,14 @@ public int getSize() | |||
| 48 | 57 | return size; | |
| 49 | 58 | } | |
| 50 | 59 | ||
| 60 | + /** | ||
| 61 | + * Gets copy of the written data as a byte array. | ||
| 62 | + * <p> | ||
| 63 | + * If your application needs better performance and smaller memory consumption, you may prefer | ||
| 64 | + * {@link #toMessageBuffer()} or {@link #toBufferList()} to avoid copying. | ||
| 65 | + * | ||
| 66 | + * @return the byte array | ||
| 67 | + */ | ||
| 51 | 68 | public byte[] toByteArray() | |
| 52 | 69 | { | |
| 53 | 70 | byte[] data = new byte[getSize()]; | |
@@ -59,6 +76,14 @@ public byte[] toByteArray() | |||
| 59 | 76 | return data; | |
| 60 | 77 | } | |
| 61 | 78 | ||
| 79 | + /** | ||
| 80 | + * Gets the written data as a MessageBuffer. | ||
| 81 | + * <p> | ||
| 82 | + * Unlike {@link #toByteArray()}, this method omits copy of the contents if size of the written data is smaller | ||
| 83 | + * than a single buffer capacity. | ||
| 84 | + * | ||
| 85 | + * @return the MessageBuffer instance | ||
| 86 | + */ | ||
| 62 | 87 | public MessageBuffer toMessageBuffer() | |
| 63 | 88 | { | |
| 64 | 89 | if (list.size() == 1) { | |
@@ -72,13 +97,21 @@ else if (list.isEmpty()) { | |||
| 72 | 97 | } | |
| 73 | 98 | } | |
| 74 | 99 | ||
| 100 | + /** | ||
| 101 | + * Returns the written data as a list of MessageBuffer. | ||
| 102 | + * <p> | ||
| 103 | + * Unlike {@link #toByteArray()} or {@link #toMessageBuffer()}, this is the fastest method that doesn't | ||
| 104 | + * copy contents in any cases. | ||
| 105 | + * | ||
| 106 | + * @return the list of MessageBuffer instances | ||
| 107 | + */ | ||
| 75 | 108 | public List<MessageBuffer> toBufferList() | |
| 76 | 109 | { | |
| 77 | 110 | return new ArrayList<MessageBuffer>(list); | |
| 78 | 111 | } | |
| 79 | 112 | ||
| 80 | 113 | /** | |
| 81 | - * Clears the internal buffers | ||
| 114 | + * Clears the written data. | ||
| 82 | 115 | */ | |
| 83 | 116 | public void clear() | |
| 84 | 117 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,12 +28,20 @@ | |||
| 28 | 28 | import static org.msgpack.core.Preconditions.checkNotNull; | |
| 29 | 29 | ||
| 30 | 30 | /** | |
| 31 | - * MessageBuffer class is an abstraction of memory for reading/writing message packed data. | ||
| 32 | - * This MessageBuffers ensures short/int/float/long/double values are written in the big-endian order. | ||
| 33 | - * <p/> | ||
| 34 | - * This class is optimized for fast memory access, so many methods are | ||
| 35 | - * implemented without using any interface method that produces invokeinterface call in JVM. | ||
| 36 | - * Compared to invokevirtual, invokeinterface is 30% slower in general because it needs to find a target function from the table. | ||
| 31 | + * MessageBuffer class is an abstraction of memory with fast methods to serialize and deserialize primitive values | ||
| 32 | + * to/from the memory. All MessageBuffer implementations ensure short/int/float/long/double values are written in | ||
| 33 | + * big-endian order. | ||
| 34 | + * <p> | ||
| 35 | + * Applications can allocate a new buffer using {@link #allocate(int)} method, or wrap an byte array or ByteBuffer | ||
| 36 | + * using {@link wrap(byte[], int, int)} methods. {@link wrap(ByteBuffer)} method supports both direct buffers and | ||
| 37 | + * array-backed buffers. | ||
| 38 | + * <p> | ||
| 39 | + * MessageBuffer class itself is optimized for little-endian CPU archtectures so that JVM (HotSpot) can take advantage | ||
| 40 | + * of the fastest JIT format which skips TypeProfile checking. To ensure this performance, applications must not load | ||
| 41 | + * unnecessary classes such as MessagePackBE. On big-endian CPU archtectures, implementation uses subclass that | ||
| 42 | + * includes TypeProfile overhead but still faster than ByteBuffer. On JVMs older than Java 7 and JVMs without Unsafe | ||
| 43 | + * API (such as Android), implementation falls back to a universal implementation that uses standard ByteBuffer class | ||
| 44 | + * internally. | ||
| 37 | 45 | */ | |
| 38 | 46 | public class MessageBuffer | |
| 39 | 47 | { | |
@@ -69,8 +77,7 @@ public class MessageBuffer | |||
| 69 | 77 | try { | |
| 70 | 78 | int major = Integer.parseInt(javaVersion.substring(0, dotPos)); | |
| 71 | 79 | int minor = Integer.parseInt(javaVersion.substring(dotPos + 1)); | |
| 72 | - isJavaAtLeast7 = major > 1 || (major == 1 && minor >= 7); | ||
| 73 | - } | ||
| 80 | + isJavaAtLeast7 = major > 1 || (major == 1 && minor >= 7); } | ||
| 74 | 81 | catch (NumberFormatException e) { | |
| 75 | 82 | e.printStackTrace(System.err); | |
| 76 | 83 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,19 +19,38 @@ | |||
| 19 | 19 | import java.io.IOException; | |
| 20 | 20 | ||
| 21 | 21 | /** | |
| 22 | - * Provides a sequence of MessageBuffers that contains message packed data. | ||
| 22 | + * Provides a sequence of MessageBuffer instances. | ||
| 23 | + * | ||
| 24 | + * A MessageBufferInput implementation has control of lifecycle of the memory so that it can reuse previously | ||
| 25 | + * allocated memory, use memory pools, or use memory-mapped files. | ||
| 23 | 26 | */ | |
| 24 | 27 | public interface MessageBufferInput | |
| 25 | 28 | extends Closeable | |
| 26 | 29 | { | |
| 27 | 30 | /** | |
| 28 | - * Get a next buffer to read. | ||
| 31 | + * Returns a next buffer to read. | ||
| 29 | 32 | * <p> | |
| 30 | - * When this method is called, the formally allocated buffer can be safely discarded. | ||
| 33 | + * This method should return a MessageBuffer instance that has data filled in. When this method is called twice, | ||
| 34 | + * the previously returned buffer is no longer used. Thus implementation of this method can safely discard it. | ||
| 35 | + * This is useful when it uses a memory pool. | ||
| 31 | 36 | * | |
| 32 | 37 | * @return the next MessageBuffer, or return null if no more buffer is available. | |
| 33 | - * @throws IOException when error occurred when reading the data | ||
| 38 | + * @throws IOException when IO error occurred when reading the data | ||
| 34 | 39 | */ | |
| 35 | 40 | MessageBuffer next() | |
| 36 | 41 | throws IOException; | |
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Closes the input. | ||
| 45 | + * <p> | ||
| 46 | + * When this method is called, the buffer previously returned from {@link next()} method is no longer used. | ||
| 47 | + * Thus implementation of this method can safely discard it. | ||
| 48 | + * <p> | ||
| 49 | + * If the input is already closed then invoking this method has no effect. | ||
| 50 | + * | ||
| 51 | + * @throws IOException when IO error occurred when closing the data source | ||
| 52 | + */ | ||
| 53 | + @Override | ||
| 54 | + void close() | ||
| 55 | + throws IOException; | ||
| 37 | 56 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,29 +20,40 @@ | |||
| 20 | 20 | import java.io.Flushable; | |
| 21 | 21 | ||
| 22 | 22 | /** | |
| 23 | - * Provides a buffered output stream for packing objects | ||
| 23 | + * Provides a buffered output stream that writes sequence of MessageBuffer instances. | ||
| 24 | + * | ||
| 25 | + * A MessageBufferOutput implementation has total control of the buffer memory so that it can reuse buffer memory, | ||
| 26 | + * use buffer pools, or use memory-mapped files. | ||
| 24 | 27 | */ | |
| 25 | 28 | public interface MessageBufferOutput | |
| 26 | 29 | extends Closeable, Flushable | |
| 27 | 30 | { | |
| 28 | 31 | /** | |
| 29 | - * Allocates the next buffer for writing message packed data. | ||
| 30 | - * If the previously allocated buffer is not flushed yet, this next method should discard | ||
| 31 | - * it without writing it. | ||
| 32 | + * Allocates the next buffer to write. | ||
| 33 | + * <p> | ||
| 34 | + * This method should return a MessageBuffer instance that has at least specified size of capacity. | ||
| 35 | + * <p> | ||
| 36 | + * When this method is called twice, the previously returned buffer is no longer used. This method may be called | ||
| 37 | + * twice without call of {@link writeBuffer(MessageBuffer)} in between. In this case, the buffer should be | ||
| 38 | + * discarded without flushing it to the output. | ||
| 32 | 39 | * | |
| 33 | 40 | * @param minimumSize the mimium required buffer size to allocate | |
| 34 | - * @return | ||
| 41 | + * @return the MessageBuffer instance with at least minimumSize bytes of capacity | ||
| 35 | 42 | * @throws IOException | |
| 36 | 43 | */ | |
| 37 | 44 | MessageBuffer next(int minimumSize) | |
| 38 | 45 | throws IOException; | |
| 39 | 46 | ||
| 40 | 47 | /** | |
| 41 | - * Flushes the previously allocated buffer. | ||
| 42 | - * This method is not always called because next method also flushes previously allocated buffer. | ||
| 43 | - * This method is called when write method is called or application wants to control the timing of flush. | ||
| 48 | + * Writes the previously allocated buffer. | ||
| 49 | + * <p> | ||
| 50 | + * This method should write the buffer previously returned from {@link next(int)} method until specified number of | ||
| 51 | + * bytes. Once the buffer is written, the buffer is no longer used. | ||
| 52 | + * <p> | ||
| 53 | + * This method is not always called for each {@link next(int)} call. In this case, the buffer should be discarded | ||
| 54 | + * without flushing it to the output. | ||
| 44 | 55 | * | |
| 45 | - * @param length the size of buffer to flush | ||
| 56 | + * @param length the number of bytes to write | ||
| 46 | 57 | * @throws IOException | |
| 47 | 58 | */ | |
| 48 | 59 | void writeBuffer(int length) | |
@@ -63,7 +74,10 @@ void write(byte[] buffer, int offset, int length) | |||
| 63 | 74 | ||
| 64 | 75 | /** | |
| 65 | 76 | * Writes an external payload data. | |
| 66 | - * This buffer is given - this MessageBufferOutput owns the buffer and may modify contents of the buffer. Contents of this buffer won't be modified by the caller. | ||
| 77 | + * <p> | ||
| 78 | + * Unlike {@link #write(byte[], int, int)} method, the buffer is given - this MessageBufferOutput implementation | ||
| 79 | + * gets ownership of the buffer and may modify contents of the buffer. Contents of this buffer won't be modified | ||
| 80 | + * by the caller. | ||
| 67 | 81 | * | |
| 68 | 82 | * @param buffer the data to add | |
| 69 | 83 | * @param offset the start offset in the data | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,7 @@ | |||
| 19 | 19 | import java.util.List; | |
| 20 | 20 | ||
| 21 | 21 | /** | |
| 22 | - * The interface {@code ArrayValue} represents MessagePack's Array type. | ||
| 22 | + * Representation of MessagePack's Array type. | ||
| 23 | 23 | * | |
| 24 | 24 | * MessagePack's Array type can represent sequence of values. | |
| 25 | 25 | */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,7 @@ | |||
| 16 | 16 | package org.msgpack.value; | |
| 17 | 17 | ||
| 18 | 18 | /** | |
| 19 | - * The interface {@code BinaryValue} represents MessagePack's Binary type. | ||
| 19 | + * Representation of MessagePack's Binary type. | ||
| 20 | 20 | * | |
| 21 | 21 | * MessagePack's Binary type can represent a byte array at most 2<sup>64</sup>-1 bytes. | |
| 22 | 22 | * | |
| Back | FazBrowse Home | New Git URL |
0 commit comments