Adjusted documentation and added ByteInput#readString(int,boolean)

This commit is contained in:
Collin Smith 2020-08-11 01:03:05 -07:00
parent efce34ef8a
commit e3fc03f240
2 changed files with 22 additions and 4 deletions

View File

@ -478,12 +478,12 @@ public class BitInput {
}
/**
* Reads <i>n</i> characters of size {@code bits} and constructs a string.
* Reads up to <i>n</i> 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);

View File

@ -411,4 +411,22 @@ public class ByteInput {
throw new EndOfInput(t);
}
}
/**
* Reads up to <i>n</i> 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);
}
}
}