quickwriter.java

来自「xstream是一个把java object序列化成xml文件的开源库,轻便好用」· Java 代码 · 共 86 行

JAVA
86
字号
package com.thoughtworks.xstream.core.util;import com.thoughtworks.xstream.io.StreamException;import java.io.IOException;import java.io.Writer;public class QuickWriter {    private final Writer writer;    private char[] buffer;    private int pointer;    public QuickWriter(Writer writer) {        this.writer = writer;        buffer = new char[1024];    }    public QuickWriter(Writer writer, int bufferSize) {        this.writer = writer;        buffer = new char[bufferSize];    }    public void write(String str) {        int len = str.length();        if (pointer + len >= buffer.length) {            flush();            if (len > buffer.length) {                raw(str.toCharArray());                return;            }        }        str.getChars(0, len, buffer, pointer);        pointer += len;    }    public void write(char c) {        if (pointer + 1 >= buffer.length) {            flush();        }        buffer[pointer++] = c;    }    public void write(char[] c) {        int len = c.length;        if (pointer + len >= buffer.length) {            flush();            if (len > buffer.length) {                raw(c);                return;            }        }        System.arraycopy(c, 0, buffer, pointer, len);        pointer += len;    }    public void flush() {        try {            writer.write(buffer, 0, pointer);            pointer = 0;            writer.flush();        } catch (IOException e) {            throw new StreamException(e);        }    }    public void close() {        try {            writer.write(buffer, 0, pointer);            pointer = 0;            writer.close();        } catch (IOException e) {            throw new StreamException(e);        }    }    private void raw(char[] c) {        try {            writer.write(c);            writer.flush();        } catch (IOException e) {            throw new StreamException(e);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?