发送和接收字节数组是不同的

时间:2016-08-17 06:01:37

标签: java spring spring-integration

使用spring集成可以向tcp发送和接收纯文本通信。

但是,
发送字节数组值和接收字节数组的值是不同的。

EchoSerializer> desirialize> extractBytes部分。 转换为不同的inputStream到byteArray时 同时发送一个不同的字符串接收字节数组值始终相同。

有什么问题?请帮帮我。

适配器部件是......

@Value("${tcp.plain.inbound.port}")
private String inboundPort;

@Autowired
private EchoSerializer echoSerializer;

@Bean
public TcpReceivingChannelAdapter plainAdapter() {
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(plainServerFactory());
    adapter.setOutputChannel(inputWithPlain());

    return adapter;
}

@Bean
public TcpSendingMessageHandler plainHandler() {
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(plainServerFactory());

    return handler;
}

@Bean
public AbstractConnectionFactory plainServerFactory() {
    int port = Integer.parseInt(inboundPort);
    TcpNioServerConnectionFactory factory = new TcpNioServerConnectionFactory(port);
    //factory.setBacklog(BACKLOG);
    factory.setTaskExecutor(taskSchedulerWithPlain());
    factory.setLookupHost(false);

    factory.setSerializer(echoSerializer);
    factory.setDeserializer(echoSerializer);

    factory.setSoTcpNoDelay(true);

    return factory;
}

@Bean
public Executor taskSchedulerWithPlain() {
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setPoolSize(PROCESS_SIZE);
    scheduler.setThreadFactory(plainAffinityThreadFactory());
    scheduler.setWaitForTasksToCompleteOnShutdown(true);

    return scheduler;
}

private AffinityThreadFactory plainAffinityThreadFactory() {
    return new AffinityThreadFactory(THREAD_NAME_PREFIX, SAME_CORE, DIFFERENT_SOCKET, ANY);
}

@Bean
public MessageChannel inputWithPlain() {
    return MessageChannels.queue(PROCESS_SIZE).get();
}

@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() {
    return Pollers.fixedRate(FIXED_RATE).get();
}

EchoSerializer方法是......

@Component
public class EchoSerializer extends AbstractByteArraySerializer {

    private final Logger log = LoggerFactory.getLogger(getClass());

    private static final int MAX_MESSAGE_SIZE = 8192;
    private static final String ROOT_TAG_END = "</ROOT>";

    public EchoSerializer() {
        super();
        this.setMaxMessageSize(MAX_MESSAGE_SIZE);
    }

    @Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        byte[] payload = extractBytes(inputStream, MAX_MESSAGE_SIZE);
        log.debug("payload size is : {}", payload.length);

        return payload;
    }

    private byte[] extractBytes(InputStream inputStream, int size) throws IOException {
        byte[] bytes = new byte[size];

        int i = 0;
        int bite = 0;
        StringBuffer sb = new StringBuffer();
        while ( (bite = inputStream.read()) != -1 ) {
            bytes[i] = (byte) bite;
            i++;
            sb.append((char) bite);

            if (-1 < sb.indexOf(ROOT_TAG_END)) {
                break;
            }
        }
        byte[] payload = new byte[i];
        System.arraycopy(bytes, 0, payload, 0, i);

        return payload;
    }

    @Override
    public void serialize(byte[] bytes, OutputStream outputStream) throws IOException {
        outputStream.write(bytes);
        outputStream.flush();
    }

}

发送测试用例是......

SocketFactory socketFactory = (SocketFactory) SSLSocketFactory.getDefault();
Socket socket = (Socket) socketFactory.createSocket(host, PORT_NUM);

OutputStream out = socket.getOutputStream();
InputStream inFromServer = socket.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);

StringBuffer sb = new StringBuffer();

sb.append("<?xml version='1.0' encoding='utf-8'?>" +
        "<ROOT>" +
        "<AAA>aaaaa</AAA>" +
        "<BBB>bbbbb</BBB>" +
        "<CCC>ccccc</CCC>" +
        "<DDD>ddddd</DDD>" +
        "</ROOT>");

byte[] bytes = sb.toString().getBytes();
StringBuffer buffer = new StringBuffer();
for (byte b : bytes) {
    System.out.println((int) b);
    buffer.append(b);
}
System.out.println(buffer);

out.write(sb.toString().getBytes());

发送byteArray是

606312010910832118101114115105111110613949464839321011109911110010511010361391171161024556396362608279798462606565656297979797976047656565626066666662989898989860476666666260676767629999999999604767676762606868686210010010010010060476868686260478279798462

但是, 收到的byteArray是

22330-67100-713387-766-68-88126125-12696-123-10-98-25-36-92-48104-125-601176376-214354109-3715-99-33-77-770058-6435-6439060-6437-64410103064-649-6419047-644-6414051050-6443-64470-100-6445-64490-980-94-648-6418010-643-64130220190-1100860100520500230103019021060709010024011012025013014015016017020180405020080220110210013020018636153514341232122

完全不同......

0 个答案:

没有答案