StringIndexOutOfBounds从string设置int值时的异常

时间:2018-05-16 11:54:34

标签: java

通过运行在远程服务器上运行命令的简单代码,在下面:

if (s.contains("CONNECTED") || s.endsWith("Enter command: ") || s.endsWith(":")) {
    //login command
    dataOutput.writeBytes(LoginCommand + "\r\n");
    Arrays.fill(line, '\n');

    lineSize = dataInput.read(line);
    s = String.copyValueOf(line, 0, lineSize);
    result = Integer.parseInt(s.substring(s.indexOf("RESP:") + 5, s.indexOf(";")));

    if (result == 0) {
        result = -101;
        //execution command
        String TelnetCommand = executedCommand.replace("%par1%", par1).replace("%par2%", par2).replace("%par3%", par3);
        dataOutput.writeBytes(TelnetCommand + "\r\n");
        int iCounter = 0;

    }

    connectionTrials = Integer.parseInt(properties.getProperty("NumberOfRetries"));
}

我收到以下异常:

java.lang.StringIndexOutOfBoundsException: String index out of range: -5
    at java.lang.String.substring(String.java:1911)
    at com.test.tool.Creation.doGet(Creation.java:85)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:808)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:517)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:499)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Thread.java:722)

注意到我尝试了hereofficial document中提到的解决方案,但没有成功。

P.S:变量result的初始值是:

int result = -100;

1 个答案:

答案 0 :(得分:1)

您正在操纵输入,而不确定它是否符合您的期望。强烈建议在处理之前验证任何输入。

一种非常简单的方法是使用regular expression

static final Pattern IS_CONNECTED = Pattern.compile("\\bCONNECTED\\b|\\bEnter command: $|:$");
static final Pattern RESPONSE = Pattern.compile("\\bRESP:(?<code>\\d+);")

if (IS_CONNECTED.matcher(s).matches()) {
    // ...
    Matcher matching_response = RESPONSE.matcher(s);
    if (matching_response.matches()) {
       String code_str = matching_response.group("code");
       result = Integer.parseInt(code_str); // Poor check, can still fail
    }
}

在您的具体情况下,您有throw new StringIndexOutOfBoundsException(-5),因为找不到RESP:;。意思是你在做:

s.substring(s.indexOf("RESP:") + 5, s.indexOf(";"));
s.substring(-1 + 5, -1);
s.substring(/*beginIndex*/ 4, /*endIndex*/ -1);

然后子串计算长度为:

sublength = -1 - 4
sublength = -5

由于sublength是负数,它throw new StringIndexOutOfBoundsException(-5)