简体   繁体   English

InputStream.read(byte [],0 length)提前停止?

[英]InputStream.read(byte[], 0 length) stops early?

I have been writing something to read a request stream (containing gzipped data) from an incoming HttpServletRequest ('request' below), however it appears that the normal InputStream read method doesn't actually read all content? 我一直在写一些东西来读取来自传入的HttpServletRequest(下面的'request')的请求流(包含gzip压缩数据),但看起来普通的InputStream读取方法实际上并没有读取所有内容?

My code was: 我的代码是:

InputStream requestStream = request.getInputStream();
if ((length = request.getContentLength()) != -1)
{
    received = new byte[length];
    requestStream.read(received, 0, length);
}
else
{
    // create a variable length list of bytes
    List<Byte> bytes = new ArrayList<Byte>();

    boolean endLoop = false;
    while (!endLoop)
    {
        // try and read the next value from the stream.. if not -1, add it to the list as a byte. if
        // it is, we've reached the end.
        int currentByte = requestStream.read();
        if (currentByte != -1)
            bytes.add((byte) currentByte);
        else
            endLoop = true;
    }
    // initialize the final byte[] to the right length and add each byte into it in the right order.
    received = new byte[bytes.size()];
    for (int i = 0; i < bytes.size(); i++)
    {
        received[i] = bytes.get(i);
    }
}

What I found during testing was that sometimes the top part (for when a content length is present) would just stop reading part way through the incoming request stream and leave the remainder of the 'received' byte array blank. 我在测试期间发现的是,有时顶部(当存在内容长度时)将停止读取传入请求流的一部分,并将“接收”字节数组的其余部分留空。 If I just make it run the else part of the if statement at all times, it reads fine and all the expected bytes are placed in 'received'. 如果我只是让它在任何时候都运行if语句的else部分,那么它读得很好并且所有预期的字节都放在'received'中。

So, it seems like I can just leave my code alone now with that change, but does anyone have any idea why the normal 'read'(byte[], int, int)' method stopped reading? 所以,似乎我现在可以单独留下我的代码进行更改,但有没有人知道为什么正常的'read'(byte [],int,int)'方法停止读取? The description says that it may stop if an end of file is present. 描述说如果存在文件结尾,它可能会停止。 Could it be that the gzipped data just happened to include bytes matching whatever the signature for that looks like? 可能是因为gzip压缩的数据恰好包含匹配任何签名的字节吗?

You need to add a while loop at the top to get all the bytes. 您需要在顶部添加while循环以获取所有字节。 The stream will attempt to read as many bytes as it can, but it is not required to return len bytes at once: 流将尝试尽可能多地读取字节数,但不需要一次返回len个字节:

An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. 尝试读取len个字节,但可以读取较小的数字,可能为零。

if ((length = request.getContentLength()) != -1)
{
    received = new byte[length];
    int pos = 0;
    do {
        int read = requestStream.read(received, pos, length-pos);

        // check for end of file or error
        if (read == -1) {
            break;
        } else {
            pos += read;
        }
    } while (pos < length);
}

EDIT: fixed while. 编辑:修复时。

You need to see how much of the buffer was filled. 您需要查看填充了多少缓冲区。 Its only guaranteed to give you at at least one byte. 它只能保证至少给你一个字节。

Perhaps what you wanted was DataInputStream.readFully() ; 也许你想要的是DataInputStream.readFully() ;

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

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