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

📄 newlinewriter.java

📁 java语言开发的P2P流媒体系统
💻 JAVA
字号:
/* 
 * P2P-Radio - Peer to peer streaming system
 * Project homepage: http://p2p-radio.sourceforge.net/
 * Copyright (C) 2003-2004 Michael Kaufmann <hallo@michael-kaufmann.ch>
 * 
 * ---------------------------------------------------------------------------
 * 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
 * (at your option) 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 p2pradio.io;

import java.io.IOException;
import java.io.Writer;


/**
 * This is an extended {@link java.io.Writer} that deals with line end marks.
 * <P>
 * It writes custom strings at a line end, for example a LF (Unix-Style)
 * or a CRLF (Windows-Style, used by the Hypertext Transfer Protocol).
 *
 * This class can be used to implement an HTTP client or server,
 * because lines in HTTP have to be terminated by a CRLF.
 * You must not use a {@link java.io.PrintWriter} for a HTTP connection,
 * because it adds a system-dependent newline character sequence
 * (system property <TT>line.separator</TT>).
 * 
 * @author Michael Kaufmann
 */
public class NewLineWriter extends Writer
{
	/**
	 * The parent {@link java.io.Writer}.
	 */
	protected Writer parent;
	
	/**
	 * The character LF (Line Feed). The value of this String is <TT>\n</TT>. 
	 */
	public static final String LF = "\n"; //$NON-NLS-1$

	/**
	 * The character CR (Carriage Return) followed by the character LF (Line Feed). The value of this String is <TT>\r\n</TT>. 
	 */
	public static final String CRLF = "\r\n"; //$NON-NLS-1$

	/**
	 * The String that marks a new line.
	 */
	protected String newLineMark;

	/**
	 * Creates a NewLineWriter that will write its data to
	 * a parent writer.
	 * 
	 * @param parent The parent Writer
	 * @param newLineMark The String to write at a line end, for example {@link #CRLF} or {@link #LF}.  
	 */
	public NewLineWriter(Writer parent, String newLineMark)
	{
		super();
		this.parent = parent;
		this.newLineMark = newLineMark;
	}

	/**
	 * Creates a NewLineWriter that will write its data to
	 * a parent writer and whose critical sections will synchronize on the given object. 
	 *	
	 * @param parent The parent Writer
	 * @param lock Object to synchronize on
	 * @param newLineMark The String to write at a line end, for example {@link #CRLF} or {@link #LF}.  
	 */
	public NewLineWriter(Writer parent, Object lock, String newLineMark)
	{
		super(lock);
		this.parent = parent;
		this.newLineMark = newLineMark;
	}
	
	public void write(int c) throws IOException
	{
		parent.write(c);
	}
	
	public void write(char[] cbuf) throws IOException
	{
		parent.write(cbuf);
	}

	public void write(char[] cbuf, int off, int len) throws IOException
	{
		parent.write(cbuf, off, len);
	}
		
	public void write(String str) throws IOException
	{
		parent.write(str);
	}
	
	/**
	 * Writes a string and adds the current new line mark to it.
	 */
	public void writeln(String str) throws IOException
	{
		parent.write(str + newLineMark);
	}
	
	/**
	 * Writes the current new line mark.
	 */
	public void writeln() throws IOException
	{
		parent.write(newLineMark);
	}
	
	public void write(String str, int off, int len) throws IOException
	{
		parent.write(str, off, len);
	}

	public void flush() throws IOException
	{
		parent.flush();
	}

	public void close() throws IOException
	{
		parent.close();
	}
	
	public void setNewLineMark(String newLineMark)
	{
		this.newLineMark = newLineMark;
	}
}

⌨️ 快捷键说明

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