使用Hazelcast readUTF时,String Index超出范围

时间:2018-06-13 13:32:38

标签: java serialization hazelcast serializable hazelcast-imap

我在Hazelcast中使用DataSerializable来序列化一个自定义类,然后我将其存储在缓存中,以便由运行在tomcat服务器中的Web应用程序访问。早些时候我得到了一个N​​PE,因为自定义类包含一些可能为null的字段。我通过添加一个检查来修复它,如果该特定字段为空。它执行writeUTF("null")但现在我以这种方式读取字符串字段列表

    int foldersCount = in.readInt();
    for (int k = 0; k< foldersCount; k++){

        String f = in.readUTF();
        this.folders.add(f);
    }

writeData看起来像这样 -

if (this.folders == null){
        out.writeUTF("null");
    }

    else{

        out.writeInt(this.folders.size());
        for (String b: this.folders){
            out.writeUTF(b);
        }

    }

我得到以下stacktrace

java.lang.StringIndexOutOfBoundsException: String index out of range: -67043329
at java.lang.String.<init>(Unknown Source)
at com.hazelcast.internal.serialization.impl.ByteArrayObjectDataInput.readUTF(ByteArrayObjectDataInput.java:569)
at o.c.i.y.s.d.s.readData(User.java:433)
at com.hazelcast.internal.serialization.impl.DataSerializableSerializer.readInternal(DataSerializableSerializer.java:158)
at com.hazelcast.internal.serialization.impl.DataSerializableSerializer.read(DataSerializableSerializer.java:105)
at com.hazelcast.internal.serialization.impl.DataSerializableSerializer.read(DataSerializableSerializer.java:50)
at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.read(StreamSerializerAdapter.java:48)
at com.hazelcast.internal.serialization.impl.AbstractSerializationService.toObject(AbstractSerializationService.java:191)
at com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore.dataToValue(AbstractNearCacheRecordStore.java:186)
at com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore.toValue(AbstractNearCacheRecordStore.java:206)
at com.hazelcast.internal.nearcache.impl.store.BaseHeapNearCacheRecordStore.updateAndGetReserved(BaseHeapNearCacheRecordStore.java:164)
at com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore.tryPublishReserved(AbstractNearCacheRecordStore.java:413)
at com.hazelcast.internal.nearcache.impl.DefaultNearCache.tryPublishReserved(DefaultNearCache.java:217)
at com.hazelcast.client.proxy.NearCachedClientMapProxy.tryPublishReserved(NearCachedClientMapProxy.java:575)
at com.hazelcast.client.proxy.NearCachedClientMapProxy.getInternal(NearCachedClientMapProxy.java:128)
at com.hazelcast.client.proxy.ClientMapProxy.get(ClientMapProxy.java:297)
at o.c.i.y.s.d.persistence.UserDAO.getContextInfo(UserDAO.java:1058)
at o.c.i.y.s.d.s.persistence.UserDAO.getUserInfo(UserDAO.java:890)
at o.c.i.y.s.d.s.service.impl.UserRestServiceImpl.getLoginUserInfo(UserRestServiceImpl.java:250)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1527)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1484)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

修改

新的读/写方法

写方法

if (this.folders == null) {
        out.writeInt(-1);
    }
    else{
        int foldersCount = this.folders.size();
        out.writeInt(foldersCount);
        for (int i = 0; i < foldersCount; ++i) {
            String b = this.folders.get(i);
            out.writeUTF(b);
        }
    }

阅读方法

int foldersCount = in.readInt();
    if (foldersCount == -1)
        this.folders = null;
    else{
        for (int k = 0; k< foldersCount; k++){

            String f = in.readUTF();
            this.folders.add(f);
        }
    }

仍然给出同样的错误

2 个答案:

答案 0 :(得分:0)

而不是

    out.writeUTF("null");

更一般的是

    out.writeInt(0);

原因是null应该等同于空集合。

至关重要的是,在写作时,folders的内容不应改变,因此书面大小也应如此 与写入的UTF字符串数相对应。

也许确实这样做:

    if (this.folders == null) {
        this.folders = Collections.emptyList();
    }
    int foldersCount = this.folders.size();
    out.writeInt(foldersCount);
    for (int i = 0; i < foldersCount; ++i) {
        String b = this.folders.get(i);
        out.writeUTF(b);
    }

删除null的情况。

答案 1 :(得分:0)

问题是您的readData方法始终希望将int作为要读取的第一个字段,但是,如果在序列化方面,foldersnull,则字节流实际上包含String,导致序列化类型与接收端要反序列化的类型不匹配,因此出现序列化异常。

为避免这种情况,您可以将null值编码为不同的int,例如使用-1表示null,如以下示例所示。这样,您可以确定在前4个字节中始终有一个int,并且可以根据其值确定是表示null还是继续读取folders元素。 / p>

void readData(ObjectDataInput in) {
  int length = in.readInt();
  if (length == -1) {
    folders = null;
  } else {
    folders = new ArrayList();
    for (int i = 0; i < length; i++) {
      folders.add(in.readUTF());
    }
  }
}

void writeData(ObjectDataOutput out) {
  if (folders == null) {
    out.writeInt(-1);
    return;
  }
  out.writeInt(folders.size());
  for (String s : folders) {
    out.writeUTF(s);
  }
}