使用 Hazelcast readUTF 时字符串索引超出范围

Posted

技术标签:

【中文标题】使用 Hazelcast readUTF 时字符串索引超出范围【英文标题】:String Index out of range when using Hazelcast readUTF 【发布时间】:2018-11-23 02:46:31 【问题描述】:

我在 Hazelcast 中使用 DataSerializable 来序列化一个自定义类,然后我将其存储在缓存中,以供在 tomcat 服务器中运行的 Web 应用程序访问。早些时候我得到了一个 NPE,因为自定义类包含一些也可能为空的字段。我通过添加一个检查来解决这个问题,如果该特定字段为空。它执行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);
        

    

我得到以下堆栈跟踪

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);
        
    

仍然报同样的错误

【问题讨论】:

这表明您的 readData / writeData 实现不是对称的。你可以发布这两种方法吗?此外,您可能会发现ObjectDataInput/Output 中可用的read/writeUTFArray 方法很有用。 我发布了写和读方法 【参考方案1】:

而不是

    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 被移除的情况。

【讨论】:

好的,那么有些问题:顺便说一句,非法索引是十六进制的:FC00FFFF。将添加到答案中 仍然相同,在 Eclipse 中突出显示的代码是 String f = in.readUTF() 也许解决方案是使读写方法对称,如 @vassilis 所说。我进行空值检查的唯一原因是因为如果文件夹结果是空值,writeUTF 会抛出 NPE 所以folders.get(i) 对某些人来说可能是空的?你是对的,代码必须是对称的。 this.folders 表示该位置没有递归。不过,您可以更好地解决它。 文件夹本身可能为空,会引发异常【参考方案2】:

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

为避免这种情况,您可以将null 值编码为不同的int,例如使用-1 表示null,如下例所示。这样,您就可以确定前 4 个字节总是有一个 int,根据它的值,您可以决定它是表示 null 还是继续读取 folders 元素。

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);
  

【讨论】:

Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -67043329 仍然给出同样的错误

以上是关于使用 Hazelcast readUTF 时字符串索引超出范围的主要内容,如果未能解决你的问题,请参考以下文章

为啥使用 readUTF 阅读时出现 EOF 异常?

使用 SmartFoxServer 实现 Hazelcast

Hazelcast 正在使用大量 JVM 线程

Hazelcast如何进行分区

Hazelcast - 最旧的条目首先被驱逐

Hazelcast - QueueListener - 多线程或 MDB