Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,35 @@ public MessagePacker writePayload(byte[] src, int off, int len)
return this;
}

/**
* This method copies the lower 8-bits of the characters.
* A separate writePayload(String src) method would be more performant because we're able to
* directly copy the bytes from the string to the right location in MessageBuffer. Introducing
* encoding in an attempt to re-use the existing writePayload(byte[] src) creates unnecessary overhead
* which in turn will affect performance.
*
* @param src the string data to add
* @return this
* @throws IOException
*/

public MessagePacker writePayload(String src) throws IOException
{
int len = src.length();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the input string contains non-ascii characters, src.getBytes() will produce a longer array than src.length(), so using this method can easily create corrupted data.

if (buffer == null || buffer.size() - position < len || len > bufferFlushThreshold) {
flush(); // call flush before write
// Directly write payload to the output without using the buffer
out.write(src.getBytes(), 0, len);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If calling src.getBytes() is allowed here, you should be able to use addPayload(byte[]) instead.

totalFlushBytes += len;
}
else {
ensureCapacity(len);
src.getBytes(0, len, buffer.array(), position);
position += len;
}
return this;
}

/**
* Writes a byte array to the output.
* <p>
Expand Down