Skip to content

Commit

Permalink
[fix][monitor] fix metrics string encoding (#18138)
Browse files Browse the repository at this point in the history
(cherry picked from commit 031e37c)
  • Loading branch information
tjiuming authored and codelipenghui committed Oct 27, 2022
1 parent 42ccecf commit b7def56
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
HttpServletResponse res = (HttpServletResponse) context.getResponse();
try {
res.setStatus(HTTP_STATUS_OK_200);
res.setContentType("text/plain");
res.setContentType("text/plain;charset=utf-8");
generateMetrics(cluster, res.getOutputStream());
} catch (Exception e) {
long end = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,27 @@ public String str() {
reset();
return s;
}

@Test
public void testWriteString() {
String str = "persistence://test/test/test_¬¬¬¬¬¬¬aabbcc";
stream.write(str);
assertEquals(str, str());
}


@Test
public void testWriteChar() {
String str = "persistence://test/test/test_¬¬¬¬¬¬¬aabbcc\"\n";
for (char c : str.toCharArray()) {
stream.write(c);
}
assertEquals(str, str());

buf.clear();

stream.write('\n').write('"').write('A').write('Z').write('a').write('z').write(' ').write(',').write('{')
.write('}').write('[').write(']').write('¬');
assertEquals(str(), "\n\"AZaz ,{}[]¬");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.pulsar.common.util;

import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;

/**
* Format strings and numbers into a ByteBuf without any memory allocation.
Expand All @@ -44,19 +45,16 @@ public SimpleTextOutputStream write(byte[] a, int offset, int len) {
}

public SimpleTextOutputStream write(char c) {
buffer.writeByte((byte) c);
write(String.valueOf(c));
return this;
}

public SimpleTextOutputStream write(String s) {
if (s == null) {
return this;
}
int len = s.length();
for (int i = 0; i < len; i++) {
buffer.writeByte((byte) s.charAt(i));
}

buffer.writeCharSequence(s, CharsetUtil.UTF_8);
return this;
}

Expand Down

0 comments on commit b7def56

Please sign in to comment.