Skip to content

Commit 1212576

Browse files
Copilotrbri
andauthored
Address code review feedback: fix typos, improve error message, optimize binary accumulation
- Rename closeIncommingSession -> closeIncomingSession in interface and all callers - Fix 'WebSockt' typos in Javadoc - Update error message to accurately describe supported content types - Use ByteArrayOutputStream for efficient binary message accumulation Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/b3db8ccb-ee0a-436b-8c39-85fd25db4747 Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
1 parent 9228f3e commit 1212576

5 files changed

Lines changed: 18 additions & 28 deletions

File tree

javac.20260412_170050.args

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/java/org/htmlunit/javascript/host/WebSocket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public void close() throws IOException {
421421
public void close(final Object code, final Object reason) {
422422
if (readyState_ != CLOSED) {
423423
try {
424-
webSocketImpl_.closeIncommingSession();
424+
webSocketImpl_.closeIncomingSession();
425425
}
426426
catch (final Throwable e) {
427427
LOG.error("WS close error - incomingSession_.close() failed", e);

src/main/java/org/htmlunit/websocket/JdkWebSocketAdapter.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package org.htmlunit.websocket;
1616

17+
import java.io.ByteArrayOutputStream;
1718
import java.io.IOException;
1819
import java.net.CookieHandler;
1920
import java.net.URI;
@@ -135,7 +136,7 @@ else if (content instanceof ByteBuffer buffer) {
135136
}
136137
else {
137138
throw new IllegalStateException(
138-
"Not Yet Implemented: WebSocket.send() was used to send non-string value");
139+
"Unsupported content type for WebSocket.send(): expected String or ByteBuffer");
139140
}
140141
}
141142
catch (final IllegalStateException e) {
@@ -150,7 +151,7 @@ else if (content instanceof ByteBuffer buffer) {
150151
* {@inheritDoc}
151152
*/
152153
@Override
153-
public void closeIncommingSession() {
154+
public void closeIncomingSession() {
154155
if (incomingSession_ != null) {
155156
incomingSession_.sendClose(java.net.http.WebSocket.NORMAL_CLOSURE, "").join();
156157
}
@@ -239,7 +240,7 @@ public void put(final URI uri,
239240
private class JdkWebSocketListenerImpl implements java.net.http.WebSocket.Listener {
240241

241242
private StringBuilder textAccumulator_;
242-
private ByteBuffer binaryAccumulator_;
243+
private ByteArrayOutputStream binaryAccumulator_;
243244

244245
JdkWebSocketListenerImpl() {
245246
super();
@@ -274,22 +275,20 @@ public CompletionStage<?> onText(final java.net.http.WebSocket webSocket,
274275
public CompletionStage<?> onBinary(final java.net.http.WebSocket webSocket,
275276
final ByteBuffer data, final boolean last) {
276277
if (binaryAccumulator_ == null) {
277-
binaryAccumulator_ = ByteBuffer.allocate(data.remaining());
278-
binaryAccumulator_.put(data);
278+
binaryAccumulator_ = new ByteArrayOutputStream();
279+
}
280+
if (data.hasArray()) {
281+
binaryAccumulator_.write(data.array(),
282+
data.arrayOffset() + data.position(), data.remaining());
279283
}
280284
else {
281-
final ByteBuffer newBuffer = ByteBuffer.allocate(
282-
binaryAccumulator_.position() + data.remaining());
283-
binaryAccumulator_.flip();
284-
newBuffer.put(binaryAccumulator_);
285-
newBuffer.put(data);
286-
binaryAccumulator_ = newBuffer;
285+
final byte[] temp = new byte[data.remaining()];
286+
data.get(temp);
287+
binaryAccumulator_.write(temp, 0, temp.length);
287288
}
288289

289290
if (last) {
290-
binaryAccumulator_.flip();
291-
final byte[] bytes = new byte[binaryAccumulator_.remaining()];
292-
binaryAccumulator_.get(bytes);
291+
final byte[] bytes = binaryAccumulator_.toByteArray();
293292
binaryAccumulator_ = null;
294293
listener_.onWebSocketBinary(bytes, 0, bytes.length);
295294
}

src/main/java/org/htmlunit/websocket/WebSocketAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.net.URI;
1919

2020
/**
21-
* Helper to have no direct dependency to the WebSockt client
21+
* Helper to have no direct dependency to the WebSocket client
2222
* implementation used by HtmlUnit.
2323
*
2424
* @author Ronald Brill
@@ -49,11 +49,11 @@ public interface WebSocketAdapter {
4949
void send(Object content) throws IOException;
5050

5151
/**
52-
* Close the incomming session.
52+
* Close the incoming session.
5353
*
5454
* @throws Exception in case of error
5555
*/
56-
void closeIncommingSession() throws Exception;
56+
void closeIncomingSession() throws Exception;
5757

5858
/**
5959
* Close the outgoing session.

src/main/java/org/htmlunit/websocket/WebSocketListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package org.htmlunit.websocket;
1616

1717
/**
18-
* Helper to have no direct dependency to the WebSockt client
18+
* Helper to have no direct dependency to the WebSocket client
1919
* implementation used by HtmlUnit.
2020
*
2121
* @author Ronald Brill

0 commit comments

Comments
 (0)