Skip to content

Commit 059b0ef

Browse files
committed
refactor: Trim the last of the gzip code
Collapses the byte-body content-type resolution into two expressions and inlines the remaining single-use string constants.
1 parent 89c523b commit 059b0ef

2 files changed

Lines changed: 5 additions & 14 deletions

File tree

src/main/java/com/retailsvc/http/internal/ResponseCompression.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
/** Response content-coding policy, and the gzip primitives the renderer writes through. */
99
public final class ResponseCompression {
1010

11-
private static final String TEXT_PREFIX = "text/";
12-
private static final String EVENT_STREAM = "text/event-stream";
13-
1411
private static final Set<String> COMPRESSIBLE_TYPES =
1512
Set.of(
1613
"application/json",
@@ -35,8 +32,8 @@ public static boolean isCompressible(String contentType) {
3532
return false;
3633
}
3734
String mediaType = ContentTypeHeader.mediaType(contentType);
38-
if (mediaType.startsWith(TEXT_PREFIX)) {
39-
return !EVENT_STREAM.equals(mediaType);
35+
if (mediaType.startsWith("text/")) {
36+
return !"text/event-stream".equals(mediaType);
4037
}
4138
if (mediaType.endsWith("+json") || mediaType.endsWith("+xml") || mediaType.endsWith("+yaml")) {
4239
return true;

src/main/java/com/retailsvc/http/internal/ResponseRenderer.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,9 @@ private static long declaredLength(Headers headers) {
129129
private void renderBytes(
130130
HttpExchange exchange, Headers headers, int status, String contentType, Object body)
131131
throws IOException {
132-
byte[] bytes;
133-
String effectiveContentType;
134-
if (body instanceof byte[] raw) {
135-
bytes = raw;
136-
effectiveContentType = contentType != null ? contentType : OCTET_STREAM;
137-
} else {
138-
effectiveContentType = contentType != null ? contentType : DEFAULT_JSON;
139-
bytes = serialize(body, effectiveContentType);
140-
}
132+
String effectiveContentType =
133+
contentType != null ? contentType : (body instanceof byte[] ? OCTET_STREAM : DEFAULT_JSON);
134+
byte[] bytes = body instanceof byte[] raw ? raw : serialize(body, effectiveContentType);
141135
defaultContentType(headers, effectiveContentType);
142136
byte[] payload = maybeCompress(exchange, headers, status, effectiveContentType, bytes);
143137
exchange.sendResponseHeaders(status, payload.length == 0 ? -1 : payload.length);

0 commit comments

Comments
 (0)