BytesLogParam implements LogParam { @Override public void append(StringBuilder builder, Set<String> maskedFields, int maxParamLength) { if (bytes == null) { builder.append("null"); return; } boolean truncate = false; String value; if (bytes.length > maxParamLength) { value = new String(bytes, 0, maxParamLength, charset); truncate = true; } else { value = new String(bytes, charset); } if (shouldMask(value, maskedFields)) { builder.append(filter(value, maskedFields)); } else { if (truncate) { builder.append(value, 0, value.length() - 1); } else { builder.append(value); } } if (truncate) { builder.append("...(truncated)"); } } BytesLogParam(byte[] bytes);  BytesLogParam(byte[] bytes, Charset charset); @Override void append(StringBuilder builder, Set<String> maskedFields, int maxParamLength);  }
@Test void appendWithTruncation() { var param = new BytesLogParam(Strings.bytes("text-♥")); var builder = new StringBuilder(); param.append(builder, Set.of(), 6); assertThat(builder.toString()).isEqualTo("text-...(truncated)"); }
@Test void appendNull() { var param = new BytesLogParam(null); var builder = new StringBuilder(); param.append(builder, Set.of(), 6); assertThat(builder.toString()).isEqualTo("null"); }
@Test void append() { var param = new BytesLogParam(Strings.bytes("message1234567890")); var builder = new StringBuilder(); param.append(builder, Set.of(), 10); assertThat(builder.toString()).isEqualTo("message12...(truncated)"); builder = new StringBuilder(); param.append(builder, Set.of(), 1000); assertThat(builder.toString()).isEqualTo("message1234567890"); }
@Test void appendWithMask() { var param = new BytesLogParam(Strings.bytes("{\"field1\":\"value1\",\"password\":\"pass123\",\"field2\":\"value2\"}")); var builder = new StringBuilder(); param.append(builder, Set.of("password", "passwordConfirm"), 50); assertThat(builder.toString()) .isEqualTo("{\"field1\":\"value1\",\"password\":\"******\",\"field2\":\"...(truncated)"); }