1 //
2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.websocket.api.extensions;
20
21 import java.nio.ByteBuffer;
22
23 /**
24 * An immutable websocket frame.
25 */
26 public interface Frame
27 {
28 public static enum Type
29 {
30 CONTINUATION((byte)0x00),
31 TEXT((byte)0x01),
32 BINARY((byte)0x02),
33 CLOSE((byte)0x08),
34 PING((byte)0x09),
35 PONG((byte)0x0A);
36
37 public static Type from(byte op)
38 {
39 for (Type type : values())
40 {
41 if (type.opcode == op)
42 {
43 return type;
44 }
45 }
46 throw new IllegalArgumentException("OpCode " + op + " is not a valid Frame.Type");
47 }
48
49 private byte opcode;
50
51 private Type(byte code)
52 {
53 this.opcode = code;
54 }
55
56 public byte getOpCode()
57 {
58 return opcode;
59 }
60
61 public boolean isControl()
62 {
63 return (opcode >= CLOSE.getOpCode());
64 }
65
66 public boolean isData()
67 {
68 return (opcode == TEXT.getOpCode()) | (opcode == BINARY.getOpCode());
69 }
70
71 public boolean isContinuation()
72 {
73 return opcode == CONTINUATION.getOpCode();
74 }
75
76 @Override
77 public String toString()
78 {
79 return this.name();
80 }
81 }
82
83 public byte[] getMask();
84
85 public byte getOpCode();
86
87 public ByteBuffer getPayload();
88
89 /**
90 * The original payload length ({@link ByteBuffer#remaining()})
91 *
92 * @return the original payload length ({@link ByteBuffer#remaining()})
93 */
94 public int getPayloadLength();
95
96 public Type getType();
97
98 public boolean hasPayload();
99
100 public boolean isFin();
101
102 /**
103 * Same as {@link #isFin()}
104 *
105 * @return true if final frame.
106 * @deprecated use {@link #isFin()} instead
107 */
108 @Deprecated
109 public boolean isLast();
110
111 public boolean isMasked();
112
113 public boolean isRsv1();
114
115 public boolean isRsv2();
116
117 public boolean isRsv3();
118
119 }