Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Offset is not correct after calling ByteBuffer#writeCString with data already in the buffer #51

Closed
@DoctorMcKay

Description

@DoctorMcKay

After calling writeCString on a buffer with data already in it, the offset is set to the length of the string + 1, instead of offset + length + 1.

Code to reproduce:

var str = "This is a test string.";
var body = new ByteBuffer(BUFFER_MAX_SIZE, ByteBuffer.LITTLE_ENDIAN);
body.writeUint32(55555); // 4 bytes, offset = 4
body.writeUint16(1234); // 2 bytes, offset = 6
body.writeCString(str); // 23 bytes, offset should = 29
body.writeUint32(123456); // 4 bytes, offset should = 33

console.log("Expected offset: %d, actual: %d", 4 + 2 + str.length + 1 + 4, body.offset); // Expected offset: 33, actual: 27

body.flip();
console.log("Uint32: %d, Uint16: %d string: %s, Uint32: %d", body.readUint32(), body.readUint16(), body.readCString(), body.readUint32()); // Uint32: 55555, Uint16: 1234 string: This is a test st@?☺, Uint32: 46

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions