Skip to content

Commit 2c39105

Browse files
authored
0.62,1
- Fix Java CharBuffer and ByteBuffer handling
2 parents 703a98a + 8118e1d commit 2c39105

File tree

7 files changed

+100
-56
lines changed

7 files changed

+100
-56
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ subprojects {
55
apply plugin: 'maven'
66

77
group 'org.iot-dsa'
8-
version '0.62.0'
8+
version '0.62.1'
99

1010
sourceCompatibility = 1.8
1111
targetCompatibility = 1.8

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/io/DSByteBuffer.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,22 @@ public void print(PrintStream out, int cols) {
8282
}
8383

8484
/**
85-
* Gets the bytes from the given buffer, which will be flipped, then cleared.
85+
* Gets the bytes from the given buffer, which should be positioned for bulk
86+
* relative gets.
8687
*/
8788
public DSByteBuffer put(ByteBuffer buf) {
88-
int pos = buf.position();
89+
int len = buf.remaining();
8990
int bufLen = buffer.length;
90-
if ((pos + length + offset) >= bufLen) {
91-
if ((pos + length) > bufLen) { //the buffer is too small
92-
growBuffer(pos + length);
91+
if ((len + length + offset) >= bufLen) {
92+
if ((len + length) > bufLen) { //the buffer is too small
93+
growBuffer(len + length);
9394
} else { //offset must be > 0, shift everything to index 0
9495
System.arraycopy(buffer, offset, buffer, 0, length);
9596
offset = 0;
9697
}
9798
}
98-
buf.flip();
99-
buf.get(buffer, length + offset, pos);
100-
buf.clear();
101-
length += pos;
99+
buf.get(buffer, length + offset, len);
100+
length += len;
102101
return this;
103102
}
104103

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/io/msgpack/MsgpackReader.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,21 @@ public MsgpackReader setInput(InputStream inputStream) {
176176
* reuse the same buffer.
177177
*/
178178
private ByteBuffer getByteBuffer(byte[] bytes, int off, int len) {
179-
if ((byteBuffer == null) || (byteBuffer.capacity() < len)) {
179+
if (byteBuffer == null) {
180180
int tmp = 1024;
181181
while (tmp < len) {
182182
tmp += 1024;
183183
}
184184
byteBuffer = ByteBuffer.allocate(tmp);
185185
} else {
186186
byteBuffer.clear();
187+
if (byteBuffer.capacity() < len) {
188+
int tmp = 1024;
189+
while (tmp < len) {
190+
tmp += 1024;
191+
}
192+
byteBuffer = ByteBuffer.allocate(tmp);
193+
}
187194
}
188195
byteBuffer.put(bytes, 0, len);
189196
byteBuffer.flip();
@@ -195,14 +202,21 @@ private ByteBuffer getByteBuffer(byte[] bytes, int off, int len) {
195202
* reuse the same char buffer.
196203
*/
197204
private CharBuffer getCharBuffer(int size) {
198-
if ((charBuffer == null) || (charBuffer.length() < size)) {
205+
if (charBuffer == null) {
199206
int tmp = 1024;
200207
while (tmp < size) {
201208
tmp += 1024;
202209
}
203210
charBuffer = CharBuffer.allocate(tmp);
204211
} else {
205212
charBuffer.clear();
213+
if (charBuffer.remaining() < size) {
214+
int tmp = 1024;
215+
while (tmp < size) {
216+
tmp += 1024;
217+
}
218+
charBuffer = CharBuffer.allocate(tmp);
219+
}
206220
}
207221
return charBuffer;
208222
}

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/io/msgpack/MsgpackWriter.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,16 @@ public void writeTo(OutputStream out) {
104104
* Writes the UTF8 bytes to the underlying buffer.
105105
*/
106106
public void writeUTF8(CharSequence arg) {
107+
if (arg.length() == 0) {
108+
return;
109+
}
107110
CharBuffer chars = getCharBuffer(arg);
108-
ByteBuffer strBuffer = getStringBuffer(chars.position() * (int) encoder.maxBytesPerChar());
111+
ByteBuffer strBuffer = getStringBuffer(
112+
chars.remaining() * (int) encoder.maxBytesPerChar());
109113
encoder.encode(chars, strBuffer, false);
110-
byteBuffer.put(strBuffer);
111114
encoder.reset();
115+
strBuffer.flip();
116+
byteBuffer.put(strBuffer);
112117
}
113118

114119
@Override
@@ -304,14 +309,15 @@ private CharBuffer getCharBuffer(CharSequence arg) {
304309
tmp += 1024;
305310
}
306311
charBuffer = CharBuffer.allocate(tmp);
307-
} else if (charBuffer.capacity() < len) {
308-
int tmp = charBuffer.capacity();
309-
while (tmp < len) {
310-
tmp += 1024;
311-
}
312-
charBuffer = CharBuffer.allocate(tmp);
313312
} else {
314313
charBuffer.clear();
314+
if (charBuffer.capacity() < len) {
315+
int tmp = charBuffer.capacity();
316+
while (tmp < len) {
317+
tmp += 1024;
318+
}
319+
charBuffer = CharBuffer.allocate(tmp);
320+
}
315321
}
316322
charBuffer.append(arg);
317323
charBuffer.flip();
@@ -329,24 +335,31 @@ private ByteBuffer getStringBuffer(int len) {
329335
tmp += 1024;
330336
}
331337
strBuffer = ByteBuffer.allocate(tmp);
332-
} else if (strBuffer.capacity() < len) {
333-
int tmp = strBuffer.capacity();
334-
while (tmp < len) {
335-
tmp += 1024;
336-
}
337-
strBuffer = ByteBuffer.allocate(tmp);
338338
} else {
339339
strBuffer.clear();
340+
if (strBuffer.capacity() < len) {
341+
int tmp = strBuffer.capacity();
342+
while (tmp < len) {
343+
tmp += 1024;
344+
}
345+
strBuffer = ByteBuffer.allocate(tmp);
346+
}
340347
}
341348
return strBuffer;
342349
}
343350

344351
private void writeString(CharSequence arg) throws IOException {
352+
if (arg.length() == 0) {
353+
byteBuffer.put(FIXSTR_PREFIX);
354+
return;
355+
}
345356
CharBuffer chars = getCharBuffer(arg);
346357
ByteBuffer strBuffer = getStringBuffer(
347-
chars.length() * (int) encoder.maxBytesPerChar());
358+
chars.remaining() * (int) encoder.maxBytesPerChar());
348359
encoder.encode(chars, strBuffer, false);
349-
int len = strBuffer.position();
360+
encoder.reset();
361+
strBuffer.flip();
362+
int len = strBuffer.remaining();
350363
if (len < (1 << 5)) {
351364
byteBuffer.put((byte) (FIXSTR_PREFIX | len));
352365
} else if (len < (1 << 8)) {
@@ -360,7 +373,6 @@ private void writeString(CharSequence arg) throws IOException {
360373
byteBuffer.putInt(len);
361374
}
362375
byteBuffer.put(strBuffer);
363-
encoder.reset();
364376
}
365377

366378
// Inner Classes

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/protocol/v2/DS2MessageReader.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,21 @@ void parseDynamicHeaders(InputStream in, int len, Map<Integer, Object> headers)
236236
* reuse the same char buffer.
237237
*/
238238
private CharBuffer getCharBuffer(int size) {
239-
if ((charBuffer == null) || (charBuffer.capacity() < size)) {
239+
if (charBuffer == null) {
240240
int tmp = 1024;
241241
while (tmp < size) {
242242
tmp += 1024;
243243
}
244244
charBuffer = CharBuffer.allocate(tmp);
245245
} else {
246246
charBuffer.clear();
247+
if (charBuffer.capacity() < size) {
248+
int tmp = 1024;
249+
while (tmp < size) {
250+
tmp += 1024;
251+
}
252+
charBuffer = CharBuffer.allocate(tmp);
253+
}
247254
}
248255
return charBuffer;
249256
}
@@ -259,14 +266,15 @@ private ByteBuffer getStringBuffer(int len) {
259266
tmp += 1024;
260267
}
261268
strBuffer = ByteBuffer.allocate(tmp);
262-
} else if (strBuffer.capacity() < len) {
263-
int tmp = strBuffer.capacity();
264-
while (tmp < len) {
265-
tmp += 1024;
266-
}
267-
strBuffer = ByteBuffer.allocate(tmp);
268269
} else {
269270
strBuffer.clear();
271+
if (strBuffer.capacity() < len) {
272+
int tmp = strBuffer.capacity();
273+
while (tmp < len) {
274+
tmp += 1024;
275+
}
276+
strBuffer = ByteBuffer.allocate(tmp);
277+
}
270278
}
271279
return strBuffer;
272280
}

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/protocol/v2/DS2MessageWriter.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,35 @@ public DS2MessageWriter write(DSITransport out) {
178178
* DSA 2.n encodes a string into the body.
179179
*/
180180
public void writeString(CharSequence str) {
181+
if (str.length() == 0) {
182+
body.putShort((short) 0, false);
183+
return;
184+
}
181185
CharBuffer chars = getCharBuffer(str);
182186
ByteBuffer strBuffer = getStringBuffer(
183-
chars.length() * (int) utf8encoder.maxBytesPerChar());
187+
chars.remaining() * (int) utf8encoder.maxBytesPerChar());
184188
utf8encoder.encode(chars, strBuffer, false);
185-
body.putShort((short) strBuffer.position(), false);
189+
utf8encoder.reset();
190+
strBuffer.flip();
191+
body.putShort((short) strBuffer.remaining(), false);
186192
body.put(strBuffer);
187193
}
188194

189195
/**
190196
* DSA 2.n encodes a string into the the given buffer.
191197
*/
192198
public void writeString(CharSequence str, DSByteBuffer buf) {
199+
if (str.length() == 0) {
200+
buf.putShort((short) 0, false);
201+
return;
202+
}
193203
CharBuffer chars = getCharBuffer(str);
194204
ByteBuffer strBuffer = getStringBuffer(
195-
chars.length() * (int) utf8encoder.maxBytesPerChar());
205+
chars.remaining() * (int) utf8encoder.maxBytesPerChar());
196206
utf8encoder.encode(chars, strBuffer, false);
197-
buf.putShort((short) strBuffer.position(), false);
207+
utf8encoder.reset();
208+
strBuffer.flip();
209+
buf.putShort((short) strBuffer.remaining(), false);
198210
buf.put(strBuffer);
199211
}
200212

@@ -239,14 +251,15 @@ private CharBuffer getCharBuffer(CharSequence arg) {
239251
tmp += 1024;
240252
}
241253
charBuffer = CharBuffer.allocate(tmp);
242-
} else if (charBuffer.capacity() < len) {
243-
int tmp = charBuffer.capacity();
244-
while (tmp < len) {
245-
tmp += 1024;
246-
}
247-
charBuffer = CharBuffer.allocate(tmp);
248254
} else {
249255
charBuffer.clear();
256+
if (charBuffer.capacity() < len) {
257+
int tmp = charBuffer.capacity();
258+
while (tmp < len) {
259+
tmp += 1024;
260+
}
261+
charBuffer = CharBuffer.allocate(tmp);
262+
}
250263
}
251264
charBuffer.append(arg);
252265
charBuffer.flip();
@@ -264,14 +277,15 @@ private ByteBuffer getStringBuffer(int len) {
264277
tmp += 1024;
265278
}
266279
strBuffer = ByteBuffer.allocate(tmp);
267-
} else if (strBuffer.capacity() < len) {
268-
int tmp = strBuffer.capacity();
269-
while (tmp < len) {
270-
tmp += 1024;
271-
}
272-
strBuffer = ByteBuffer.allocate(tmp);
273280
} else {
274281
strBuffer.clear();
282+
if (strBuffer.capacity() < len) {
283+
int tmp = strBuffer.capacity();
284+
while (tmp < len) {
285+
tmp += 1024;
286+
}
287+
strBuffer = ByteBuffer.allocate(tmp);
288+
}
275289
}
276290
return strBuffer;
277291
}

dslink-v2/src/test/java/org/iot/dsa/dslink/MsgpackTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public void testStrings() throws Exception {
4141
CharsetEncoder encoder = DSString.UTF8.newEncoder();
4242
encoder.encode(charBuffer, byteBuffer, false);
4343
byteBuffer.flip();
44-
CharsetDecoder decoder = DSString.UTF8.newDecoder();
4544
charBuffer.clear();
45+
CharsetDecoder decoder = DSString.UTF8.newDecoder();
4646
decoder.decode(byteBuffer, charBuffer, false);
4747
charBuffer.flip();
4848
result = charBuffer.toString();
@@ -57,7 +57,6 @@ public void theTest() throws Exception {
5757
tmp.endList();
5858
DSIReader reader = new MsgpackReader(new ByteArrayInputStream(tmp.toByteArray()));
5959
reader.getElement().toList();
60-
6160
final ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
6261
MsgpackWriter out = new MsgpackWriter();
6362
out.beginList();
@@ -68,8 +67,6 @@ public void theTest() throws Exception {
6867
out.value(new DSMap());
6968
out.value(new DSList());
7069
out.endList();
71-
//out.writeTo(baos1);
72-
//out.reset();
7370
byte[] encoded = out.toByteArray();
7471
DSIReader parser = new MsgpackReader(new ByteArrayInputStream(encoded));
7572
DSList list = parser.getElement().toList();

0 commit comments

Comments
 (0)