From 8c3c304dcd3287f89f72edaaaaad2401cf5721ec Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Wed, 19 Aug 2020 20:27:05 -0700 Subject: [PATCH] Created ByteOutput#writeString(CharSequence,int) writeString writes a string and either truncates or pads it with zeros to len --- core/src/com/riiablo/io/ByteOutput.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/src/com/riiablo/io/ByteOutput.java b/core/src/com/riiablo/io/ByteOutput.java index 60c8ccc5..74b35ade 100644 --- a/core/src/com/riiablo/io/ByteOutput.java +++ b/core/src/com/riiablo/io/ByteOutput.java @@ -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); + } + } }