Skip to content
Draft
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
7 changes: 7 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.0-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
248 changes: 248 additions & 0 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 17 additions & 15 deletions src/main/java/org/java_websocket/drafts/Draft_6455.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.enums.CloseHandshakeType;
import org.java_websocket.enums.HandshakeState;
Expand Down Expand Up @@ -105,6 +104,13 @@ public class Draft_6455 extends Draft {
*/
private static final String CONNECTION = "Connection";

/**
* DateTimeFormatter for HTTP date format (thread-safe)
*/
private static final DateTimeFormatter HTTP_DATE_FORMAT =
DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.US)
.withZone(ZoneId.of("GMT"));

/**
* Logger instance
*
Expand Down Expand Up @@ -723,7 +729,7 @@ private int getSizeBytes(ByteBuffer mes) {
@Override
public List<Framedata> translateFrame(ByteBuffer buffer) throws InvalidDataException {
while (true) {
List<Framedata> frames = new LinkedList<>();
List<Framedata> frames = new ArrayList<>();
Framedata cur;
if (incompleteframe != null) {
// complete an incomplete frame
Expand Down Expand Up @@ -816,11 +822,7 @@ public void reset() {
* @return the server time
*/
private String getServerTime() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat.format(calendar.getTime());
return HTTP_DATE_FORMAT.format(ZonedDateTime.now(ZoneId.of("GMT")));
}

/**
Expand Down Expand Up @@ -1114,15 +1116,15 @@ public CloseHandshakeType getCloseHandshakeType() {

@Override
public String toString() {
String result = super.toString();
StringBuilder result = new StringBuilder(super.toString());
if (getExtension() != null) {
result += " extension: " + getExtension().toString();
result.append(" extension: ").append(getExtension().toString());
}
if (getProtocol() != null) {
result += " protocol: " + getProtocol().toString();
result.append(" protocol: ").append(getProtocol().toString());
}
result += " max frame size: " + this.maxFrameSize;
return result;
result.append(" max frame size: ").append(this.maxFrameSize);
return result.toString();
}

@Override
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/org/java_websocket/util/ByteBufferUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
*/
public class ByteBufferUtils {

/**
* Shared empty ByteBuffer to avoid repeated allocations.
* Safe to share because it has zero capacity and duplicate() provides independent position/limit state.
*/
private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.allocate(0);

/**
* Private constructor for static class
*/
Expand All @@ -52,22 +58,25 @@ public static int transferByteBuffer(ByteBuffer source, ByteBuffer dest) {
int fremain = source.remaining();
int toremain = dest.remaining();
if (fremain > toremain) {
int limit = Math.min(fremain, toremain);
source.limit(limit);
// We know fremain > toremain, so no need for Math.min
int originalLimit = source.limit();
source.limit(source.position() + toremain);
dest.put(source);
return limit;
source.limit(originalLimit);
return toremain;
} else {
dest.put(source);
return fremain;
}
}

/**
* Get a ByteBuffer with zero capacity
* Get a ByteBuffer with zero capacity. Returns a duplicate of a shared empty buffer
* for efficiency - each duplicate has independent position/limit/mark state.
*
* @return empty ByteBuffer
* @return empty ByteBuffer with zero capacity
*/
public static ByteBuffer getEmptyByteBuffer() {
return ByteBuffer.allocate(0);
return EMPTY_BYTE_BUFFER.duplicate();
}
}