java的无缓冲输出_java io系列24之 BufferedWriter(字符缓冲输出流)

}

73

74 // 将字符数组cbuf写入到缓冲中,从cbuf的off位置开始写入,写入长度是len。

75 public void write(char cbuf[], int off, int len) throws IOException {

76 synchronized (lock) {

77 ensureOpen();

78 if ((off < 0) || (off > cbuf.length) || (len < 0) ||

79 ((off + len) > cbuf.length) || ((off + len) < 0)) {

80 throw new IndexOutOfBoundsException();

81 } else if (len == 0) {

82 return;

83 }

84

85 if (len >= nChars) {

86 /* If the request length exceeds the size of the output buffer,

87 flush the buffer and then write the data directly. In this

88 way buffered streams will cascade harmlessly. */

89 flushBuffer();

90 out.write(cbuf, off, len);

91 return;

92 }

93

94 int b = off, t = off + len;

95 while (b < t) {

96 int d = min(nChars - nextChar, t - b);

97 System.arraycopy(cbuf, b, cb, nextChar, d);

98 b += d;

99 nextChar += d;

100 if (nextChar >= nChars)

101 flushBuffer();

102 }

103 }

104 }

105

106 // 将字符串s写入到缓冲中,从s的off位置开始写入,写入长度是len。

107 public void write(String s, int off, int len) throws IOException {

108 synchronized (lock) {

109 ensureOpen();

110

111 int b = off, t = off + len;

112 while (b < t) {

113 int d = min(nChars - nextChar, t - b);

114 s.getChars(b, b + d, cb, nextChar);

115 b += d;

116 nextChar += d;

117 if (nextChar >= nChars)

118 flushBuffer();

119 }

120 }

121 }

122

123 // 将换行符写入到缓冲中

124 public void newLine() throws IOException {

125 write(lineSeparator);

126 }

127

128 // 清空缓冲区数据

129 public void flush() throws IOException {

130 synchronized (lock) {

131 flushBuffer();

132 out.flush();

133 }

134 }

135

136 public void close() throws IOException {

137 synchronized (lock) {

138 if (out == null) {

139 return;

140 }

141 try {

142 flushBuffer();

143 } finally {

144 out.close();

145 out = null;

146 cb = null;

147 }

148 }

149 }

150 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值