All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swim.ws.WsEncoder Maven / Gradle / Ivy

// Copyright 2015-2019 SWIM.AI inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package swim.ws;

import swim.codec.Encoder;
import swim.codec.EncoderException;
import swim.codec.OutputBuffer;

public abstract class WsEncoder {
  public abstract boolean isMasked();

  public abstract void maskingKey(byte[] maskingKey);

  public  Encoder> frameEncoder(WsFrame frame) {
    final WsOpcode opcode = frame.opcode();
    switch (opcode) {
      case CONTINUATION: return Encoder.error(new EncoderException("invalid opcode: " + opcode));
      case TEXT: return textFrameEncoder(frame);
      case BINARY: return binaryFrameEncoder(frame);
      case CLOSE: return closeFrameEncoder(frame);
      case PING: return pingFrameEncoder(frame);
      case PONG: return pongFrameEncoder(frame);
      default: return Encoder.error(new EncoderException("reserved opcode: " + opcode));
    }
  }

  public  Encoder> encodeFrame(WsFrame frame, OutputBuffer output) {
    final WsOpcode opcode = frame.opcode();
    switch (opcode) {
      case CONTINUATION: return Encoder.error(new EncoderException("invalid opcode: " + opcode));
      case TEXT: return encodeTextFrame(frame, output);
      case BINARY: return encodeBinaryFrame(frame, output);
      case CLOSE: return encodeCloseFrame(frame, output);
      case PING: return encodePingFrame(frame, output);
      case PONG: return encodePongFrame(frame, output);
      default: return Encoder.error(new EncoderException("reserved opcode: " + opcode));
    }
  }

  public  Encoder> textFrameEncoder(WsFrame frame) {
    return new WsFrameEncoder(this, frame);
  }

  public  Encoder> encodeTextFrame(WsFrame frame, OutputBuffer output) {
    return WsFrameEncoder.encode(output, this, frame);
  }

  public  Encoder> binaryFrameEncoder(WsFrame frame) {
    return new WsFrameEncoder(this, frame);
  }

  public  Encoder> encodeBinaryFrame(WsFrame frame, OutputBuffer output) {
    return WsFrameEncoder.encode(output, this, frame);
  }

  public  Encoder> closeFrameEncoder(WsFrame frame) {
    return new WsFrameEncoder(this, frame);
  }

  public  Encoder> encodeCloseFrame(WsFrame frame, OutputBuffer output) {
    return WsFrameEncoder.encode(output, this, frame);
  }

  public  Encoder> pingFrameEncoder(WsFrame frame) {
    return new WsFrameEncoder(this, frame);
  }

  public  Encoder> encodePingFrame(WsFrame frame, OutputBuffer output) {
    return WsFrameEncoder.encode(output, this, frame);
  }

  public  Encoder> pongFrameEncoder(WsFrame frame) {
    return new WsFrameEncoder(this, frame);
  }

  public  Encoder> encodePongFrame(WsFrame frame, OutputBuffer output) {
    return WsFrameEncoder.encode(output, this, frame);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy