Search in sources :

Example 1 with ByteArrayOutputStreamPayload

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)));
}
Also used : ByteArrayOutputStreamPayload(com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload)

Example 2 with ByteArrayOutputStreamPayload

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]);
}
Also used : ByteArrayWritableByteChannel(com.ctrip.xpipe.payload.ByteArrayWritableByteChannel) ByteArrayOutputStreamPayload(com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload) Test(org.junit.Test)

Example 3 with ByteArrayOutputStreamPayload

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 })));
}
Also used : RedisError(com.ctrip.xpipe.redis.core.protocal.error.RedisError) ByteArrayOutputStreamPayload(com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload) AbstractRedisTest(com.ctrip.xpipe.redis.core.AbstractRedisTest) Test(org.junit.Test)

Example 4 with ByteArrayOutputStreamPayload

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;
}
Also used : ByteArrayOutputStreamPayload(com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload) RedisRuntimeException(com.ctrip.xpipe.redis.core.exception.RedisRuntimeException)

Example 5 with ByteArrayOutputStreamPayload

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();
}
Also used : StringInOutPayload(com.ctrip.xpipe.payload.StringInOutPayload) ByteArrayWritableByteChannel(com.ctrip.xpipe.payload.ByteArrayWritableByteChannel) ByteArrayOutputStreamPayload(com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload) IOException(java.io.IOException)

Aggregations

ByteArrayOutputStreamPayload (com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload)6 ByteArrayWritableByteChannel (com.ctrip.xpipe.payload.ByteArrayWritableByteChannel)2 Test (org.junit.Test)2 XpipeRuntimeException (com.ctrip.xpipe.exception.XpipeRuntimeException)1 StringInOutPayload (com.ctrip.xpipe.payload.StringInOutPayload)1 AbstractRedisTest (com.ctrip.xpipe.redis.core.AbstractRedisTest)1 RedisRuntimeException (com.ctrip.xpipe.redis.core.exception.RedisRuntimeException)1 RedisError (com.ctrip.xpipe.redis.core.protocal.error.RedisError)1 ReplicationStoreMeta (com.ctrip.xpipe.redis.core.store.ReplicationStoreMeta)1 IOException (java.io.IOException)1