2
* $Id: CRLFOutputStream.java,v 1.2 2003/10/19 16:16:50 dog Exp $
3
* Copyright (C) 2002 The Free Software Foundation
5
* This file is part of GNU inetlib, a library.
7
* GNU inetlib is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* GNU inetlib is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this library; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
* As a special exception, if you link this library with other files to
22
* produce an executable, this library does not by itself cause the
23
* resulting executable to be covered by the GNU General Public License.
24
* This exception does not however invalidate any other reasons why the
25
* executable file might be covered by the GNU General Public License.
28
package gnu.inet.util;
30
import java.io.FilterOutputStream;
31
import java.io.IOException;
32
import java.io.OutputStream;
33
import java.io.UnsupportedEncodingException;
36
* An output stream that filters LFs into CR/LF pairs.
38
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
39
* @author $Revision: 1.2 $ $Date: 2003/10/19 16:16:50 $
41
public class CRLFOutputStream extends FilterOutputStream
44
static final String US_ASCII = "US-ASCII";
49
public static final int CR = 13;
54
public static final int LF = 10;
59
public static final byte[] CRLF = { CR, LF };
67
* Constructs a CR/LF output stream connected to the specified output stream.
69
public CRLFOutputStream(OutputStream out)
76
* Writes a character to the underlying stream.
77
* @exception IOException if an I/O error occurred
79
public void write(int ch) throws IOException
94
* Writes a byte array to the underlying stream.
95
* @exception IOException if an I/O error occurred
97
public void write(byte b[]) throws IOException
99
write(b, 0, b.length);
103
* Writes a portion of a byte array to the underlying stream.
104
* @exception IOException if an I/O error occurred
106
public void write(byte b[], int off, int len) throws IOException
110
for (int i = off; i < len; i++)
115
out.write(b, d, i - d);
116
out.write(CRLF, 0, 2);
119
case LF:if (last != CR)
121
out.write(b, d, i - d);
122
out.write(CRLF, 0, 2);
130
out.write(b, d, len - d);
134
* Writes the specified ASCII string to the underlying stream.
135
* @exception IOException if an I/O error occurred
137
public void write(String text) throws IOException
141
byte[]bytes = text.getBytes(US_ASCII);
142
write(bytes, 0, bytes.length);
144
catch(UnsupportedEncodingException e)
146
throw new IOException("Not ASCII: " + text);
151
* Writes a newline to the underlying stream.
152
* @exception IOException if an I/O error occurred
154
public void writeln() throws IOException