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]);
}
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();
}
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);
}
}
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());
}
Aggregations