[关闭]
@946898963 2018-05-18T03:53:36.000000Z 字数 2475 阅读 951

Volley-PoolingByteArrayOutputStream源码解析

Android控件跟框架 Android源码分析


PoolingByteArrayOutputStream其实就是一个输出流,只不过系统的输出流在使用byte[]时,如果大小不够,会自动扩大byte[]的大小。而PoolingByteArrayOutputStream则是使用了ByteArrayPool的字节数组缓冲池,从池中获取byte[] 使用完毕后再归还。在BasicNetwork中对响应进行解析的时候使用到了该输出流。

源码分析,在注释中:

  1. /**
  2. * 继承ByteArrayOutputStream,原始 ByteArrayOutputStream 中用于接受写入 bytes 的 buf,每次空间不足时便会 new 更大容量的 byte[],而 PoolingByteArrayOutputStream 使用了 ByteArrayPool 作为 Byte[] 缓存来减少这种操作,从而提高性能。
  3. *
  4. * A variation of {@link java.io.ByteArrayOutputStream} that uses a pool of byte[] buffers instead
  5. * of always allocating them fresh, saving on heap churn.
  6. */
  7. public class PoolingByteArrayOutputStream extends ByteArrayOutputStream {
  8. /**
  9. * 默认byte[]的大小
  10. *
  11. * If the {@link #PoolingByteArrayOutputStream(ByteArrayPool)} constructor is called, this is
  12. * the default size to which the underlying byte array is initialized.
  13. */
  14. private static final int DEFAULT_SIZE = 256;
  15. /**
  16. * byte[]的缓冲池
  17. */
  18. private final ByteArrayPool mPool;
  19. /**
  20. * 带byte[]缓冲池的输出流,使用默认的byte[]长度
  21. * Constructs a new PoolingByteArrayOutputStream with a default size. If more bytes are written
  22. * to this instance, the underlying byte array will expand.
  23. */
  24. public PoolingByteArrayOutputStream(ByteArrayPool pool) {
  25. this(pool, DEFAULT_SIZE);
  26. }
  27. /**
  28. * Constructs a new {@code ByteArrayOutputStream} with a default size of {@code size} bytes. If
  29. * more than {@code size} bytes are written to this instance, the underlying byte array will
  30. * expand.
  31. *
  32. * @param size initial size for the underlying byte array. The value will be pinned to a default
  33. * minimum size.
  34. */
  35. public PoolingByteArrayOutputStream(ByteArrayPool pool, int size) {
  36. mPool = pool;
  37. /**
  38. * buf,该输出流将要内容写入的目标byte[],如果多次写入,buf长度不够的时候需要扩展长度
  39. */
  40. buf = mPool.getBuf(Math.max(size, DEFAULT_SIZE));
  41. }
  42. @Override
  43. public void close() throws IOException {
  44. //当输出流关闭的时候,释放该byte[]回到byte缓冲池中
  45. mPool.returnBuf(buf);
  46. buf = null;
  47. super.close();
  48. }
  49. @Override
  50. public void finalize() {
  51. //当垃圾回收机制准备回收该输出流时,将该byte[]回收到缓冲池
  52. mPool.returnBuf(buf);
  53. }
  54. /**
  55. * Ensures there is enough space in the buffer for the given number of additional bytes.
  56. * 扩充当前输出流正在使用的byte[]的大小
  57. */
  58. private void expand(int i) {
  59. /* Can the buffer handle @i more bytes, if not expand it */
  60. if (count + i <= buf.length) {
  61. //当已经写入的字节数加上预计扩展的字节数之和,仍然不大于当前的byte[]的长度时,不需要扩展
  62. return;
  63. }
  64. //当当前的byte[]不再满足时,需要从byte[]缓冲池中获取一个byte[],大小为(count + i) * 2
  65. byte[] newbuf = mPool.getBuf((count + i) * 2);
  66. //将当前的byte[]内容复制到新的byte[]中
  67. System.arraycopy(buf, 0, newbuf, 0, count);
  68. //将旧的byte[]进行回收到byte[]缓冲池中
  69. mPool.returnBuf(buf);
  70. buf = newbuf;
  71. }
  72. /**
  73. * 从buffer的offset位置读len长度的内容,写入到输出流的byte[](buf)中
  74. */
  75. @Override
  76. public synchronized void write(byte[] buffer, int offset, int len) {
  77. expand(len);
  78. super.write(buffer, offset, len);
  79. }
  80. @Override
  81. public synchronized void write(int oneByte) {
  82. //扩展1个字节长度
  83. expand(1);
  84. super.write(oneByte);
  85. }
  86. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注