java中未知长度的字节数组
我正在用java构造一个字节数组,但我不知道该数组有多长。
我想要一些像 Java 的 StringBuffer 这样的工具,您只需调用 .append(byte b) 或 .append(byte[] buf) 并让它缓冲我的所有字节,并在完成后返回给我一个 byte[] 。 是否有一个类对字节执行 StringBuffer 对字符串执行的操作? 它看起来不像 ByteBuffer 类是我正在寻找的。
有人有好的解决办法吗?
I am constructing an array of bytes in java and I don't know how long the array will be.
I want some tool like Java's StringBuffer that you can just call .append(byte b) or .append(byte[] buf) and have it buffer all my bytes and return to me a byte[] when I'm done. Is there a class that does for bytes what StringBuffer does for Strings? It does not look like the ByteBuffer class is what I'm looking for.
Anyone have a good solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
ByteArrayOutputStream
。 您可以使用 write( byte[] ) ,它会根据需要增长。Try
ByteArrayOutputStream
. You can usewrite( byte[] )
and it will grow as needed.只是为了扩展前面的答案,您可以使用 ByteArrayOutputStream 及其方法
public void write(byte[] b, int off, int len)
,其中参数为:如果您想将其用作“字节生成器”并逐字节插入,您可以使用以下命令:
然后您可以使用
baos.toString()
方法将数组转换为字符串。 优点是当你需要设置输入的编码时,你可以简单地使用 ie:Just to extend the previous answer, you can use ByteArrayOutputStream and it's method
public void write(byte[] b, int off, int len)
, where parameters are:If you want to use it as a "byte builder" and insert byte by byte, you can use this:
Then you can use
baos.toString()
method to convert the array to string. The advantage is when you need to set up encoding of input, you can simply use i.e.:我写了一个非常易于使用并且避免了大量字节数组缓冲区复制的程序。
它有一种称为 add 的方法。
您可以向其中添加字符串、字节、字节、long、int、double、float、short 和 chars。
该 API 易于使用并且具有一定程度的故障安全性。 它不允许您复制缓冲区,也不提倡拥有两个读取器。
它具有边界检查模式和无边界检查的“我知道我在做什么”模式。
边界检查模式会自动增长它,因此没有麻烦。
https://github.com/RichardHightower/ boon/wiki/Auto-Growable-Byte-Buffer-like-a-ByteBuilder
这是有关如何使用它的完整分步指南。 它在 github 上。
Java Boon - 像 ByteBuilder 这样的自动增长字节缓冲区
您是否曾经想要一个易于使用的自动增长的缓冲区数组和/或者您可以给它一个固定的大小并只向其中添加内容? 我有。 我也写了一篇。
看..我可以向它写入字符串(它将它们转换为 UTF-8)。
然后我可以从缓冲区中读取字符串:
我永远不必设置数组的大小。 它将根据需要以有效的方式自动增长。
如果我确切地知道我的数据有多大,那么我可以使用 createExact 来节省一些边界检查。
如果我使用创建精确,那么我会说...嘿..我确切地知道它可以增长到多大,并且它永远不会超过这个数字,如果它超过了...你可以用麻袋打我的头岩石!
下面用一袋石头砸你的头!
抛出异常!!!!
它适用于双精度数。
它适用于浮点数。
它适用于 int。
它适用于 char。
>它适用于短。
它甚至适用于字节。
您可以将各种基元添加到字节数组中。
现在我们只需验证是否可以读回所有内容。
一旦你调用了
,你就表示你已经完成了 ByteBuffer 的操作!
一旦您请求字节,它就变得毫无用处,因为它将内部字节数组设置为空。
当您调用 readAndReset 时,它会为您提供缓冲区。 这是我的内部状态,你可以拥有它,但我将把它设置为空,这样就没有其他人使用它。
没关系。 如果您确定一次只有一个实例正在使用缓冲区(字节[]),则只需创建另一个实例。
您甚至可以使用刚刚使用的缓冲区,如
这是因为没有复制任何缓冲区。 ByteBuf 写入您给它的缓冲区。
如果您希望将另一个副本提供给 ByteBuf,请执行以下操作:
这毕竟是福音。 :)
快来看看福利吧。 您免费获得上述类和 idx、idxInt 和 idxLong!
https://github.com/RichardHightower/boon/
I wrote one that is really easy to use and avoids a lot of byte array buffer copying.
It has one method called add.
You can add strings, bytes, byte, long, int, double, float, short, and chars to it.
The API is easy to use and somewhat fail safe. It does not allow you to copy the buffer around and does not promote having two readers.
It has a bounds check mode and a I KNOW WHAT I AM DOING MODE with no bounds checking.
The bounds check mode auto-grows it so there is no hassle.
https://github.com/RichardHightower/boon/wiki/Auto-Growable-Byte-Buffer-like-a-ByteBuilder
Here is a complete step by step guide on how to use it. It is on github.
Java Boon - Auto Growable Byte Buffer like a ByteBuilder
Have you ever wanted an easy to use buffer array that grow automatically and/or you can give it a fix size and just add stuff to it? I have. I wrote one too.
Look.. I can write strings to it (it converts them to UTF-8).
Then later I can read the String out of the buffer:
I never have to set the size of the array. It will auto-grow as needed in an efficient manner.
If I know exactly how large my data is going to be than I can save some bounds checking by using createExact.
If I use create exact, then I am saying... hey.. I know exactly how big it can grow to and it will never go over this number and if it does...you can hit me over the head with a sack of rocks!
The following hits you over the head with a sack of rocks!
THROWS AN EXCEPTION!!!!
It works with doubles.
It works with float.
It works with int.
It works with char.
It works with short.
It even works with bytes.
You can add all sorts of primitives to your byte array.
Now we just verify that we can read everything back.
Once you call
then you are saying that you are done with the ByteBuffer!
Once you ask for the bytes, it becomes useless as it sets the internal byte array to nothing.
When you call readAndReset, it is giving you its buffer. Here is my internal state, you can have it, but I am going to set it to null so nobody else uses it.
It is ok. Just create another if you are sure only one instance at a time is using the buffer (byte []).
You can even use the buffer you were just using as in
This is because no buffer gets copied. ByteBuf writes to the buffer you give it.
If you want another copy to be given to ByteBuf then do this:
This is boon after all. :)
Come check out boon. You get the above class and idx, and idxInt and idxLong for free!
https://github.com/RichardHightower/boon/
让我们来看看。 Java中有ByteBuffer类。
http://docs.oracle.com/javase/7 /docs/api/java/nio/ByteBuffer.html
它具有批量方法,可将连续的字节序列从字节数组传输到硬件缓冲区。 它会成功的。
它还具有绝对和相对 get 和 put 方法,用于读取和写入 byte[] 和其他原语到字节缓冲区。
它还具有压缩、复制和切片字节缓冲区的方法。
它还有一个批量放置来放置数组,这与您要求的附加非常接近:
请参阅:
http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#put(byte[])
我编写了自己的小库来操作字节数组。 :)
你可以像这样添加它们,这样
你就可以得到 b 的所有内容,并在 a 的内容之后得到 c 的内容。
如果您想将 a 增大 21,您可以执行以下操作:
如果您想将 a 的大小加倍,您可以执行以下操作:
请参阅...
https://github.com/RichardHightower/boon/blob/master/src/main/ java/org/boon/core/primitive/Byt.java
创建
索引
包含
切片:
增长
收缩:
复制:
添加:
添加实际上是使用 System.arraycopy 将它们添加在一起(考虑不安全,但还不是)。
将一个数组添加到另一个数组:
插入:
这里是一些方法的一瞥:
更多......
Let's see. There is the ByteBuffer class in Java.
http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html
It has bulk methods that transfer contiguous sequences of bytes from a byte array to hardware buffers. It would do the trick.
It also has absolute and relative get and put methods that read and write byte[]s and other primitives to / for the byte buffer.
It also has methods for compacting, duplicating, and slicing a byte buffer.
It also has a bulk put to put an array, which is pretty close to the append you were asking for:
See:
http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#put(byte[])
I wrote my own little lib for manipulating byte arrays. :)
You can add them like so
this would give you all of the contents of b, and c after the contents for a.
If you wanted to grow a by 21, you could do the following:
If you wanted to double the size of a, you could do the following:
See...
https://github.com/RichardHightower/boon/blob/master/src/main/java/org/boon/core/primitive/Byt.java
Create
Index
Contains
Slice:
Grow
Shrink:
Copy:
Add:
The add actually adds them together by using System.arraycopy (considering Unsafe, but not yet).
Add one array to another:
Insert:
Here is a peek at a few of the methods:
More....