~ubuntu-branches/ubuntu/karmic/libgnuinet-java/karmic

« back to all changes in this revision

Viewing changes to source/gnu/inet/util/CRLFOutputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-04-14 12:42:10 UTC
  • Revision ID: james.westby@ubuntu.com-20040414124210-osc3q0wzthgme27p
Tags: upstream-0.0.cvs20031116
ImportĀ upstreamĀ versionĀ 0.0.cvs20031116

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: CRLFOutputStream.java,v 1.2 2003/10/19 16:16:50 dog Exp $
 
3
 * Copyright (C) 2002 The Free Software Foundation
 
4
 * 
 
5
 * This file is part of GNU inetlib, a library.
 
6
 * 
 
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.
 
11
 * 
 
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.
 
16
 * 
 
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
 
20
 *
 
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.
 
26
 */
 
27
 
 
28
package gnu.inet.util;
 
29
 
 
30
import java.io.FilterOutputStream;
 
31
import java.io.IOException;
 
32
import java.io.OutputStream;
 
33
import java.io.UnsupportedEncodingException;
 
34
 
 
35
/** 
 
36
 * An output stream that filters LFs into CR/LF pairs.
 
37
 *
 
38
 * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
 
39
 * @author $Revision: 1.2 $ $Date: 2003/10/19 16:16:50 $
 
40
 */
 
41
public class CRLFOutputStream extends FilterOutputStream
 
42
{
 
43
 
 
44
  static final String US_ASCII = "US-ASCII";
 
45
 
 
46
  /**
 
47
   * The CR octet.
 
48
   */
 
49
  public static final int CR = 13;
 
50
 
 
51
  /**
 
52
   * The LF octet.
 
53
   */
 
54
  public static final int LF = 10;
 
55
 
 
56
  /**
 
57
   * The CR/LF pair.
 
58
   */
 
59
  public static final byte[] CRLF = { CR, LF };
 
60
 
 
61
  /**
 
62
   * The last byte read.
 
63
   */
 
64
  protected int last;
 
65
 
 
66
  /**
 
67
   * Constructs a CR/LF output stream connected to the specified output stream.
 
68
   */
 
69
  public CRLFOutputStream(OutputStream out)
 
70
  {
 
71
    super(out);
 
72
    last = -1;
 
73
  }
 
74
 
 
75
  /**
 
76
   * Writes a character to the underlying stream.
 
77
   * @exception IOException if an I/O error occurred
 
78
   */
 
79
  public void write(int ch) throws IOException
 
80
  {
 
81
    if (ch == CR)
 
82
      out.write(CRLF);
 
83
    else if (ch == LF)
 
84
    {
 
85
      if (last != CR)
 
86
        out.write(CRLF);
 
87
    }
 
88
    else
 
89
        out.write(ch);
 
90
    last = ch;
 
91
  }
 
92
 
 
93
  /**
 
94
   * Writes a byte array to the underlying stream.
 
95
   * @exception IOException if an I/O error occurred
 
96
   */
 
97
  public void write(byte b[]) throws IOException
 
98
  {
 
99
    write(b, 0, b.length);
 
100
  }
 
101
 
 
102
  /**
 
103
   * Writes a portion of a byte array to the underlying stream.
 
104
   * @exception IOException if an I/O error occurred
 
105
   */
 
106
  public void write(byte b[], int off, int len) throws IOException
 
107
  {
 
108
    int d = off;
 
109
      len += off;
 
110
    for (int i = off; i < len; i++)
 
111
    {
 
112
      switch (b[i])
 
113
      {
 
114
      case CR:
 
115
        out.write(b, d, i - d);
 
116
        out.write(CRLF, 0, 2);
 
117
        d = i + 1;
 
118
        break;
 
119
        case LF:if (last != CR)
 
120
        {
 
121
          out.write(b, d, i - d);
 
122
          out.write(CRLF, 0, 2);
 
123
        }
 
124
        d = i + 1;
 
125
        break;
 
126
      }
 
127
      last = b[i];
 
128
    }
 
129
    if (len - d > 0)
 
130
      out.write(b, d, len - d);
 
131
  }
 
132
 
 
133
  /**
 
134
   * Writes the specified ASCII string to the underlying stream.
 
135
   * @exception IOException if an I/O error occurred
 
136
   */
 
137
  public void write(String text) throws IOException
 
138
  {
 
139
    try
 
140
    {
 
141
      byte[]bytes = text.getBytes(US_ASCII);
 
142
      write(bytes, 0, bytes.length);
 
143
    }
 
144
    catch(UnsupportedEncodingException e)
 
145
    {
 
146
      throw new IOException("Not ASCII: " + text);
 
147
    }
 
148
  }
 
149
 
 
150
  /**
 
151
   * Writes a newline to the underlying stream.
 
152
   * @exception IOException if an I/O error occurred
 
153
   */
 
154
  public void writeln() throws IOException
 
155
  {
 
156
    out.write(CRLF);
 
157
  }
 
158
 
 
159
}