Java 如何将ByteArrawSerializer的messageSize设置为无限?

Java 如何将ByteArrawSerializer的messageSize设置为无限?,java,spring,spring-integration,Java,Spring,Spring Integration,我使用ByteArrawSerializer作为套接字消息反序列化器。 消息结束总是由服务器关闭套接字来指示 由于消息可能很大,我想定义序列化程序的messagesize unlimited。但是怎么做呢 以下情况会导致缓冲区溢出错误: ByteArrayRawSerializer s = new ByteArrayRawSerializer(); s.setMaxMessageSize(Integer.MAX_VALUE); 使用如此大的缓冲区是完全不切实际的,每个新请求都会尝试分配>2Gb

我使用ByteArrawSerializer作为套接字消息反序列化器。 消息结束总是由服务器关闭套接字来指示

由于消息可能很大,我想定义序列化程序的messagesize unlimited。但是怎么做呢

以下情况会导致缓冲区溢出错误:

ByteArrayRawSerializer s = new ByteArrayRawSerializer();
s.setMaxMessageSize(Integer.MAX_VALUE);

使用如此大的缓冲区是完全不切实际的,每个新请求都会尝试分配>2Gb的内存

您需要使用一个更合理的大小,该大小足以处理预期的消息大小

或者,创建自定义反序列化程序,根据需要分配更多缓冲区

编辑

这是一个弹性原始反序列化器

/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.integration.ip.tcp.serializer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.core.serializer.Deserializer;
import org.springframework.util.StreamUtils;

/**
 * A deserializer that uses an elastic {@link ByteArrayOutputStream}
 * instead of a fixed buffer. Completion is indicated by the sender
 * closing the socket.
 *
 * @author Gary Russell
 * @since 5.0
 *
 */
public class ByteArrayElasticRawDeserializer implements Deserializer<byte[]> {

    @Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamUtils.copy(inputStream, out);
        out.close();
        return out.toByteArray();
    }

}

我不是天才,但这是我的帮助。。那么,是否可以用无限的消息大小初始化ByteArrayRawSerializer?事实上,我以前不知道消息的大小。Java语言只允许一个Integer.MAX_值的缓冲区,即使这样,也只有在内存足够的情况下。JVM进一步限制了线程主java.lang.OutOfMemoryError中的异常:请求的数组大小超过VM限制原始反序列化程序很简单;当更多字节到达时,您需要一个更复杂的方法来根据需要分配内存。我用一个简单的弹性反序列化器更新了我的答案,缓冲区将根据需要增长。修复了StreamUtils导入。