forked from isopov/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayBufferOutput.java
More file actions
169 lines (154 loc) · 4.5 KB
/
Copy pathArrayBufferOutput.java
File metadata and controls
169 lines (154 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// MessagePack for Java
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.core.buffer;
import java.util.List;
import java.util.ArrayList;
/**
* MessageBufferOutput adapter that writes data into a list of byte arrays.
* <p>
* This class allocates a new buffer instead of resizing the buffer when data doesn't fit in the initial capacity.
* This is faster than ByteArrayOutputStream especially when size of written bytes is large because resizing a buffer
* usually needs to copy contents of the buffer.
*/
public class ArrayBufferOutput
implements MessageBufferOutput
{
private List<MessageBuffer> list;
private MessageBuffer lastBuffer;
private int bufferSize;
public ArrayBufferOutput()
{
this(8192);
}
public ArrayBufferOutput(int bufferSize)
{
this.bufferSize = bufferSize;
this.list = new ArrayList<MessageBuffer>();
}
/**
* Gets the size of the written data.
*
* @return number of bytes
*/
public int getSize()
{
int size = 0;
for (MessageBuffer buffer : list) {
size += buffer.size();
}
return size;
}
/**
* Gets a copy of the written data as a byte array.
* <p>
* If your application needs better performance and smaller memory consumption, you may prefer
* {@link #toMessageBuffer()} or {@link #toBufferList()} to avoid copying.
*
* @return the byte array
*/
public byte[] toByteArray()
{
byte[] data = new byte[getSize()];
int off = 0;
for (MessageBuffer buffer : list) {
buffer.getBytes(0, data, off, buffer.size());
off += buffer.size();
}
return data;
}
/**
* Gets the written data as a MessageBuffer.
* <p>
* Unlike {@link #toByteArray()}, this method omits copy of the contents if size of the written data is smaller
* than a single buffer capacity.
*
* @return the MessageBuffer instance
*/
public MessageBuffer toMessageBuffer()
{
if (list.size() == 1) {
return list.get(0);
}
else if (list.isEmpty()) {
return MessageBuffer.allocate(0);
}
else {
return MessageBuffer.wrap(toByteArray());
}
}
/**
* Returns the written data as a list of MessageBuffer.
* <p>
* Unlike {@link #toByteArray()} or {@link #toMessageBuffer()}, this is the fastest method that doesn't
* copy contents in any cases.
*
* @return the list of MessageBuffer instances
*/
public List<MessageBuffer> toBufferList()
{
return new ArrayList<MessageBuffer>(list);
}
/**
* Clears the written data.
*/
public void clear()
{
list.clear();
}
@Override
public MessageBuffer next(int minimumSize)
{
if (lastBuffer != null && lastBuffer.size() > minimumSize) {
return lastBuffer;
}
else {
int size = Math.max(bufferSize, minimumSize);
MessageBuffer buffer = MessageBuffer.allocate(size);
lastBuffer = buffer;
return buffer;
}
}
@Override
public void writeBuffer(int length)
{
list.add(lastBuffer.slice(0, length));
if (lastBuffer.size() - length > bufferSize / 4) {
lastBuffer = lastBuffer.slice(length, lastBuffer.size() - length);
}
else {
lastBuffer = null;
}
}
@Override
public void write(byte[] buffer, int offset, int length)
{
MessageBuffer copy = MessageBuffer.allocate(length);
copy.putBytes(0, buffer, offset, length);
list.add(copy);
}
@Override
public void add(byte[] buffer, int offset, int length)
{
MessageBuffer wrapped = MessageBuffer.wrap(buffer, offset, length);
list.add(wrapped);
}
@Override
public void close()
{ }
@Override
public void flush()
{ }
}