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

📄 unicodewriter.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 09/24/2004
 *
 * UnicodeWriter.java - Writes Unicode output with the proper BOM.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.io;

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


/**
 * Writes Unicode text to an output stream.  If the specified encoding is a
 * Unicode, then the text is preceeded by the proper Unicode BOM.  If it is any
 * other encoding, this class behaves just like <code>OutputStreamWriter</code>.
 * This class is here because Java's <code>OutputStreamWriter</code> apparently
 * doesn't believe in writing BOMs.
 * <p>
 *
 * For optimum performance, it is recommended that you wrap all instances of
 * <code>UnicodeWriter</code> with a <code>java.io.BufferedWriter</code>.
 *
 * @author Robert Futrell
 * @version 0.7
 */
public class UnicodeWriter extends Writer {

	/**
	 * The writer actually doing the writing.
	 */
	private OutputStreamWriter internalOut;

	private static final byte[] UTF8_BOM = new byte[] {
												(byte)0xEF,
												(byte)0xBB,
												(byte)0xBF
											};

	private static final byte[] UTF16LE_BOM = new byte[] { 
												(byte)0xFF,
												(byte)0xFE
											};

	private static final byte[] UTF16BE_BOM = new byte[] {
												(byte)0xFE,
												(byte)0xFF
											};


/*****************************************************************************/


	/**
	 * This is a utility constructor since the vast majority of the time, this
	 * class will be used to write Unicode files.
	 *
	 * @param fileName The file to which to write the Unicode output.
	 * @param encoding The encoding to use.
	 * @throws UnsupportedEncodingException If the specified encoding is not
	 *                                      supported.
	 * @throws IOException If an IO exception occurs.
	 */
	public UnicodeWriter(String fileName, String encoding)
								throws UnsupportedEncodingException,
									IOException {
		this(new FileOutputStream(fileName), encoding);
	}



/*****************************************************************************/


	/**
	 * This is a utility constructor since the vast majority of the time, this
	 * class will be used to write Unicode files.
	 *
	 * @param file The file to which to write the Unicode output.
	 * @param encoding The encoding to use.
	 * @throws UnsupportedEncodingException If the specified encoding is not
	 *                                      supported.
	 * @throws IOException If an IO exception occurs.
	 */
	public UnicodeWriter(File file, String encoding)
								throws UnsupportedEncodingException,
									IOException {
		this(new FileOutputStream(file), encoding);
	}



/*****************************************************************************/


	/**
	 * Creates a new writer.
	 *
	 * @param out The output stream to write.
	 * @param encoding The encoding to use.
	 * @throws UnsupportedEncodingException If the specified encoding is not
	 *                                      supported.
	 * @throws IOException If an IO exception occurs.
	 */
	public UnicodeWriter(OutputStream out, String encoding)
								throws UnsupportedEncodingException,
								IOException {
		init(out, encoding);
	}


/*****************************************************************************/


	/**
	 * Closes this writer.
	 *
	 * @throws IOException If an IO exception occurs.
	 */
	public void close() throws IOException {
		if (internalOut!=null)
			internalOut.close();
	}


/*****************************************************************************/


	/**
	 * Flushes the stream.
	 *
	 * @throws IOException If an IO exception occurs.
	 */
	public void flush() throws IOException {
		if (internalOut!=null)
			internalOut.flush();
	}


/*****************************************************************************/


	/**
	 * Returns the encoding being used to write this output stream (i.e., the
	 * encoding of the file).
	 *
	 * @return The encoding of the stream.
	 */
	public String getEncoding() {
		return internalOut.getEncoding();
	}


/*****************************************************************************/


	/**
	 * Initializes the internal output stream and writes the BOM if the
	 * specified encoding is a Unicode encoding.
	 *
	 * @param out The output stream we are writing.
	 * @param encoding The encoding in which to write.
	 * @throws UnsupportedEncodingException If the specified encoding isn't
	 *                                      supported.
	 * @throws IOException If an I/O error occurs while writing a BOM.
	 */
	private void init(OutputStream out, String encoding)
								throws UnsupportedEncodingException,
								IOException {

		internalOut = new OutputStreamWriter(out, encoding);

		// Write the proper BOM if they specified a Unicode encoding.
		if (encoding.equals("UTF-8"))
			out.write(UTF8_BOM, 0, UTF8_BOM.length);
		else if (encoding.equals("UTF-16LE"))
			out.write(UTF16LE_BOM, 0, UTF16LE_BOM.length);
		else if (encoding.equals("UTF-16") || encoding.equals("UTF-16BE"))
			out.write(UTF16BE_BOM, 0, UTF16BE_BOM.length);

	}


/*****************************************************************************/


	/**
	 * Writes a portion of an array of characters.
	 *
	 * @param cbuf The buffer of characters.
	 * @param off The offset from which to start writing characters.
	 * @param len The number of characters to write.
	 * @throws IOException If an I/O error occurs.
	 */
	public void write(char[] cbuf, int off, int len) throws IOException {
		if (internalOut!=null)
			internalOut.write(cbuf, off, len);
		else
			throw new IOException("internalOut is null.");
	}


/*****************************************************************************/


	/**
	 * Writes a single character.
	 *
	 * @param c An integer specifying the character to write.
	 * @throws IOException If an IO error occurs.
	 */
	public void write(int c) throws IOException {
		if (internalOut!=null)
			internalOut.write(c);
		else
			throw new IOException("internalOut is null.");
	}


/*****************************************************************************/


	/**
	 * Writes a portion of a string.
	 *
	 * @param str The string from which to write.
	 * @param off The offset from which to start writing characters.
	 * @param len The number of characters to write.
	 * @throws IOException If an IO error occurs.
	 */
	public void write(String str, int off, int len) throws IOException {
		if (internalOut!=null)
			internalOut.write(str, off, len);
		else
			throw new IOException("internalOut is null.");
	}


/*****************************************************************************/

}

⌨️ 快捷键说明

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