Search in sources :

Example 1 with ByteArrayWritableByteChannel

use of com.ctrip.xpipe.payload.ByteArrayWritableByteChannel 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 2 with ByteArrayWritableByteChannel

use of com.ctrip.xpipe.payload.ByteArrayWritableByteChannel 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)

Example 3 with ByteArrayWritableByteChannel

use of com.ctrip.xpipe.payload.ByteArrayWritableByteChannel in project x-pipe by ctripcorp.

the class AbstractRedisKeeperTest method readFileChannelInfoMessageAsBytes.

protected byte[] readFileChannelInfoMessageAsBytes(ReferenceFileRegion referenceFileRegion) {
    try {
        ByteArrayWritableByteChannel bach = new ByteArrayWritableByteChannel();
        referenceFileRegion.transferTo(bach, 0L);
        return bach.getResult();
    } catch (IOException e) {
        throw new IllegalStateException(String.format("[read]%s", referenceFileRegion), e);
    }
}
Also used : ByteArrayWritableByteChannel(com.ctrip.xpipe.payload.ByteArrayWritableByteChannel) IOException(java.io.IOException)

Example 4 with ByteArrayWritableByteChannel

use of com.ctrip.xpipe.payload.ByteArrayWritableByteChannel in project x-pipe by ctripcorp.

the class AbstractRedisKeeperTest method readRdbFileTilEnd.

protected String readRdbFileTilEnd(RdbStore rdbStore) throws IOException, InterruptedException {
    final ByteArrayWritableByteChannel bachannel = new ByteArrayWritableByteChannel();
    final CountDownLatch latch = new CountDownLatch(1);
    rdbStore.readRdbFile(new RdbFileListener() {

        @Override
        public void setRdbFileInfo(EofType eofType, long rdbFileOffset) {
        }

        @Override
        public void onFileData(ReferenceFileRegion referenceFileRegion) throws IOException {
            if (referenceFileRegion == null) {
                latch.countDown();
                return;
            }
            referenceFileRegion.transferTo(bachannel, 0L);
        }

        @Override
        public boolean isOpen() {
            return true;
        }

        @Override
        public void exception(Exception e) {
            latch.countDown();
        }

        @Override
        public void beforeFileData() {
        }
    });
    latch.await(5, TimeUnit.SECONDS);
    return new String(bachannel.getResult());
}
Also used : ByteArrayWritableByteChannel(com.ctrip.xpipe.payload.ByteArrayWritableByteChannel) EofType(com.ctrip.xpipe.redis.core.protocal.protocal.EofType) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ReferenceFileRegion(com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)

Aggregations

ByteArrayWritableByteChannel (com.ctrip.xpipe.payload.ByteArrayWritableByteChannel)4 IOException (java.io.IOException)3 ByteArrayOutputStreamPayload (com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload)2 ReferenceFileRegion (com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)1 StringInOutPayload (com.ctrip.xpipe.payload.StringInOutPayload)1 EofType (com.ctrip.xpipe.redis.core.protocal.protocal.EofType)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1