|
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,42 +56,69 @@ 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 { |
63 | 78 | flush(); |
64 | 79 | } |
65 | 80 | catch (IOException ex) { |
66 | | - // IOException must not happen because underlaying ArrayBufferOutput never throws IOException |
| 81 | + // IOException must not happen because underlying ArrayBufferOutput never throws IOException |
67 | 82 | throw new RuntimeException(ex); |
68 | 83 | } |
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 { |
75 | 98 | flush(); |
76 | 99 | } |
77 | 100 | catch (IOException ex) { |
78 | | - // IOException must not happen because underlaying ArrayBufferOutput never throws IOException |
| 101 | + // IOException must not happen because underlying ArrayBufferOutput never throws IOException |
79 | 102 | throw new RuntimeException(ex); |
80 | 103 | } |
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 { |
87 | 118 | flush(); |
88 | 119 | } |
89 | 120 | catch (IOException ex) { |
90 | | - // IOException must not happen because underlaying ArrayBufferOutput never throws IOException |
| 121 | + // IOException must not happen because underlying ArrayBufferOutput never throws IOException |
91 | 122 | throw new RuntimeException(ex); |
92 | 123 | } |
93 | 124 | return getArrayBufferOut().toBufferList(); |
|
0 commit comments