Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 23, 2025
Merged
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 @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -122,7 +123,7 @@ private static Map<String, String> execCmdAndParseResult(TelnetClient telnetClie
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -128,16 +129,16 @@ private void send(OutputStream out, WebsocketProtocol websocketProtocol) throws
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -406,7 +407,7 @@ public MetricsData build() {
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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -86,7 +87,7 @@ void testDeserializeWithBytes() {
@Test
void testDeserializeWithInvalidBytes() {

byte[] invalidBytes = "invalid data".getBytes();
byte[] invalidBytes = "invalid data".getBytes(StandardCharsets.UTF_8);

assertThrows(RuntimeException.class, () -> deserializer.deserialize("", invalidBytes));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;

import java.nio.charset.StandardCharsets;

/**
* test case for {@link FileUtil}.
*/
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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;
Expand Down Expand Up @@ -103,9 +104,10 @@ private String loadResponseFromFile(String resourcePath) throws Exception {

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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

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;
Expand Down Expand Up @@ -62,7 +64,7 @@ void uploadNewPlugin() throws Exception {
"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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void setUp() {
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
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;
Expand Down Expand Up @@ -87,7 +89,9 @@ void testSavePlugin() {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void uploadFile(MultipartFile file, String path, String fileName) {
// 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void testUploadVersion_ValidInput() throws Exception {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void testUploadVersion_ValidInput() throws Exception {
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"));
Expand Down
Loading