Created ByteOutput#writeString(CharSequence,int)

writeString writes a string and either truncates or pads it with zeros to len
This commit is contained in:
Collin Smith 2020-08-19 20:27:05 -07:00
parent 8506cc4324
commit 8c3c304dcd

View File

@ -114,4 +114,15 @@ public class ByteOutput {
buffer.writeCharSequence(chars, CharsetUtil.US_ASCII);
return this;
}
public ByteOutput writeString(CharSequence chars, int len) {
if (len < 0) throw new IllegalArgumentException("len(" + len + ") < " + 0);
assert aligned() : "not aligned";
final int charsLength = chars.length();
if (len <= charsLength) {
return writeString(chars.subSequence(0, len));
} else {
return writeString(chars).skipBytes(len - charsLength);
}
}
}