eygraber
04/21/2022, 10:55 PMByteBuffer
code. I'm using ktor-io but there's something that I'm not doing correctly. Code in 🧵eygraber
04/21/2022, 10:56 PMif (byteBuffer.position() == 0) {
return;
}
byteBuffer.flip();
try {
builder.append(decoder.decode(byteBuffer));
} catch (CharacterCodingException e) {
if (throwOnFailure) {
throw new IllegalArgumentException(e);
} else {
builder.append(INVALID_INPUT_CHARACTER);
}
} finally {
// Use the byte buffer to write again.
byteBuffer.flip();
byteBuffer.limit(byteBuffer.capacity());
}
eygraber
04/21/2022, 10:59 PMBuffer
):
if(writePosition == 0) return
resetForRead()
try {
builder.append(readBytes().decodeToString())
}
catch(e: Exception) {
if(throwOnFailure) {
throw IllegalArgumentException(e)
}
else {
builder.append(INVALID_INPUT_CHARACTER)
}
}
finally {
reset()
}
eygraber
04/21/2022, 11:01 PMreadBytes
(this is after writing 1 byte to the buffer then calling my function, then writing 3 bytes to the buffer then calling my function, then writing 3 bytes to the buffer then calling my function):
47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-60, -126, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-32, -95, -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
ephemient
04/21/2022, 11:21 PMBuffer
you're using. doesn't seem to be okio.Bufferephemient
04/21/2022, 11:24 PMresetForRead()
doesn't seem to be equivalent to `flip()`: it marks the whole buffer as readable, not just the part up to the write positioneygraber
04/21/2022, 11:25 PMBuffer
but my IDE is having issues resolving dependencies, so it's hard to step through it to see how it works.
I'm assuming there's a way to do it with okio.Buffer
as well?ephemient
04/21/2022, 11:28 PMeygraber
04/21/2022, 11:37 PM