ByteArrayOutputStream容量限制 [英] ByteArrayOutputStream capacity restriction

查看:1470
本文介绍了ByteArrayOutputStream容量限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建 ByteArrayOutputStream barr = new ByteArrayOutputStream(1); ,即容量为1字节,并向其写入1个字节以上 barr.write ( 123456789000000 .getBytes()); 。没有错误发生,我检查了 barr 的长度是15。为什么我的文字没有被阻止或包裹?有没有一种方法可以防止写超出容量的内容,并且可以使用哪个 outputstream
我的可用内存非常有限,不想超出我的限制定义

I create ByteArrayOutputStream barr = new ByteArrayOutputStream(1);, i.e. with capacity 1 bytes and write to it more than 1 byte barr.write("123456789000000".getBytes());. No error occurs, I check the length of barr it is 15. Why my writing was not blocked or wrapped? Is there a way to prevent of writing more than capacity and which outputstream could be used for that? I am very limited in available memory and don`t want to write there more than my limitations define

PS
非常感谢您的回答!我已经跟进了问题
如果您愿意可能看起来

P.S. Thanks a lot for the answers! I had a following up question It could be great if you could look

推荐答案

这是因为您指定给构造函数的容量为初始大小缓冲区。如果您写入更多数据,缓冲区将自动以更大的大小重新分配,以容纳更多数据。

That is because the capacity that you specify to the constructor is the initial size of the buffer. If you write more data, the buffer will be automatically re-allocated with a larger size, to fit more data.

据我所知, ByteArrayOutputStream 限制缓冲区的增长。您可以改用其他东西,例如 java.nio.ByteBuffer ,它的大小是固定的。

As far as I know, there is no way with ByteArrayOutputStream to limit the growth of the buffer. You could use something else instead, for example a java.nio.ByteBuffer, which has a fixed size.

这篇关于ByteArrayOutputStream容量限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