程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(2)

java io BufferedWriter源码分析

发布于2020-11-19 20:48     阅读(624)     评论(0)     点赞(11)     收藏(2)


目录

简介

字段out,cb,nChars,nextChar,defaultCharBufferSize,lineSeparator,两个构造函数

方法ensureOpen,flushBuffer,write,min

2个write方法,方法newLine,flush,close


简介

  1. package java.io;
  2. /**
  3. * 将文本写入字符输出流,缓冲字符,以便有效地编写单个字符、数组和字符串。
  4. *
  5. * <p> 可以指定缓冲区大小,也可以接受默认大小。
  6. * 缺省值对于大多数用途来说已经足够大了。
  7. *
  8. * <p>提供了一个newLine()方法,它使用平台自己的线分隔符概念,由系统属性line.separator定义。
  9. * 不是所有平台都使用换行符('\n')来结束行。
  10. * 因此,最好调用这个方法来终止每个输出行,直接生成换行符。
  11. *
  12. * <p>一般来说,写入器立即将其输出发送到底层字符流或字节流。
  13. * 除非需要立刻输出,否则建议将BufferedWriter包裹在任何可能
  14. * 需要write()操作的写入器周围,比如FileWriters和OutputStreamWriters。例如,
  15. *
  16. * <pre>
  17. * PrintWriter out
  18. * = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
  19. * </pre>
  20. *
  21. * 将缓冲PrintWriter的输出到文件中。
  22. * 如果没有缓冲,每次调用print()方法都会导致字符被转换成字节,然后立即写入文件,这可能非常低效。
  23. *
  24. * @see PrintWriter
  25. * @see FileWriter
  26. * @see OutputStreamWriter
  27. * @see java.nio.file.Files#newBufferedWriter
  28. *
  29. * @author Mark Reinhold
  30. * @since JDK1.1
  31. */
  32. public class BufferedWriter extends Writer

字段out,cb,nChars,nextChar,defaultCharBufferSize,lineSeparator,两个构造函数

  1. // 内部有一个Writer,类似于装饰器模式
  2. private Writer out;
  3. // 缓存数组cb
  4. private char cb[];
  5. // cb的大小和下一个写入的index
  6. private int nChars, nextChar;
  7. // 默认缓存大小,8192个char
  8. private static int defaultCharBufferSize = 8192;
  9. /**
  10. * 行分隔符字符串。
  11. * 这是在流创建时的line.separator的值。
  12. */
  13. private String lineSeparator;
  14. /**
  15. * 创建使用默认大小的输出缓冲区的缓冲字符输出流。
  16. *
  17. * @param out A Writer
  18. */
  19. public BufferedWriter(Writer out) {
  20. this(out, defaultCharBufferSize);
  21. }
  22. /**
  23. * 创建一个新的缓冲字符输出流,该流使用给定大小的输出缓冲区。
  24. *
  25. * @param out A Writer
  26. * @param sz Output-buffer size, a positive integer
  27. *
  28. * @exception IllegalArgumentException If {@code sz <= 0}
  29. */
  30. public BufferedWriter(Writer out, int sz) {
  31. // 以out作为lock
  32. super(out);
  33. if (sz <= 0)
  34. throw new IllegalArgumentException("Buffer size <= 0");
  35. // 设置out和sz大小的cb
  36. this.out = out;
  37. cb = new char[sz];
  38. // nChars为cb的长度,nextChar为0
  39. nChars = sz;
  40. nextChar = 0;
  41. // line.separator的值
  42. lineSeparator = java.security.AccessController.doPrivileged(
  43. new sun.security.action.GetPropertyAction("line.separator"));
  44. }

方法ensureOpen,flushBuffer,write,min

  1. /** 检查以确保流没有被关闭 */
  2. private void ensureOpen() throws IOException {
  3. if (out == null)
  4. throw new IOException("Stream closed");
  5. }
  6. /**
  7. * 将输出缓冲区刷新到基础字符流,而不刷新流本身。
  8. * 这个方法是非私有的,所以它可以被PrintStream调用。
  9. */
  10. void flushBuffer() throws IOException {
  11. synchronized (lock) {
  12. ensureOpen();
  13. if (nextChar == 0)
  14. return;
  15. // out中写入cb[0,nextChar]
  16. out.write(cb, 0, nextChar);
  17. // 指针重新设置为0
  18. nextChar = 0;
  19. }
  20. }
  21. /**
  22. * 写入单个字符
  23. *
  24. * @exception IOException If an I/O error occurs
  25. */
  26. public void write(int c) throws IOException {
  27. synchronized (lock) {
  28. ensureOpen();
  29. // 如果缓冲区溢出,将缓冲区刷入底层writer
  30. if (nextChar >= nChars)
  31. flushBuffer();
  32. // nextChar处设置c,然后nextChar++
  33. cb[nextChar++] = (char) c;
  34. }
  35. }
  36. /**
  37. * 我们自己的min方法,如果我们已经用完文件描述符,试图打印堆栈跟踪,避免加载java.lang.Math。
  38. */
  39. private int min(int a, int b) {
  40. if (a < b) return a;
  41. return b;
  42. }

