Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,10 @@ private static String castToString(Object value) {
if (value instanceof java.util.Date dateValue) {
return Values.dateFormatFor(dateValue).format(dateValue);
} else if (value instanceof ByteBuffer byteBuffer) {
return Base64.getEncoder().encodeToString(Utils.readBytes(byteBuffer));
// Utils.readBytes reads from index 0 to limit, ignoring position; use
// Utils.toArray so a sliced or partially consumed buffer is encoded
// from its current position to its limit.
return Base64.getEncoder().encodeToString(Utils.toArray(byteBuffer));
} else if (value instanceof byte[] rawBytes) {
return Base64.getEncoder().encodeToString(rawBytes);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,23 @@ public void castFieldsWithSchema() {
assertEquals(Timestamp.SCHEMA.type(), transformedSchema.field("timestamp").schema().type());
}

@Test
public void castSlicedByteBufferFieldToStringUsesRemainingBytes() {
xformValue.configure(Map.of(Cast.SPEC_CONFIG, "bytes:string"));

Schema schema = SchemaBuilder.struct()
.field("bytes", Schema.BYTES_SCHEMA)
.build();
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[] {1, 2, 3, 4});
byteBuffer.position(1);
byteBuffer.limit(3);

Struct value = new Struct(schema).put("bytes", byteBuffer);
SourceRecord transformed = xformValue.apply(new SourceRecord(null, null, "topic", 0, schema, value));

assertEquals("AgM=", ((Struct) transformed.value()).get("bytes"));
}

@SuppressWarnings("unchecked")
@Test
public void castFieldsSchemaless() {
Expand Down