Source:
<code>NatsMessage(String subject, String replyTo, byte[] data, boolean utf8mode) { this.subject = subject; this.replyTo = replyTo; this.data = data; if (utf8mode) { <START> StringBuilder protocolStringBuilder = new StringBuilder((2 * subject.length()) + 20); END> protocolStringBuilder.append(PUB_SPACE); protocolStringBuilder.append(subject); protocolStringBuilder.append(SPACE); if (replyTo != null) { protocolStringBuilder.append(replyTo); protocolStringBuilder.append(SPACE); } protocolStringBuilder.append(String.valueOf(data.length)); this.protocolBytes = protocolStringBuilder.toString().getBytes(StandardCharsets.UTF_8); } else { byte[] lengthBytes = new byte[12]; int idx = lengthBytes.length; int size = (data != null) ? data.length : 0; if (size > 0) { for (int i = size; i > 0; i /= 10) { idx--; lengthBytes[idx] = digits[i % 10]; } } else { idx--; lengthBytes[idx] = digits[0]; } int len = 4 + subject.length() + 1 + (lengthBytes.length - idx); if (replyTo != null) { len += replyTo.length() + 1; } this.protocolBytes = new byte[len]; int pos = 0; protocolBytes[0] = 'P'; protocolBytes[1] = 'U'; protocolBytes[2] = 'B'; protocolBytes[3] = ' '; pos = 4; pos = copy(protocolBytes, pos, subject); protocolBytes[pos] = ' '; pos++; if (replyTo != null) { pos = copy(protocolBytes, pos, replyTo); protocolBytes[pos] = ' '; pos++; } System.arraycopy(lengthBytes, idx, protocolBytes, pos, lengthBytes.length - idx); } this.sizeInBytes = this.protocolBytes.length + data.length + 4; }</code><technical_language>I guess is estimation size grow needed, strange 2*subject. like: int subjectSize = subject.length() * 2; int replySize = (replyTo != null) ? replyTo.length() * 2 : 0; StringBuilder protocolStringBuilder = new StringBuilder(4 + subjectSize + 1 + replySize + 1);</technical_language>

Target:
NatsMessage(String subject, String replyTo, byte[] data, boolean utf8mode) { this.subject = subject; this.replyTo = replyTo; this.data = data; if (utf8mode) { int subjectSize = subject.length() * 2; int replySize = (replyTo != null) ? replyTo.length() * 2 : 0; StringBuilder protocolStringBuilder = new StringBuilder(4 + subjectSize + 1 + replySize + 1); protocolStringBuilder.append(PUB_SPACE); protocolStringBuilder.append(subject); protocolStringBuilder.append(SPACE); if (replyTo != null) { protocolStringBuilder.append(replyTo); protocolStringBuilder.append(SPACE); } protocolStringBuilder.append(String.valueOf(data.length)); this.protocolBytes = protocolStringBuilder.toString().getBytes(StandardCharsets.UTF_8); } else { byte[] lengthBytes = new byte[12]; int idx = lengthBytes.length; int size = (data != null) ? data.length : 0; if (size > 0) { for (int i = size; i > 0; i /= 10) { idx--; lengthBytes[idx] = digits[i % 10]; } } else { idx--; lengthBytes[idx] = digits[0]; } int len = 4 + subject.length() + 1 + (lengthBytes.length - idx); if (replyTo != null) { len += replyTo.length() + 1; } this.protocolBytes = new byte[len]; int pos = 0; protocolBytes[0] = 'P'; protocolBytes[1] = 'U'; protocolBytes[2] = 'B'; protocolBytes[3] = ' '; pos = 4; pos = copy(protocolBytes, pos, subject); protocolBytes[pos] = ' '; pos++; if (replyTo != null) { pos = copy(protocolBytes, pos, replyTo); protocolBytes[pos] = ' '; pos++; } System.arraycopy(lengthBytes, idx, protocolBytes, pos, lengthBytes.length - idx); } this.sizeInBytes = this.protocolBytes.length + data.length + 4; }