1 //
2 // ========================================================================
3 // Copyright (c) 1995-2014 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.common.frames;
20
21 import org.eclipse.jetty.io.ByteBufferPool;
22 import org.eclipse.jetty.websocket.api.extensions.Frame;
23 import org.eclipse.jetty.websocket.common.OpCode;
24 import org.eclipse.jetty.websocket.common.WebSocketFrame;
25
26 /**
27 * A Data Frame
28 */
29 public class DataFrame extends WebSocketFrame
30 {
31 private ByteBufferPool pool;
32
33 protected DataFrame(byte opcode)
34 {
35 super(opcode);
36 }
37
38 /**
39 * Construct new DataFrame based on headers of provided frame.
40 * <p>
41 * Useful for when working in extensions and a new frame needs to be created.
42 */
43 public DataFrame(Frame basedOn)
44 {
45 this(basedOn,false);
46 }
47
48 /**
49 * Construct new DataFrame based on headers of provided frame, overriding for continuations if needed.
50 * <p>
51 * Useful for when working in extensions and a new frame needs to be created.
52 */
53 public DataFrame(Frame basedOn, boolean continuation)
54 {
55 super(basedOn.getOpCode());
56 copyHeaders(basedOn);
57 if (continuation)
58 {
59 setOpCode(OpCode.CONTINUATION);
60 }
61 }
62
63 @Override
64 public void assertValid()
65 {
66 /* no extra validation for data frames (yet) here */
67 }
68
69 @Override
70 public boolean isControlFrame()
71 {
72 return false;
73 }
74
75 @Override
76 public boolean isDataFrame()
77 {
78 return true;
79 }
80
81 public void releaseBuffer()
82 {
83 if (pool!=null)
84 {
85 pool.release(this.data);
86 this.data=null;
87 }
88 }
89
90 /**
91 * Set the data frame to continuation mode
92 */
93 public void setIsContinuation()
94 {
95 setOpCode(OpCode.CONTINUATION);
96 }
97
98 /**
99 * Sets the buffer pool used for the payload
100 */
101 public void setBufferPool(ByteBufferPool pool)
102 {
103 this.pool = pool;
104 }
105 }