0

堆栈跟踪很长,但这是相关部分。

Caused by: java.lang.ArrayIndexOutOfBoundsException: 33
  at java.lang.String.equals(String.java:1018)
  at java.util.HashMap.get(HashMap.java:305)

因此,当我从中检索元素时HashMap<String,?>,会调用 String#equqls。然后,从行抛出 ArrayIndexOutOfBoundExceptionif (v1[i++] != v2[j++])

// Sun java 1.6
public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}

这真的很奇怪。


更新。异常来自日志分析程序。我没有在那里使用反射。我还想到了一些损坏了字符串对象但没有从代码中得到任何线索的东西。而且这个问题在重启后没有重现。如果我继续重试,我不知道它是否会重现。

  1. 日志工具

    public static Map<String, String> parseLine(String line) {
        if (StringUtils.isEmpty(line)) {
            return Collections.emptyMap();
        }
        String[] parts = StringUtils.split(line, '\t');
        Map<String, String> results = new HashMap<String, String>();
        for (String part : parts) {
            String[] keyVal = StringUtils.split(part, "=", 2);
            if (keyVal.length == 2) {
                String key = keyVal[0];
                String value = keyVal[1];
                results.put(key, value);
            }
        }
        return results;
    }
    
    public static void process(String fileName, Date date, Closure closure) throws IOException {
        InputStream is = null;
        Reader r = null;
        try {
            is = new FileInputStream(getFilename(fileName, date));
            is = new BufferedInputStream(is);
            is = new GZIPInputStream(is);
            r = new InputStreamReader(is, "utf8");
            LineIterator iter = IOUtils.lineIterator(r);
            while (iter.hasNext()) {
                String line = iter.nextLine();
                Map<String, String> map = LogUtils.parseLine(line);
                closure.execute(map);
            }
        } finally {
            IOUtils.closeQuietly(r);
            IOUtils.closeQuietly(is);
        }
    }
    
  2. 关闭

    public void execute(Map<String, String> line) {
        if (dataMap == null) {
            dataMap = new HashMap<String, Map<Long, Integer>>();
        }
    
        String identifier = line.get(Constants.IDENTIFIER_KEY); // Exception thrown from there
        /* ...blahblah... */
    }
    

为简单起见,我将创建一个闭包对象并LogUtils.process用它调用。LogUtils 将 line(Example: key1=value1\tkey2=value2\t...kn=valuen) 换成HashMap. 然后LogUtils将地图传递给闭包。line.get(Constants.IDENTIFIER_KEY);当 Constants.IDENTIFIER_KEY 是静态最终字段时会引发异常。所以 equals 方法的 lhs 和 rhs 是一个静态的 final String 和一个StringUtils.split(line, '\t'). 我检查了 commons-lang 的代码。它String#substring实际上是一个。所以还是很奇怪。

4

1 回答 1

2

这基本上表明某些东西正在破坏字符串对象。很容易重现:

import java.lang.reflect.*;

public class Test {
    public static void main(String[] args) throws Exception {
        String x = "hello";
        String y = new String("xhell");

        Field field = String.class.getDeclaredField("offset");
        field.setAccessible(true);
        field.set(y, 1);
        System.out.println(x.equals(y));
    }
}

...但我们不知道这是否真的是字符串在您的情况下被损坏的方式。

于 2012-09-03T06:03:46.033 回答