从标准I/O读取和写入的IndexOutOfBoundsException。

时间:2022-07-29 20:34:26

I'm new to Java and currently doing some experiments on it. I wrote a little program that does read and write stream of std I/O but I kept getting exceptions thrown for out of range. Here is my code

我是Java新手,目前正在做一些实验。我编写了一个小程序,它可以读取和写入std I/O流,但是我一直在抛出超出范围的异常。这是我的代码

int BLOCKSIZE = 128*1024;                                                                                                                                                
InputStream inStream = new BufferedInputStream(System.in);

OutputStream outStream = new BufferedOutputStream(System.out);



byte[] buffer = new byte[BLOCKSIZE];




int bytesRead = 0;
int writePos = 0;
int readPos = 0;
while ((bytesRead = inStream.read(buffer,readPos,BLOCKSIZE)) != -1) {
 outStream.write(buffer,writePos,BLOCKSIZE);
 readPos += bytesRead;
 writePos += BLOCKSIZE;
 buffer = new byte[BLOCKSIZE];
}

Here is the exception thrown:"Exception in thread "main" java.lang.IndexOutOfBoundsException at java.io.BufferedInputStream.read(BufferedInputStream.java:327) at JavaPigz.main(JavaPigz.java:73)"

下面是抛出的异常:“线程中的异常”main“java.lang”。在JavaPigz.main(JavaPigz.java:73)中读取(BufferedInputStream.java:327)。

73th col is the inStream.read(...) statement. Basically I want to read 128kb bytes from stdin once and write it to the stdout and go back to read another 128kb chunk, so on and so forth. The same exception is also thrown to outStream.write()

73th col是inStream.read(…)语句。基本上,我想从stdin中读取128kb的字节,并将其写入stdout,然后返回读取另一个128kb块,等等。同样的异常也被抛出到outStream.write()

I did some debugging and it looks BufferedInputStream buffers at most 64kb chunk once. Don't know if this is true. Thank you.

我做了一些调试,它看起来缓冲了最多64kb块的缓冲区。不知道这是不是真的。谢谢你!

Edit: I also tried doing InputStream inStream = new BufferedInputStream(System.in,BLOCKSIZE); to specify the size of buffered chunk I want. But turns out it keeps giving size of 64kb no matter what is specified

编辑:我还尝试了InputStream inStream = new bufferedputstream (systemin,BLOCKSIZE);要指定我想要的缓冲块的大小。但事实证明,不管指定的是什么,它的大小都是64kb。

2 个解决方案

#1


3  

You're increasing your readPos (and writePos) in your loop. The subsequent reads are starting at that offset for inserting into your buffer, and attempting to write BLOCKSIZE bytes into it ... which won't fit, thus giving you an index out of bounds error.

您在循环中增加了readPos(和writePos)。随后的读取将从插入到缓冲区的偏移量开始,并尝试将BLOCKSIZE字节写入它……这是不合适的,从而给你一个界限误差的索引。

The way you have that loop written, readPos and writePos should always be 0 especially since you're creating a new buffer every time. That being said ... you really don't want to do that, you want to re-use the buffer. It looks like you're just trying to read from the input stream and write it to the output stream ...

您的循环书写方式,readPos和writePos应该始终是0,特别是您每次都创建一个新的缓冲区。那就是说……你真的不想那样做,你想重新使用缓冲区。看起来你只是想从输入流中读取数据并将其写入输出流…

while ((bytesRead = inStream.read(buffer,readPos,BLOCKSIZE)) != -1) {
    outStream.write(buffer,writePos,bytesRead);
}

#2


0  

your readPos and writePos correspond to the array ... not to the stream ...

您的readPos和writePos对应于这个数组…不属于溪流……

set them 0 and leave them at 0

把它们设为0,然后把它们设为0。

in your write call set param 3 to bytesRead instead of BLOCKSIZE

在您的write调用中,将param 3设置为bytesRead而不是BLOCKSIZE。

#1


3  

You're increasing your readPos (and writePos) in your loop. The subsequent reads are starting at that offset for inserting into your buffer, and attempting to write BLOCKSIZE bytes into it ... which won't fit, thus giving you an index out of bounds error.

您在循环中增加了readPos(和writePos)。随后的读取将从插入到缓冲区的偏移量开始,并尝试将BLOCKSIZE字节写入它……这是不合适的,从而给你一个界限误差的索引。

The way you have that loop written, readPos and writePos should always be 0 especially since you're creating a new buffer every time. That being said ... you really don't want to do that, you want to re-use the buffer. It looks like you're just trying to read from the input stream and write it to the output stream ...

您的循环书写方式,readPos和writePos应该始终是0,特别是您每次都创建一个新的缓冲区。那就是说……你真的不想那样做,你想重新使用缓冲区。看起来你只是想从输入流中读取数据并将其写入输出流…

while ((bytesRead = inStream.read(buffer,readPos,BLOCKSIZE)) != -1) {
    outStream.write(buffer,writePos,bytesRead);
}

#2


0  

your readPos and writePos correspond to the array ... not to the stream ...

您的readPos和writePos对应于这个数组…不属于溪流……

set them 0 and leave them at 0

把它们设为0,然后把它们设为0。

in your write call set param 3 to bytesRead instead of BLOCKSIZE

在您的write调用中,将param 3设置为bytesRead而不是BLOCKSIZE。