use of com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload in project x-pipe by ctripcorp.
the class AbstractRedisCommand method payloadToString.
protected String payloadToString(Object payload) {
if (payload instanceof String) {
logger.debug("[payloadToString]{}", payload);
return (String) payload;
}
if (payload instanceof ByteArrayOutputStreamPayload) {
ByteArrayOutputStreamPayload baous = (ByteArrayOutputStreamPayload) payload;
String result = new String(baous.getBytes(), Codec.defaultCharset);
logger.debug("[payloadToString]{}", result);
return result;
}
String clazz = payload == null ? "null" : payload.getClass().getSimpleName();
throw new IllegalStateException(String.format("unknown payload %s:%s", clazz, StringUtil.toString(payload)));
}
use of com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload in project x-pipe by ctripcorp.
the class ArrayParserTest method testArray.
@Test
public void testArray() throws IOException {
String str1 = randomString();
String str2 = randomString();
Long long3 = 1024L;
String[] data = new String[] { "*3\r\n", "+" + str1 + "\r\n", "$" + str2.length() + "\r\n" + str2 + "\r\n", ":" + long3 + "\r\n" };
ArrayParser resultParser = (ArrayParser) parse(arrayParser, data);
Object[] result = resultParser.getPayload();
Assert.assertEquals(3, result.length);
Assert.assertEquals(str1, result[0]);
ByteArrayOutputStreamPayload bap = (ByteArrayOutputStreamPayload) result[1];
ByteArrayWritableByteChannel channel = new ByteArrayWritableByteChannel();
bap.out(channel);
Assert.assertEquals(str2, new String(channel.getResult()));
Assert.assertEquals(long3, result[2]);
}
use of com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload in project x-pipe by ctripcorp.
the class ParserManagerTest method test.
@Test
public void test() {
Assert.assertEquals(":1\r\n", ByteBufUtils.readToString(ParserManager.parse(1L)));
Assert.assertEquals("+nihao\r\n", ByteBufUtils.readToString(ParserManager.parse("nihao")));
Assert.assertEquals("-error\r\n", ByteBufUtils.readToString(ParserManager.parse(new RedisError("error"))));
Assert.assertEquals("*2\r\n+str\r\n:1\r\n", ByteBufUtils.readToString(ParserManager.parse(new Object[] { "str", 1L })));
Assert.assertEquals("$5\r\nnihao\r\n", ByteBufUtils.readToString(ParserManager.parse(new ByteArrayOutputStreamPayload("nihao"))));
int a = 1;
Assert.assertEquals("*2\r\n+str\r\n:1\r\n", ByteBufUtils.readToString(ParserManager.parse(new Object[] { "str", a })));
}
use of com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload in project x-pipe by ctripcorp.
the class DefaultRedisClient method handleArray.
private String[] handleArray(Object[] result) {
String[] strArray = new String[result.length];
int index = 0;
for (Object param : result) {
if (param instanceof String) {
strArray[index] = (String) param;
} else if (param instanceof ByteArrayOutputStreamPayload) {
byte[] bytes = ((ByteArrayOutputStreamPayload) param).getBytes();
strArray[index] = new String(bytes, Codec.defaultCharset);
} else {
throw new RedisRuntimeException("request unkonwn, can not be transformed to string!");
}
index++;
}
return strArray;
}
use of com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload in project x-pipe by ctripcorp.
the class BulkStringParser method getWriteByteBuf.
@Override
protected ByteBuf getWriteByteBuf() {
if (payload == null) {
if (logger.isInfoEnabled()) {
logger.info("[getWriteBytes][payload null]");
}
return Unpooled.wrappedBuffer(new byte[0]);
}
if ((payload instanceof StringInOutPayload) || (payload instanceof ByteArrayOutputStreamPayload)) {
try {
ByteArrayWritableByteChannel channel = new ByteArrayWritableByteChannel();
payload.out(channel);
byte[] content = channel.getResult();
String length = String.valueOf((char) DOLLAR_BYTE) + content.length + RedisClientProtocol.CRLF;
return Unpooled.wrappedBuffer(length.getBytes(), content, RedisClientProtocol.CRLF.getBytes());
} catch (IOException e) {
logger.error("[getWriteBytes]", e);
return Unpooled.wrappedBuffer(new byte[0]);
}
}
throw new UnsupportedOperationException();
}
Aggregations