001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.socket; 018 019import java.nio.ByteBuffer; 020 021/** 022 * A binary WebSocket message. 023 * 024 * @author Rossen Stoyanchev 025 * @since 4.0 026 */ 027public final class BinaryMessage extends AbstractWebSocketMessage<ByteBuffer> { 028 029 /** 030 * Create a new binary WebSocket message with the given ByteBuffer payload. 031 * @param payload the non-null payload 032 */ 033 public BinaryMessage(ByteBuffer payload) { 034 super(payload, true); 035 } 036 037 /** 038 * Create a new binary WebSocket message with the given payload representing the 039 * full or partial message content. When the {@code isLast} boolean flag is set 040 * to {@code false} the message is sent as partial content and more partial 041 * messages will be expected until the boolean flag is set to {@code true}. 042 * @param payload the non-null payload 043 * @param isLast if the message is the last of a series of partial messages 044 */ 045 public BinaryMessage(ByteBuffer payload, boolean isLast) { 046 super(payload, isLast); 047 } 048 049 /** 050 * Create a new binary WebSocket message with the given byte[] payload. 051 * @param payload a non-null payload; note that this value is not copied so care 052 * must be taken not to modify the array. 053 */ 054 public BinaryMessage(byte[] payload) { 055 this(payload, true); 056 } 057 058 /** 059 * Create a new binary WebSocket message with the given byte[] payload representing 060 * the full or partial message content. When the {@code isLast} boolean flag is set 061 * to {@code false} the message is sent as partial content and more partial 062 * messages will be expected until the boolean flag is set to {@code true}. 063 * @param payload a non-null payload; note that this value is not copied so care 064 * must be taken not to modify the array. 065 * @param isLast if the message is the last of a series of partial messages 066 */ 067 public BinaryMessage(byte[] payload, boolean isLast) { 068 this(payload, 0, ((payload == null) ? 0 : payload.length), isLast); 069 } 070 071 /** 072 * Create a new binary WebSocket message by wrapping an existing byte array. 073 * @param payload a non-null payload; note that this value is not copied so care 074 * must be taken not to modify the array. 075 * @param offset the offset into the array where the payload starts 076 * @param length the length of the array considered for the payload 077 * @param isLast if the message is the last of a series of partial messages 078 */ 079 public BinaryMessage(byte[] payload, int offset, int length, boolean isLast) { 080 super(payload != null ? ByteBuffer.wrap(payload, offset, length) : null, isLast); 081 } 082 083 084 @Override 085 public int getPayloadLength() { 086 return getPayload().remaining(); 087 } 088 089 @Override 090 protected String toStringPayload() { 091 return getPayload().toString(); 092 } 093 094}