• ByteBuffer操作简介


    1. package java.nio;
    2. public abstract class ByteBuffer
    3. extends Buffer
    4. implements Comparable
    5. {
    6. /*申请指定空间的ByteBuffer,DirectByteBuffer与HeapByteBuffer的区别稍后介绍*/
    7. public static ByteBuffer allocate(int capacity) {
    8. if (capacity < 0)
    9. throw new IllegalArgumentException();
    10. return new HeapByteBuffer(capacity, capacity);
    11. }
    12. public static ByteBuffer allocateDirect(int capacity) {
    13. // Android-changed: Android's DirectByteBuffers carry a MemoryRef.
    14. // return new DirectByteBuffer(capacity);
    15. DirectByteBuffer.MemoryRef memoryRef = new DirectByteBuffer.MemoryRef(capacity);
    16. return new DirectByteBuffer(capacity, memoryRef);
    17. }
    18. /*Byte数组转ByteBuffer*/
    19. public static ByteBuffer wrap(byte[] array,
    20. int offset, int length)
    21. {
    22. try {
    23. return new HeapByteBuffer(array, offset, length);
    24. } catch (IllegalArgumentException x) {
    25. throw new IndexOutOfBoundsException();
    26. }
    27. }
    28. public static ByteBuffer wrap(byte[] array) {
    29. return wrap(array, 0, array.length);
    30. }
    31. /*ByteBuffer转Byte数组*/
    32. public abstract Object array();
    33. /*get 方法,会改变position,remain不足会抛BufferUnderflowException异常*/
    34. public ByteBuffer get(byte[] dst, int offset, int length) {
    35. checkBounds(offset, length, dst.length);
    36. if (length > remaining())
    37. throw new BufferUnderflowException();
    38. int end = offset + length;
    39. for (int i = offset; i < end; i++)
    40. dst[i] = get(); //这个不带参数的get将会调用HeapByteBuffer的get方法,所以他会需要自增position.
    41. return this;
    42. }
    43. public final int remaining() {
    44. return limit - position;
    45. }
    46. /*put方法,会改变position,抛出BufferOverflowException异常*/
    47. public abstract ByteBuffer put(byte b);
    48. public abstract ByteBuffer put(int index, byte b);
    49. /*当参数为ByteBuffer时,参数中的src的position也会改变,如果目标buffer剩余位置小于*/
    50. public ByteBuffer put(ByteBuffer src) {
    51. if (src == this)
    52. throw new IllegalArgumentException();
    53. if (isReadOnly())
    54. throw new ReadOnlyBufferException();
    55. int n = src.remaining();
    56. if (n > remaining())
    57. throw new BufferOverflowException();
    58. ......
    59. }
    60. public ByteBuffer put(byte[] src, int offset, int length) {
    61. checkBounds(offset, length, src.length);
    62. if (length > remaining())
    63. throw new BufferOverflowException();
    64. int end = offset + length;
    65. for (int i = offset; i < end; i++)
    66. this.put(src[i]);
    67. return this;
    68. }
    69. /*rewind,重置position*/
    70. public Buffer rewind() {
    71. position = 0;
    72. mark = -1;
    73. return this;
    74. }
    75. /*flip方法要注意,他不但会改变position,还会改变limit,所以在put、get、remaining前后用这个要慎重*/
    76. public Buffer flip() {
    77. limit = position;
    78. position = 0;
    79. mark = -1;
    80. return this;
    81. }
    82. /*position,直接设置position*/
    83. public Buffer position(int newPosition) {
    84. if ((newPosition > limit) || (newPosition < 0))
    85. // Android-changed: Improved error message.
    86. throw new IllegalArgumentException("Bad position " + newPosition + "/" + limit);
    87. position = newPosition;
    88. if (mark > position) mark = -1;
    89. return this;
    90. }
    91. }

    ByteBuffer有两种实现方式:HeapByteBuffer基于Java堆的实现,而DirectByteBuffer 使用了 unsafed 的API 进行了对外的实现。HeapByteBuffer就是创建效率高,读取和写入的效率低。DirectByteBuffer是创建效率低,读取和写入的效率高。

  • 相关阅读:
    初心、真心
    使用Spring Data Elasticsearch 进行索引的增、删、改、查
    【无标题】
    ChatGPT3.5使用体验
    优雅的接口防刷处理方案
    获取远程仓库的信息和远程分支的信息
    【Reinforcement Learning】Ubuntu中mujoco210 mujoco_py D4RL安装及错误解决
    Jupyter installation Tutorial
    FindMy网络帮助您找到电动车
    【C语言】指针笔试题解析
  • 原文地址:https://blog.csdn.net/linchuanzhi_886/article/details/126749412