我试试这个代码: byte arr[] = ByteBuffer.allocate(2).putInt(1).array()
但是它失败了一个BufferOverflowException
。 1太大而无法存储在2个字节中?还是我的问题在其他地方?Java(Android) - BufferOverflowException on putInt
0
A
回答
3
1太大而无法存储在2个字节中?
那么,一个int
是... putInt
总是写4个字节。从documentation for ByteBuffer.putInt
抛出:
BufferOverflowException - 如果是留在这个缓冲
少于四个字节,如果你只想把一个两字节整数,使用putShort
代替。如果您想要以可变宽度编码(其中空间取决于值)存储数据,则可能需要自己编写代码。
1
从方法putInt
的javadoc的:
写入四个字节含有给定int值,在当前的字节顺序,到该缓冲区的当前位置,然后递增由四个
位置
你只分配了2个字节,因此BufferOverflowException
:
当相对的put操作达到目标缓冲区的限制时抛出的未检查异常。
可以解决expading到4个字节的缓冲或使用putShort仅使用2个字节来存储数字1
1
ByteBuffer.putInt(1)
把一个整数值到缓冲器中。一个整数是4个字节长(32位)。
相关问题
- 1. MappedByteBuffer - BufferOverflowException
- 2. sun.misc.Unsafe putInt,getInt来源
- 3. 生成Javadoc时BufferOverflowException?
- 4. ADT得到BufferOverflowException在DDMS
- 5. PutInt捆绑不起作用
- 6. Android/Java - Margin on left/right/bottom
- 7. 的Java客户端的WebSockets暗流的BasicWebSocketSessionRemoteEndpoint sendBinary抛出BufferOverflowException
- 8. 将int转换为字节数组BufferOverflowException
- 9. Java NoSuchElementException on nextInt
- 10. Java Char on actionscript
- 11. Java -cp on linux
- 12. OMR/OCR on android
- 13. Uber Ride on android
- 14. Everything on Activity - Android
- 15. Android PorterDuffXfermode on ICS
- 16. Android - Loop on JsonArray?
- 17. ActionBarDisappearing on Android 4.0+
- 18. 'Global'ProgressDialog on android
- 19. Android - Pathfinding on map.png
- 20. Android db.delete(); on ListActivity
- 21. 如何使用PutInt从寄存器CL打印
- 22. JPanel on CardLayout Swing Java
- 23. click on listener on android widget按钮
- 24. graphQL networkStatus 7 on Android
- 25. spannable on android for textView
- 26. php cgi on android unrooted
- 27. ListAdapter On Xamarin for Android
- 28. Tagsoup and XOM on android
- 29. android-Staying on same activity
- 30. SignalR-java客户端不兼容的CPU abi on android kitkat 4.4.2