⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 safeprintwriter.java

📁 一个仿qq的程序源码 一个用纯java开发的
💻 JAVA
字号:
package qianqian.p2pchat.io;

import java.io.Writer;
import java.io.OutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

//安全的PrintWriter
public class SafePrintWriter extends Writer {
	protected Writer out;

	private boolean autoFlush = false;

	private String lineSeparator;

	private boolean closed = false;

	// 构造子1
	public SafePrintWriter(Writer out, String lineSeparator) {
		this(out, false, lineSeparator);
	}

	// 构造子2
	public SafePrintWriter(Writer out, char lineSeparator) {
		this(out, false, String.valueOf(lineSeparator));
	}

	// 构造子3
	public SafePrintWriter(Writer out, boolean autoFlush, String lineSeparator) {
		super(out);
		this.out = out;
		this.autoFlush = autoFlush;
		if (lineSeparator == null) {
			throw new NullPointerException("Null line separator");
		}
		this.lineSeparator = lineSeparator;
	}

	// 构造子4
	public SafePrintWriter(OutputStream out, boolean autoFlush,
			String encoding, String lineSeparator)
			throws UnsupportedEncodingException {
		this(new OutputStreamWriter(out, encoding), autoFlush, lineSeparator);
	}

	// 覆盖父类刷新流方法
	public void flush() throws IOException {
		synchronized (lock) {
			if (closed)
				throw new IOException("Stream closed");
			out.flush();
		}
	}

	// 覆盖父类关闭流方法
	public void close() throws IOException {
		try {
			this.flush();
		} catch (IOException ex) {
			// do nothing
		}
		synchronized (lock) {
			out.close();
			this.closed = true;
		}
	}

	// 写入整型数据
	public void write(int c) throws IOException {
		synchronized (lock) {
			if (closed)
				throw new IOException("Stream closed");
			out.write(c);
		}
	}

	// 写入字符数组
	public void write(char[] text, int offset, int length) throws IOException {
		synchronized (lock) {
			if (closed)
				throw new IOException("Stream closed");
			out.write(text, offset, length);
		}
	}

	// 写入字符数组
	public void write(char[] text) throws IOException {
		synchronized (lock) {
			if (closed)
				throw new IOException("Stream closed");
			out.write(text, 0, text.length);
		}
	}

	// 写入字符串
	public void write(String s, int offset, int length) throws IOException {
		synchronized (lock) {
			if (closed)
				throw new IOException("Stream closed");
			out.write(s, offset, length);
		}
	}

	// 写入布尔数据
	public void print(boolean b) throws IOException {
		if (b)
			this.write("true");
		else
			this.write("false");
	}

	// 写入布尔数据
	public void println(boolean b) throws IOException {
		if (b)
			this.write("true" + lineSeparator);
		else
			this.write("false" + lineSeparator);
		if (autoFlush)
			out.flush();
	}

	// 写入字符
	public void print(char c) throws IOException {
		this.write(String.valueOf(c));
	}

	// 写入字符
	public void println(char c) throws IOException {
		this.write(c + lineSeparator);
		this.write(lineSeparator);
		if (autoFlush)
			out.flush();
	}

	// 写入整型数据
	public void print(int i) throws IOException {
		this.write(String.valueOf(i));
	}

	// 写入整型数据
	public void println(int i) throws IOException {
		this.write(i + lineSeparator);
		if (autoFlush)
			out.flush();
	}

	// 写入长整型数据
	public void print(long l) throws IOException {
		this.write(String.valueOf(l));
	}

	// 写入长整型数据
	public void println(long l) throws IOException {
		this.write(l + lineSeparator);
		if (autoFlush)
			out.flush();
	}

	// 写入浮点数据
	public void print(float f) throws IOException {
		this.write(String.valueOf(f));
	}

	// 写入浮点数据
	public void println(float f) throws IOException {
		this.write(f + lineSeparator);
		if (autoFlush)
			out.flush();
	}

	// 写入双精度数据
	public void print(double d) throws IOException {
		this.write(String.valueOf(d));
	}

	// 写入双精度数据
	public void println(double d) throws IOException {
		this.write(d + lineSeparator);
		if (autoFlush)
			out.flush();
	}

	// 写入字符数组
	public void print(char[] text) throws IOException {
		this.write(String.valueOf(text));
	}

	// 写入字符数组
	public void println(char[] text) throws IOException {
		this.write(text);
		this.write(lineSeparator);
		if (autoFlush)
			out.flush();
	}

	// 写入字符串
	public void print(String s) throws IOException {
		if (s == null)
			this.write("null");
		else
			this.write(s);
	}

	// 写入字符串
	public void println(String s) throws IOException {
		if (s == null)
			this.write("null");
		else
			this.write(s + lineSeparator);
		if (autoFlush)
			out.flush();
	}

	// 写入对象
	public void print(Object o) throws IOException {
		if (o == null)
			this.write("null");
		else
			this.write(o.toString());
	}

	// 写入对象
	public void println(Object o) throws IOException {
		if (o == null)
			this.write("null");
		else
			this.write(o.toString() + lineSeparator);
		if (autoFlush)
			out.flush();
	}

	// 空白写入
	public void println() throws IOException {
		this.write(lineSeparator);
		if (autoFlush)
			out.flush();
	}
}

⌨️ 快捷键说明

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