Class WebSocketFrame

java.lang.Object
dev.relism.flash.websocket.WebSocketFrame

public final class WebSocketFrame extends Object
Zero-copy view over WebSocketSession's read buffer. One instance allocated per connection, reset per frame — zero steady-state allocation.

Lifetime contract: valid ONLY within the synchronous scope of WebSocketHandler.onMessage(WebSocketSession, WebSocketFrame). The underlying buffer is owned by WebSocketSession and will be overwritten on the next frame read. Never retain a reference beyond the callback; use copyPayload() when data must outlive the call.

Debug safety: buffer() is package-private to prevent accidental cross-boundary access. External consumers must go through copyPayload() or process in-place within the callback.

  • Field Details

  • Constructor Details

    • WebSocketFrame

      public WebSocketFrame()
  • Method Details

    • opcode

      public byte opcode()
    • isFin

      public boolean isFin()
    • payloadOffset

      public int payloadOffset()
    • payloadLength

      public int payloadLength()
    • generation

      public int generation()
      Returns the generation counter at the moment of this call. Store it at callback entry and compare later to detect stale retention:
      
         int gen = frame.generation();
         executor.submit(() -> {
             assert frame.generation() == gen : "frame buffer was recycled!";
         });
       
    • copyPayload

      public byte[] copyPayload()
      Copies the payload into a fresh array. Allocates — O(n) in payload size. Use only when data must outlive the onMessage callback or be handed off to another thread. For payloads > a few KB consider wrapping the result in a pooled ByteBuffer to avoid GC pressure.