2016-04-14 00:30:09 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2016 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you 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 io.netty.handler.codec.redis;
|
|
|
|
|
2018-02-04 01:50:27 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2016-04-12 14:22:41 +02:00
|
|
|
import io.netty.util.internal.UnstableApi;
|
|
|
|
|
2016-04-14 00:30:09 +02:00
|
|
|
/**
|
|
|
|
* Type of <a href="http://redis.io/topics/protocol">RESP (REdis Serialization Protocol)</a>.
|
|
|
|
*/
|
2016-04-12 14:22:41 +02:00
|
|
|
@UnstableApi
|
2016-04-14 00:30:09 +02:00
|
|
|
public enum RedisMessageType {
|
|
|
|
|
2018-02-04 01:50:27 +01:00
|
|
|
INLINE_COMMAND(null, true),
|
2016-04-14 00:30:09 +02:00
|
|
|
SIMPLE_STRING((byte) '+', true),
|
|
|
|
ERROR((byte) '-', true),
|
|
|
|
INTEGER((byte) ':', true),
|
|
|
|
BULK_STRING((byte) '$', false),
|
2018-02-04 01:50:27 +01:00
|
|
|
ARRAY_HEADER((byte) '*', false);
|
2016-04-14 00:30:09 +02:00
|
|
|
|
2018-02-04 01:50:27 +01:00
|
|
|
private final Byte value;
|
2016-04-14 00:30:09 +02:00
|
|
|
private final boolean inline;
|
|
|
|
|
2018-02-04 01:50:27 +01:00
|
|
|
RedisMessageType(Byte value, boolean inline) {
|
2016-04-14 00:30:09 +02:00
|
|
|
this.value = value;
|
|
|
|
this.inline = inline;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 01:50:27 +01:00
|
|
|
* Returns length of this type.
|
2016-04-14 00:30:09 +02:00
|
|
|
*/
|
2018-02-04 01:50:27 +01:00
|
|
|
public int length() {
|
|
|
|
return value != null ? RedisConstants.TYPE_LENGTH : 0;
|
2016-04-14 00:30:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if this type is inline type, or returns {@code false}. If this is {@code true},
|
|
|
|
* this type doesn't have length field.
|
|
|
|
*/
|
|
|
|
public boolean isInline() {
|
|
|
|
return inline;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-04 01:50:27 +01:00
|
|
|
* Determine {@link RedisMessageType} based on the type prefix {@code byte} read from given the buffer.
|
2016-04-14 00:30:09 +02:00
|
|
|
*/
|
2018-02-04 01:50:27 +01:00
|
|
|
public static RedisMessageType readFrom(ByteBuf in, boolean decodeInlineCommands) {
|
|
|
|
final int initialIndex = in.readerIndex();
|
|
|
|
final RedisMessageType type = valueOf(in.readByte());
|
|
|
|
if (type == INLINE_COMMAND) {
|
|
|
|
if (!decodeInlineCommands) {
|
|
|
|
throw new RedisCodecException("Decoding of inline commands is disabled");
|
|
|
|
}
|
|
|
|
// reset index to make content readable again
|
|
|
|
in.readerIndex(initialIndex);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the message type's prefix to the given buffer.
|
|
|
|
*/
|
|
|
|
public void writeTo(ByteBuf out) {
|
|
|
|
if (value == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
out.writeByte(value.byteValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static RedisMessageType valueOf(byte value) {
|
2016-04-14 00:30:09 +02:00
|
|
|
switch (value) {
|
|
|
|
case '+':
|
|
|
|
return SIMPLE_STRING;
|
|
|
|
case '-':
|
|
|
|
return ERROR;
|
|
|
|
case ':':
|
|
|
|
return INTEGER;
|
|
|
|
case '$':
|
|
|
|
return BULK_STRING;
|
|
|
|
case '*':
|
|
|
|
return ARRAY_HEADER;
|
|
|
|
default:
|
2018-02-04 01:50:27 +01:00
|
|
|
return INLINE_COMMAND;
|
2016-04-14 00:30:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|