jdk源码解析--Writer类
Posted 我的IT技术路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdk源码解析--Writer类相关的知识,希望对你有一定的参考价值。
流的四个基类中,我们之前介绍了三种,字节输入流,字节输出流,字符输入流。现在我们介绍最后一种:字符输出流Writer。和其他基类一样,Writer是一个抽象类,代表着将数据从内存中写入到其他文件中。先通过一个简单的demo说明一下这个抽象类:
1. import java.io.IOException;
2. import java.io.Writer;
3. import java.util.Optional;
4.
5. public class WriterTest {
6.
7. public static void main(String[] args) throws IOException {
8. try (IntWriter intWriter = new IntWriter()){
9. intWriter.write(new char[]{'1','2','3','4'},0,4);
10. Optional.ofNullable(intWriter.getInteger()).ifPresent(System.out::println);
11. }
12.
13. }
14.
15. private static class IntWriter extends Writer {
16.
17. Integer value ;
18. @Override
19. public void write(char[] cbuf, int off, int len) {
20. long initValue = 0;
21. for (int i =0;i<len;i++){
22. if (cbuf[i]<='9' && cbuf[i]>='0'){
23. initValue = initValue*10+(cbuf[i]-'0');
24. }else {
25. break;
26. }
27. }
28. if (initValue <= Integer.MAX_VALUE && initValue >=Integer.MIN_VALUE){
29. value = (int) initValue;
30. }
31. }
32. public Integer getInteger(){
33. return value;
34. }
35.
36. @Override
37. public void flush() throws IOException {
38.
39. }
40.
41. @Override
42. public void close() throws IOException {
43.
44. }
45. }
46.
47. }
上面的例子是一个整型的输出流功能,将输入的字符串转化成一个对应的整型输出。Demo还是比较简单,如果要实现一个输出流的话,需要重写三个抽象函数,一个是关闭,刷新,写入。下面我们看下该类的类图结构:
和之前的outputstream类似,都实现了这三个接口。实际上也确实应该如此。由于类比较简单,我们就直接看下具体的实现源码:
1. public abstract class Writer implements Appendable, Closeable, Flushable {
2. private char[] writeBuffer;//写入数据的数组
3. private static final int WRITE_BUFFER_SIZE = 1024;//buffer长度
4. protected Object lock;//锁对象
5. protected Writer() {
6. this.lock = this;
7. }
8. protected Writer(Object lock) {
9. if (lock == null) {
10. throw new NullPointerException();
11. }
12. this.lock = lock;
13. }
14. public void write(int c) throws IOException {//写入一个char的ascall值
15. synchronized (lock) {
16. if (writeBuffer == null){
17. writeBuffer = new char[WRITE_BUFFER_SIZE];
18. }
19. writeBuffer[0] = (char) c;
20. write(writeBuffer, 0, 1);
21. }
22. }
23. public void write(char cbuf[]) throws IOException {//写入一个字符数组
24. write(cbuf, 0, cbuf.length);
25. }
26. abstract public void write(char cbuf[], int off, int len) throws IOException;
27. public void write(String str) throws IOException {//写入字符串
28. write(str, 0, str.length());
29. }
30. public void write(String str, int off, int len) throws IOException {//写入一个字符串,告诉起点和长度
31. synchronized (lock) {//加锁
32. char cbuf[];
33. if (len <= WRITE_BUFFER_SIZE) {//长度校验
34. if (writeBuffer == null) {
35. writeBuffer = new char[WRITE_BUFFER_SIZE];
36. }
37. cbuf = writeBuffer;
38. } else { // Don't permanently allocate very large buffers.
39. cbuf = new char[len];
40. }
41. str.getChars(off, (off + len), cbuf, 0);
42. write(cbuf, 0, len);//写入长度
43. }
44. }
45. public Writer append(CharSequence csq) throws IOException {
46. if (csq == null)
47. write("null");
48. else
49. write(csq.toString());//追加写入数据
50. return this;
51. }
52. public Writer append(CharSequence csq, int start, int end) throws IOException {
53. CharSequence cs = (csq == null ? "null" : csq);
54. write(cs.subSequence(start, end).toString());//追加写入数据
55. return this;
56. }
57. public Writer append(char c) throws IOException {
58. write(c);
59. return this;
60. }
61. abstract public void flush() throws IOException;
62. abstract public void close() throws IOException;
63.
64.
65. }
以上是关于jdk源码解析--Writer类的主要内容,如果未能解决你的问题,请参考以下文章