franta-hg@107: /* CRLFOutputStream.java -- franta-hg@107: Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. franta-hg@107: franta-hg@107: This file is part of GNU Classpath. franta-hg@107: franta-hg@107: GNU Classpath is free software; you can redistribute it and/or modify franta-hg@107: it under the terms of the GNU General Public License as published by franta-hg@107: the Free Software Foundation; either version 2, or (at your option) franta-hg@107: any later version. franta-hg@107: franta-hg@107: GNU Classpath is distributed in the hope that it will be useful, but franta-hg@107: WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@107: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU franta-hg@107: General Public License for more details. franta-hg@107: franta-hg@107: You should have received a copy of the GNU General Public License franta-hg@107: along with GNU Classpath; see the file COPYING. If not, write to the franta-hg@107: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA franta-hg@107: 02110-1301 USA. franta-hg@107: franta-hg@107: Linking this library statically or dynamically with other modules is franta-hg@107: making a combined work based on this library. Thus, the terms and franta-hg@107: conditions of the GNU General Public License cover the whole franta-hg@107: combination. franta-hg@107: franta-hg@107: As a special exception, the copyright holders of this library give you franta-hg@107: permission to link this library with independent modules to produce an franta-hg@107: executable, regardless of the license terms of these independent franta-hg@107: modules, and to copy and distribute the resulting executable under franta-hg@107: terms of your choice, provided that you also meet, for each linked franta-hg@107: independent module, the terms and conditions of the license of that franta-hg@107: module. An independent module is a module which is not derived from franta-hg@107: or based on this library. If you modify this library, you may extend franta-hg@107: this exception to your version of the library, but you are not franta-hg@107: obligated to do so. If you do not wish to do so, delete this franta-hg@107: exception statement from your version. franta-hg@107: */ franta-hg@107: package org.sonews.util.io; // original package: gnu.java.net franta-hg@107: franta-hg@107: import java.io.FilterOutputStream; franta-hg@107: import java.io.IOException; franta-hg@107: import java.io.OutputStream; franta-hg@107: import java.io.UnsupportedEncodingException; franta-hg@107: franta-hg@107: /** franta-hg@107: * An output stream that filters LFs into CR/LF pairs. franta-hg@107: * franta-hg@107: * @author Chris Burdess (dog@gnu.org) franta-hg@107: */ franta-hg@107: public class CRLFOutputStream extends FilterOutputStream { franta-hg@107: franta-hg@107: static final String US_ASCII = "US-ASCII"; franta-hg@107: /** franta-hg@107: * The CR octet. franta-hg@107: */ franta-hg@107: public static final int CR = 13; franta-hg@107: /** franta-hg@107: * The LF octet. franta-hg@107: */ franta-hg@107: public static final int LF = 10; franta-hg@107: /** franta-hg@107: * The CR/LF pair. franta-hg@107: */ franta-hg@107: public static final byte[] CRLF = {CR, LF}; franta-hg@107: /** franta-hg@107: * The last byte read. franta-hg@107: */ franta-hg@107: protected int last; franta-hg@107: franta-hg@107: /** franta-hg@107: * Constructs a CR/LF output stream connected to the specified output stream. franta-hg@107: */ franta-hg@107: public CRLFOutputStream(OutputStream out) { franta-hg@107: super(out); franta-hg@107: last = -1; franta-hg@107: } franta-hg@107: franta-hg@107: /** franta-hg@107: * Writes a character to the underlying stream. franta-hg@107: * @exception IOException if an I/O error occurred franta-hg@107: */ franta-hg@107: @Override franta-hg@107: public void write(int ch) throws IOException { franta-hg@107: if (ch == CR) { franta-hg@107: out.write(CRLF); franta-hg@107: } else if (ch == LF) { franta-hg@107: if (last != CR) { franta-hg@107: out.write(CRLF); franta-hg@107: } franta-hg@107: } else { franta-hg@107: out.write(ch); franta-hg@107: } franta-hg@107: last = ch; franta-hg@107: } franta-hg@107: franta-hg@107: /** franta-hg@107: * Writes a byte array to the underlying stream. franta-hg@107: * @exception IOException if an I/O error occurred franta-hg@107: */ franta-hg@107: @Override franta-hg@107: public void write(byte[] b) throws IOException { franta-hg@107: write(b, 0, b.length); franta-hg@107: } franta-hg@107: franta-hg@107: /** franta-hg@107: * Writes a portion of a byte array to the underlying stream. franta-hg@107: * @exception IOException if an I/O error occurred franta-hg@107: */ franta-hg@107: @Override franta-hg@107: public void write(byte[] b, int off, int len) throws IOException { franta-hg@107: int d = off; franta-hg@107: len += off; franta-hg@107: for (int i = off; i < len; i++) { franta-hg@107: switch (b[i]) { franta-hg@107: case CR: franta-hg@107: out.write(b, d, i - d); franta-hg@107: out.write(CRLF, 0, 2); franta-hg@107: d = i + 1; franta-hg@107: break; franta-hg@107: case LF: franta-hg@107: if (last != CR) { franta-hg@107: out.write(b, d, i - d); franta-hg@107: out.write(CRLF, 0, 2); franta-hg@107: } franta-hg@107: d = i + 1; franta-hg@107: break; franta-hg@107: } franta-hg@107: last = b[i]; franta-hg@107: } franta-hg@107: if (len - d > 0) { franta-hg@107: out.write(b, d, len - d); franta-hg@107: } franta-hg@107: } franta-hg@107: franta-hg@107: /** franta-hg@107: * Writes the specified ASCII string to the underlying stream. franta-hg@107: * @exception IOException if an I/O error occurred franta-hg@107: */ franta-hg@107: public void write(String text) throws IOException { franta-hg@107: try { franta-hg@107: byte[] bytes = text.getBytes(US_ASCII); franta-hg@107: write(bytes, 0, bytes.length); franta-hg@107: } catch (UnsupportedEncodingException e) { franta-hg@107: throw new IOException("The US-ASCII encoding is not supported on this system", e); franta-hg@107: } franta-hg@107: } franta-hg@107: franta-hg@107: /** franta-hg@107: * Writes a newline to the underlying stream. franta-hg@107: * @exception IOException if an I/O error occurred franta-hg@107: */ franta-hg@107: public void writeln() throws IOException { franta-hg@107: out.write(CRLF, 0, 2); franta-hg@107: } franta-hg@107: }