DefaultRedisClient extends AbstractObservable implements RedisClient { @Override public String[] readCommands(ByteBuf byteBuf) { while(true){ switch(commandState){ case READ_SIGN: if(!hasDataRead(byteBuf)){ return null; } int readIndex = byteBuf.readerIndex(); byte sign = byteBuf.getByte(readIndex); if(sign == RedisClientProtocol.ASTERISK_BYTE){ redisClientProtocol = new ArrayParser(); }else if(sign == '\n'){ byteBuf.readByte(); return new String[]{"\n"}; }else{ redisClientProtocol = new SimpleStringParser(); } commandState = COMMAND_STATE.READ_COMMANDS; break; case READ_COMMANDS: RedisClientProtocol<?> resultParser = redisClientProtocol.read(byteBuf); if(resultParser == null){ return null; } Object result = resultParser.getPayload(); if(result == null){ return new String[0]; } commandState = COMMAND_STATE.READ_SIGN ; String []ret = null; if(result instanceof String){ ret = handleString((String)result); }else if(result instanceof Object[]){ ret = handleArray((Object[])result); }else{ throw new IllegalStateException("unkonw result array:" + result); } return ret; default: throw new IllegalStateException("unkonwn state:" + commandState); } } } DefaultRedisClient(Channel channel, RedisKeeperServer redisKeeperServer); @Override RedisKeeperServer getRedisKeeperServer(); @Override void setSlaveListeningPort(int port); @Override void capa(CAPA capa); @Override boolean capaOf(CAPA capa); @Override int getSlaveListeningPort(); @Override void setClientIpAddress(String host); @Override String getClientIpAddress(); @Override String toString(); @Override String[] readCommands(ByteBuf byteBuf); @Override String info(); @Override String ip(); @Override void close(); @Override RedisSlave becomeSlave(); @Override Channel channel(); @Override void sendMessage(ByteBuf byteBuf); @Override void sendMessage(byte[] bytes); void addChannelCloseReleaseResources(final Releasable releasable); @Override void setClientEndpoint(Endpoint endpoint); @Override Endpoint getClientEndpoint(); @Override void release(); @Override Set<CAPA> getCapas(); @Override boolean isKeeper(); @Override void setKeeper();  }
@Test public void testReadCommands(){ String []commands = new String[]{"*1\r\n$4\r\nPING\r\n"}; List<String[]> args = readCommands(commands); Assert.assertEquals(1, args.size()); commands = new String[]{"*1\r\n$4\r\nPING", "\r\n"}; args = readCommands(commands); Assert.assertEquals(1, args.size()); commands = new String[]{"*1\r\n$4\r\nPING", "\r\n", "*1\r\n$4\r\nPING\r\n"}; args = readCommands(commands); Assert.assertEquals(2, args.size()); }