View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.socket;
18  
19  import java.nio.ByteBuffer;
20  
21  /**
22   * A binary WebSocket message.
23   *
24   * @author Rossen Stoyanchev
25   * @since 4.0
26   */
27  public final class BinaryMessage extends AbstractWebSocketMessage<ByteBuffer> {
28  
29  	/**
30  	 * Create a new binary WebSocket message with the given ByteBuffer payload.
31  	 * @param payload the non-null payload
32  	 */
33  	public BinaryMessage(ByteBuffer payload) {
34  		super(payload, true);
35  	}
36  
37  	/**
38  	 * Create a new binary WebSocket message with the given payload representing the
39  	 * full or partial message content. When the {@code isLast} boolean flag is set
40  	 * to {@code false} the message is sent as partial content and more partial
41  	 * messages will be expected until the boolean flag is set to {@code true}.
42  	 * @param payload the non-null payload
43  	 * @param isLast if the message is the last of a series of partial messages
44  	 */
45  	public BinaryMessage(ByteBuffer payload, boolean isLast) {
46  		super(payload, isLast);
47  	}
48  
49  	/**
50  	 * Create a new binary WebSocket message with the given byte[] payload.
51  	 * @param payload a non-null payload; note that this value is not copied so care
52  	 * must be taken not to modify the array.
53  	 */
54  	public BinaryMessage(byte[] payload) {
55  		this(payload, true);
56  	}
57  
58  	/**
59  	 * Create a new binary WebSocket message with the given byte[] payload representing
60  	 * the full or partial message content. When the {@code isLast} boolean flag is set
61  	 * to {@code false} the message is sent as partial content and more partial
62  	 * messages will be expected until the boolean flag is set to {@code true}.
63  	 * @param payload a non-null payload; note that this value is not copied so care
64  	 * must be taken not to modify the array.
65  	 * @param isLast if the message is the last of a series of partial messages
66  	 */
67  	public BinaryMessage(byte[] payload, boolean isLast) {
68  		this(payload, 0, ((payload == null) ? 0 : payload.length), isLast);
69  	}
70  
71  	/**
72  	 * Create a new binary WebSocket message by wrapping an existing byte array.
73  	 * @param payload a non-null payload; note that this value is not copied so care
74  	 * must be taken not to modify the array.
75  	 * @param offset the offset into the array where the payload starts
76  	 * @param length the length of the array considered for the payload
77  	 * @param isLast if the message is the last of a series of partial messages
78  	 */
79  	public BinaryMessage(byte[] payload, int offset, int length, boolean isLast) {
80  		super(payload != null ? ByteBuffer.wrap(payload, offset, length) : null, isLast);
81  	}
82  
83  
84  	@Override
85  	public int getPayloadLength() {
86  		return getPayload().remaining();
87  	}
88  
89  	@Override
90  	protected String toStringPayload() {
91  		return getPayload().toString();
92  	}
93  
94  }