Skip to content

Commit 877151a

Browse files
Address code review: fix OutputStream resource handling and remove redundant cast
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
1 parent 9a60956 commit 877151a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/main/java/dansplugins/activitytracker/services/DiscordWebhookService.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,14 @@ private void sendWebhookMessage(String content) {
8787
connection.setDoOutput(true);
8888

8989
String jsonPayload = "{\"content\": \"" + escapeJson(content) + "\"}";
90+
byte[] input = jsonPayload.getBytes(StandardCharsets.UTF_8);
9091

9192
OutputStream os = connection.getOutputStream();
92-
byte[] input = jsonPayload.getBytes(StandardCharsets.UTF_8);
93-
os.write(input, 0, input.length);
94-
os.close();
93+
try {
94+
os.write(input, 0, input.length);
95+
} finally {
96+
os.close();
97+
}
9598

9699
int responseCode = connection.getResponseCode();
97100
if (responseCode < 200 || responseCode >= 300) {
@@ -132,7 +135,7 @@ private String escapeJson(String text) {
132135
break;
133136
default:
134137
if (c < 0x20) {
135-
sb.append(String.format("\\u%04x", (int) c));
138+
sb.append(String.format("\\u%04x", c));
136139
} else {
137140
sb.append(c);
138141
}

0 commit comments

Comments
 (0)