简体   繁体   English

我可以使字节数组的大小动态吗?

[英]Can I make the size of a byte array dynamic?

I'm using a byte array with the write() method of OutputStream to store the data into a file. 我正在使用带有OutputStream的write()方法的字节数组将数据存储到文件中。 For that, I'm initiating the byte array with a predefined size. 为此,我正在使用预定义的大小启动字节数组。 But I don't want it to take up so much space for every InputStream whose size could be much lower than the size of the array. 但是我不希望它占用每个InputStream这么大的空间,其大小可能远低于数组的大小。 Is it possible to somehow make the array dynamic so that it adjusts its size depending on the size of the InputStream ? 是否有可能以某种方式使数组动态,以便根据InputStream的大小调整其大小? I have something like this in my code: 我的代码中有类似的东西:

byte[] b = new byte[20000];
int length;                 
while ((length = in.read(b))!= -1) {
    //writing it to a file
    op.write(b, 0, length);
    k++;
}

in.close();
op.close();

Here, in is the InputStream object and op is the FileOutputStream object. 这里, inInputStream对象, opFileOutputStream对象。 I've tried using ArrayLists, but there is no direct method of FileOutputStream to write data into an ArrayList, it only accepts byte[] . 我已经尝试过使用ArrayLists,但没有FileOutputStream直接方法将数据写入ArrayList,它只接受byte[]

The problem isn't on the output side at all - it's just that you don't know how big the input is. 问题根本不在输出端 - 只是你不知道输入有多大。

It's not clear where the InputStream is coming from - if the input is being received over the network for example, you may not have any indication of the size beforehand. 目前还不清楚InputStream的来源 - 例如,如果通过网络接收输入,您可能事先没有任何大小的指示。 InputStream itself doesn't have any simple way of checking whether the stream has a known length. InputStream本身没有任何简单的方法来检查流是否具有已知长度。

Bear in mind that the 20K array here is only important while you're copying the data. 请记住,此处的20K阵列在您复制数据时很重要。 After that, it can be garbage collected. 之后,它可以被垃圾收集。 Do you have any indication that this is actually an issue? 你有迹象表明这实际上是一个问题吗?

You could just reduce the size of the array to (say) 4K or 8K. 可以将阵列的大小减小到(例如)4K或8K。 You'd still be able to handle any size of input, as you're looping over it and copying it in chunks. 您仍然可以处理任何大小的输入,因为您循环并以块的形式复制它。 It just wouldn't be quite as efficient in cases where you can read more than that buffer size in one go. 它只是将不会非常有效的情况下,你可以一气呵成读比缓冲区大小的更多。 The extent of that efficiency difference will be very context-sensitive - only you can measure whether how that code will perform in your real situation - or how important that performance is in terms of time, or how useful it is to save a few thousand bytes which can be garbage collected immediately afterwards anyway. 效率差异的程度将非常依赖于上下文 - 只有您可以衡量代码在实际情况下的表现 - 或者性能在时间方面的重要性,或者保存几千字节的有用程度无论如何都可以立即收集垃圾。

It doesn't matter what size the buffer is from the point of view of the code. 从代码的角度来看,缓冲区的大小无关紧要。 A fixed size of 8192 is sufficient for most purposes, regardless of the file length. 无论文件长度如何,固定大小8192对于大多数用途就足够了。 Your code will work with any size greater than zero. 您的代码可以使用任何大于零的大小。

Simply take some small amount of buffer. 只需少量缓冲液即可。 It works. 有用。

Long back stumbled with this sort of situation and the below piece of code working fine for me. 长期以来偶然发现这种情况和下面的代码对我来说很好。

 byte[] b = new byte[1024]; //a little buffer
            int length;                 
            while ((length = in.read(b))!= -1) {
                //writing it to a file
                op.write(b, 0, length);
                k++;
            }

            in.close();
            op.close();

Asked and got clarification here long back : How the buffer byte array is continuously filling while streaming? 在这里询问并得到澄清: 缓冲字节数组在流式传输时如何连续填充?

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

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