[MINOR UPDATE] always specify the char encoding in getBytes (#3011)

Co-authored-by: aias00 <rokkki@163.com>
This commit is contained in:
PJ Fanning
2025-01-23 18:40:59 +08:00
committed by GitHub
co-authored by aias00
parent 783bd8de89
commit 6509b8bfc6
13 changed files with 43 additions and 24 deletions
@@ -33,6 +33,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.hertzbeat.alert.dto.AlertDefineDTO;
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
@@ -68,7 +69,7 @@ class AlertDefineJsonImExportServiceTest {
@BeforeEach
public void setup() {
inputStream = new ByteArrayInputStream(JSON_DATA.getBytes());
inputStream = new ByteArrayInputStream(JSON_DATA.getBytes(StandardCharsets.UTF_8));
AlertDefineDTO alertDefine = new AlertDefineDTO();
alertDefine.setName("App1");
@@ -20,6 +20,7 @@ package org.apache.hertzbeat.collector.collect.telnet;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -122,7 +123,7 @@ public class TelnetCollectImpl extends AbstractCollect {
return new HashMap<>(16);
}
OutputStream outputStream = telnetClient.getOutputStream();
outputStream.write(cmd.getBytes());
outputStream.write(cmd.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
String result = new String(telnetClient.getInputStream().readAllBytes());
String[] lines = result.split("\n");
@@ -28,6 +28,7 @@ import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.HashMap;
@@ -128,16 +129,16 @@ public class WebsocketCollectImpl extends AbstractCollect {
byte[] key = generateRandomKey();
String base64Key = base64Encode(key);
String requestLine = "GET " + websocketProtocol.getPath() + " HTTP/1.1\r\n";
out.write(requestLine.getBytes());
out.write(requestLine.getBytes(StandardCharsets.UTF_8));
String hostName = InetAddress.getLocalHost().getHostAddress();
out.write(("Host:" + hostName + "\r\n").getBytes());
out.write("Upgrade: websocket\r\n".getBytes());
out.write("Connection: Upgrade\r\n".getBytes());
out.write("Sec-WebSocket-Version: 13\r\n".getBytes());
out.write("Sec-WebSocket-Extensions: chat, superchat\r\n".getBytes());
out.write(("Sec-WebSocket-Key: " + base64Key + "\r\n").getBytes());
out.write("Content-Length: 0\r\n".getBytes());
out.write("\r\n".getBytes());
out.write(("Host:" + hostName + "\r\n").getBytes(StandardCharsets.UTF_8));
out.write("Upgrade: websocket\r\n".getBytes(StandardCharsets.UTF_8));
out.write("Connection: Upgrade\r\n".getBytes(StandardCharsets.UTF_8));
out.write("Sec-WebSocket-Version: 13\r\n".getBytes(StandardCharsets.UTF_8));
out.write("Sec-WebSocket-Extensions: chat, superchat\r\n".getBytes(StandardCharsets.UTF_8));
out.write(("Sec-WebSocket-Key: " + base64Key + "\r\n").getBytes(StandardCharsets.UTF_8));
out.write("Content-Length: 0\r\n".getBytes(StandardCharsets.UTF_8));
out.write("\r\n".getBytes(StandardCharsets.UTF_8));
out.flush();
}
@@ -22,6 +22,7 @@ package org.apache.hertzbeat.common.entity.message;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@@ -406,7 +407,7 @@ public final class CollectRep {
fieldIndex < row.getColumnsList().size()) {
String value = row.getColumns(fieldIndex);
if (value != null) {
vector.set(rowIndex, value.getBytes());
vector.set(rowIndex, value.getBytes(StandardCharsets.UTF_8));
}
}
}
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.ipc.ArrowStreamWriter;
@@ -86,7 +87,7 @@ class KafkaMetricsDataDeserializerTest {
@Test
void testDeserializeWithInvalidBytes() {
byte[] invalidBytes = "invalid data".getBytes();
byte[] invalidBytes = "invalid data".getBytes(StandardCharsets.UTF_8);
assertThrows(RuntimeException.class, () -> deserializer.deserialize("", invalidBytes));
}
@@ -24,6 +24,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import java.nio.charset.StandardCharsets;
/**
* test case for {@link FileUtil}.
*/
@@ -41,9 +43,12 @@ class FileUtilTest {
@BeforeEach
void setUp() {
jsonFile = new MockMultipartFile("file", "test.json", MediaType.APPLICATION_JSON_VALUE, "test content".getBytes());
excelFile = new MockMultipartFile("file", "test.xlsx", EXCEL_TYPE, "test content".getBytes());
yamlFile = new MockMultipartFile("file", "test.yaml", YAML_TYPE, "test content".getBytes());
jsonFile = new MockMultipartFile("file", "test.json", MediaType.APPLICATION_JSON_VALUE,
"test content".getBytes(StandardCharsets.UTF_8));
excelFile = new MockMultipartFile("file", "test.xlsx", EXCEL_TYPE,
"test content".getBytes(StandardCharsets.UTF_8));
yamlFile = new MockMultipartFile("file", "test.yaml", YAML_TYPE,
"test content".getBytes(StandardCharsets.UTF_8));
emptyFile = new MockMultipartFile("file", "", null, (byte[]) null);
}
@@ -40,6 +40,7 @@ import org.springframework.util.ResourceUtils;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.LinkedList;
@@ -103,9 +104,10 @@ public class DockerMonitorE2eTest extends AbstractCollectE2eTest {
private void sendJsonResponse(HttpExchange exchange, String response) throws IOException {
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(200, response.getBytes().length);
final byte[] array = response.getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, array.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(response.getBytes());
os.write(array);
}
}
@@ -19,6 +19,8 @@ package org.apache.hertzbeat.manager.controller;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.hertzbeat.common.constants.CommonConstants;
@@ -62,7 +64,7 @@ class PluginControllerTest {
"jarFile",
"plugin-test.jar",
"application/java-archive",
"This is the file content".getBytes()
"This is the file content".getBytes(StandardCharsets.UTF_8)
);
this.mockMvc.perform(MockMvcRequestBuilders.multipart("/api/plugin")
@@ -32,6 +32,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.hertzbeat.manager.service.impl.AbstractImExportServiceImpl;
import org.apache.hertzbeat.manager.service.impl.JsonImExportServiceImpl;
@@ -66,7 +67,7 @@ class JsonImExportServiceTest {
void testParseImport() throws IOException {
String json = "[{}]";
ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes());
ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
AbstractImExportServiceImpl.MonitorDTO monitorDTO = new AbstractImExportServiceImpl.MonitorDTO();
@@ -28,6 +28,8 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
@@ -87,7 +89,9 @@ class PluginServiceTest {
PluginServiceImpl service = spy(pluginService);
doReturn(metadata).when(service).validateJarFile(any());
MockMultipartFile mockFile = new MockMultipartFile("file", "test-plugin.jar", "application/java-archive", "plugin-content".getBytes());
MockMultipartFile mockFile = new MockMultipartFile(
"file", "test-plugin.jar", "application/java-archive",
"plugin-content".getBytes(StandardCharsets.UTF_8));
PluginUpload pluginUpload = new PluginUpload(mockFile, "Test Plugin", true);
when(metadataDao.save(any(PluginMetadata.class))).thenReturn(new PluginMetadata());
@@ -49,7 +49,7 @@ public class LocalFileStorageServiceImpl implements FileStorageService {
// Define the file path
File localFile = new File(directory,fileName);
try (FileOutputStream outputStream = new FileOutputStream(localFile)) {
outputStream.write(file.getBytes());
outputStream.write(file.getBytes(StandardCharsets.UTF_8));
}
// Return the file URL (local file path in this case)
@@ -132,7 +132,7 @@ class VersionControllerTest {
when(versionService.upload(any(), any())).thenReturn(true);
mockMvc.perform(multipart("/version/upload")
.file("file", "dummy content".getBytes())
.file("file", "dummy content".getBytes(StandardCharsets.UTF_8))
.param("templateDto", templateDtoJson))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(0));
@@ -132,7 +132,7 @@ class VersionDOControllerTest {
when(versionService.upload(any(), any())).thenReturn(true);
mockMvc.perform(multipart("/version/upload")
.file("file", "dummy content".getBytes())
.file("file", "dummy content".getBytes(StandardCharsets.UTF_8))
.param("templateDto", templateDtoJson))
.andExpect(status().isOk())
.andExpect(jsonPath("$.msg").value("upload success"));