From e3fc03f240d60b8c625bd6f320f5c93dadf7bccb Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Tue, 11 Aug 2020 01:03:05 -0700 Subject: [PATCH] Adjusted documentation and added ByteInput#readString(int,boolean) --- core/src/com/riiablo/io/BitInput.java | 8 ++++---- core/src/com/riiablo/io/ByteInput.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/core/src/com/riiablo/io/BitInput.java b/core/src/com/riiablo/io/BitInput.java index 621c086a..70da63cd 100644 --- a/core/src/com/riiablo/io/BitInput.java +++ b/core/src/com/riiablo/io/BitInput.java @@ -478,12 +478,12 @@ public class BitInput { } /** - * Reads n characters of size {@code bits} and constructs a string. + * Reads up to n characters of size {@code bits} and constructs a + * string. * - * @param maxLen number of characters to read + * @param maxLen maximum number of characters to read * @param bits size of each character ({@code 7} or {@code 8}) - * @param nullTerminated {@code true} to stop reading at {@code \0}, otherwise - * {@code maxLen} characters will be read (variable-width string) + * @param nullTerminated {@code true} to stop reading at {@code '\0}' */ public String readString(int maxLen, int bits, boolean nullTerminated) { if (maxLen < 0) throw new IllegalArgumentException("maxLen(" + maxLen + ") < " + 0); diff --git a/core/src/com/riiablo/io/ByteInput.java b/core/src/com/riiablo/io/ByteInput.java index bc3a8079..085e0392 100644 --- a/core/src/com/riiablo/io/ByteInput.java +++ b/core/src/com/riiablo/io/ByteInput.java @@ -411,4 +411,22 @@ public class ByteInput { throw new EndOfInput(t); } } + + /** + * Reads up to n bytes and constructs a string. + * + * @param maxLen maximum number of characters to read + * @param nullTerminated {@code true} to stop reading at {@code '\0'} + */ + public String readString(int maxLen, boolean nullTerminated) { + assert aligned() : "not aligned"; + if (!nullTerminated) return readString(maxLen); + try { + final String string = unalign().readString(maxLen, Byte.SIZE, true); + assert aligned() : "not aligned"; + return string; + } catch (IndexOutOfBoundsException t) { + throw new EndOfInput(t); + } + } }