简体   繁体   English

Java NIO,读取未完成

[英]Java NIO, reads not completing

I am attempting to write a simple HTTP async server and a test client. 我正在尝试编写一个简单的HTTP异步服务器和一个测试客户端。 My test client makes a bunch of requests to the server, but doesn't do anything with the response except read it. 我的测试客户端向服务器发出了一堆请求,但是除了读取响应之外,对响应没有任何处理。 The problem I'm running into is after a bunch of successful responses, my client fails to read the full response of a particular request. 我遇到的问题是在一堆成功的响应之后,我的客户端无法读取特定请求的完整响应。 I have no idea why this is happening. 我不知道为什么会这样。

Here is the read/write handler for my server: read/write handler 这是我的服务器的读/写处理程序读/写处理程序

The important code is the function that sends the file: 重要的代码是发送文件的功能:

    public void handleWrite(SelectionKey key) throws IOException {
    client.write(out); // Write the headers to the stream
    if (state == State.SENDING_RESPONSE && out.remaining() == 0) {
        // We are done writing the headers
        if (sendFile && (mapped == null)) {
            // Send the file via direct transfer
            System.err.println("DT: " + fPath + " " + pos + "/" + fileSize);
            long transferred = f.transferTo(pos, fileSize, client);
            pos += transferred;
            System.err.println("DT: " + fPath + " " + pos + "/" + fileSize);
        } else if (mapped != null){
            // Send the file (in mapped from either filesystem or from
            // cache)
            System.err.println("MAPPED: " + fPath + " " + mapped.position() + "/" + mapped.limit());
            written += client.write(mapped);
            System.err.println("MAPPED: " + fPath + " " + written + "/" + mapped.limit());
        }
    }
    if (out.remaining() == 0
            && ((mapped == null && pos == fileSize) || (mapped != null && mapped
                    .remaining() == 0))) {
        // We are done transferring the file!
        System.err.println(pos + " " + fileSize);
        assert (pos == fileSize) || (mapped.position() == mapped.limit()) : "File not sent.";
        // Must reset position if from cache
        if (inCache) {
            mapped.position(0);
        }
        if (sendFile) {
            cache();
            f.close();
            inputStream.close();
        }
        state = State.SOCKET_CLOSED;
        d.getKey(client).cancel();
        client.close();
    }
}

and here is the response handler in the client: response handler 这是客户端中的响应处理程序响应处理程序

Important code is the read part: 重要的代码是阅读的部分:

        if((sz = header.get("content-length")) != null) {
        bodySize = Integer.parseInt(sz);
        byte[] buff = new byte[bodySize];
        int k = 0, read = 0;
        while((k = connection.getInputStream().read(buff, read, bodySize - read)) != -1 && read < bodySize) {
            read += k;
        }
        assert read == bodySize : "Not all of file read. " + read + " " + bodySize;
    }

The assertion at the bottom of the response handler is the one that's failing. 响应处理程序底部的断言是失败的断言。 I've also included the server and dispatcher themselves, as well as the client if thats helpful. 我也包括了服务器和调度程序本身,以及客户端(如果有帮助的话)。 server/dispatcher client 服务器/调度 程序客户端

while((k = connection.getInputStream().read(buff, read, bodySize - read)) != -1 && read < bodySize) {
      read += k;

Throw that away and use DataInputStream.readFully(data). 扔掉它并使用DataInputStream.readFully(data).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM