Mercurial > stress-tester
changeset 729:11cdcfb2d190
more byte[] <-> ByteString/String utilities
author | Devel 1 |
---|---|
date | Mon, 04 Dec 2017 12:23:59 +0100 |
parents | c3a2898241a0 |
children | 8e04c0095608 |
files | stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java |
diffstat | 1 files changed, 46 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java Mon Dec 04 12:23:08 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java Mon Dec 04 12:23:59 2017 +0100 @@ -4,6 +4,7 @@ import com.passus.data.ByteBuff; import com.passus.data.ByteBuffUtils; import com.passus.data.ByteString; +import com.passus.data.ByteStringImpl; import com.passus.data.DataHelper.BigEndianDataHelper; import com.passus.data.DataUtils; import com.passus.net.Ip4Address; @@ -11,6 +12,7 @@ import com.passus.net.IpAddress; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import org.apache.commons.lang3.mutable.Mutable; import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.mutable.MutableLong; @@ -78,6 +80,21 @@ return result; } + public int readByteStringNullTerminated(byte[] bytes, int off, Mutable<ByteString> value) { + return readByteStringTerminated(bytes, off, AsciiUtils.NUL, value); + } + + public int readByteStringTerminated(byte[] bytes, int off, byte delim, Mutable<ByteString> value) { + int delimIdx = ByteBuffUtils.localize(bytes, off, delim); + if (delimIdx == -1) { + return -1; + } else { + int length = delimIdx - off; + value.setValue(new ByteStringImpl(bytes, off, length)); + return length + 1; + } + } + public int readIntVLC(ByteBuff buffer) { int len = getIntVLC(buffer.buffer(), buffer.startIndex(), buffer.endIndex(), mutableInt); buffer.skipBytes(len); @@ -259,6 +276,7 @@ } public int writeByteStringNullTerminated(byte data[], int startIndex, int endIndex, ByteString value, boolean sanitize) { + // TODO: bound check int len = 0; byte[] valData = value.getBytes(); if (!value.isEmpty()) { @@ -281,6 +299,21 @@ return len + 1; } + public int readStringNullTerminated(byte[] bytes, int off, Mutable<String> value) { + return readStringTerminated(bytes, off, AsciiUtils.NUL, value); + } + + public int readStringTerminated(byte[] bytes, int off, byte delim, Mutable<String> value) { + int index = ByteBuffUtils.localize(bytes, off, AsciiUtils.NUL); + if (index != -1) { + int len = index - off; + value.setValue(new String(bytes, off, len, CHARSET)); + return len + 1; + } + + throw new IllegalArgumentException("Unable to find delimiter."); + } + public String readStringNullTerminated(ByteBuff buffer) { int index = ByteBuffUtils.localize(buffer, AsciiUtils.NUL); if (index != -1) { @@ -294,6 +327,19 @@ throw new IllegalArgumentException("Unable to find NULL delimiter."); } + public int writeStringNullTerminated(byte bytes[], int startIndex, int endIndex, String value) { + // TODO: bound check + if (value != null && !value.isEmpty()) { + byte[] data = value.getBytes(CHARSET); + System.arraycopy(data, 0, bytes, startIndex, data.length); + bytes[startIndex + data.length] = AsciiUtils.NUL; + return data.length + 1; + } + + bytes[startIndex] = AsciiUtils.NUL; + return 1; + } + public int writeStringNullTerminated(ByteBuff buffer, String value) { if (value != null && !value.isEmpty()) { byte[] data = value.getBytes(CHARSET);