2个write方法,方法newLine,flush,close

  1. /**
  2. * 写入字符数组的一部分。
  3. *
  4. * <p> 通常,该方法将给定数组中的字符存储到该流的缓冲区中,根据需要将缓冲区刷新到底层流。
  5. * 但是,如果请求的长度至少与缓冲区一样大,则此方法将刷新缓冲区并直接将字符写入底层流。
  6. * 因此冗余的BufferedWriter不会复制不必要的数据。
  7. *
  8. * @param cbuf A character array
  9. * @param off Offset from which to start reading characters
  10. * @param len Number of characters to write
  11. *
  12. * @exception IOException If an I/O error occurs
  13. */
  14. public void write(char cbuf[], int off, int len) throws IOException {
  15. synchronized (lock) {
  16. ensureOpen();
  17. if ((off < 0) || (off > cbuf.length) || (len < 0) ||
  18. ((off + len) > cbuf.length) || ((off + len) < 0)) {
  19. throw new IndexOutOfBoundsException();
  20. } else if (len == 0) {
  21. return;
  22. }
  23. if (len >= nChars) {
  24. /* 如果请求长度超过输出缓冲区的大小,则刷新缓冲区,然后直接写入数据。
  25. 以这种方式缓冲的流将无害地倾泻。
  26. */
  27. flushBuffer();
  28. out.write(cbuf, off, len);
  29. return;
  30. }
  31. int b = off, t = off + len;
  32. // 当len>0,进行循环
  33. while (b < t) {
  34. // 每次复制min(nChars-nextChar,len)个元素到cb
  35. int d = min(nChars - nextChar, t - b);
  36. System.arraycopy(cbuf, b, cb, nextChar, d);
  37. b += d;
  38. nextChar += d;
  39. if (nextChar >= nChars)
  40. flushBuffer();
  41. }
  42. }
  43. }
  44. /**
  45. * 写入字符串的一部分。
  46. *
  47. * <p> 如果len参数的值为负,则不写入字符。
  48. * 这与父类中write(String,int,int)方法的说明相反,
  49. * 它要求抛出一个IndexOutOfBoundsException。
  50. *
  51. * @param s String to be written
  52. * @param off Offset from which to start reading characters
  53. * @param len Number of characters to be written
  54. *
  55. * @exception IOException If an I/O error occurs
  56. */
  57. public void write(String s, int off, int len) throws IOException {
  58. synchronized (lock) {
  59. ensureOpen();
  60. int b = off, t = off + len;
  61. while (b < t) {
  62. // 每次复制min(nChars-nextChar,len)个元素到cb
  63. int d = min(nChars - nextChar, t - b);
  64. s.getChars(b, b + d, cb, nextChar);
  65. b += d;
  66. nextChar += d;
  67. if (nextChar >= nChars)
  68. flushBuffer();
  69. }
  70. }
  71. }
  72. /**
  73. * 写入行分隔符。行分隔符字符串由系统属性line.separator行定义。
  74. * ,不一定是单一的newline ('\n')字符。
  75. *
  76. * @exception IOException If an I/O error occurs
  77. */
  78. public void newLine() throws IOException {
  79. write(lineSeparator);
  80. }
  81. /**
  82. * 刷新流
  83. *
  84. * @exception IOException If an I/O error occurs
  85. */
  86. public void flush() throws IOException {
  87. synchronized (lock) {
  88. // 把缓存写入out,然后底层的out进行flush
  89. flushBuffer();
  90. out.flush();
  91. }
  92. }
  93. @SuppressWarnings("try")
  94. public void close() throws IOException {
  95. synchronized (lock) {
  96. if (out == null) {
  97. return;
  98. }
  99. // 缓存写入out后,设置out,cb为null
  100. try (Writer w = out) {
  101. flushBuffer();
  102. } finally {
  103. out = null;
  104. cb = null;
  105. }
  106. }
  107. }

 

原文链接:https://blog.csdn.net/xushiyu1996818/article/details/109719885



所属网站分类: 技术文章 > 博客

作者:我爱java

链接:http://www.javaheidong.com/blog/article/957/69c4f009f9cdc603098c/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

11 0
收藏该文
已收藏

评论内容:(最多支持255个字符)