/[classpath]/inetlib/source/gnu/inet/util/CRLFOutputStream.java
ViewVC logotype

Diff of /inetlib/source/gnu/inet/util/CRLFOutputStream.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by dog, Thu Oct 21 15:21:55 2004 UTC revision 1.7 by dog, Thu Nov 25 22:15:05 2004 UTC
# Line 1  Line 1 
1  /*  /*
2   * $Id$   * CRLFOutputStream.java
3   * Copyright (C) 2002 The Free Software Foundation   * Copyright (C) 2002 The Free Software Foundation
4   *   *
5   * This file is part of GNU inetlib, a library.   * This file is part of GNU inetlib, a library.
# Line 47  import java.io.UnsupportedEncodingExcept Line 47  import java.io.UnsupportedEncodingExcept
47   * An output stream that filters LFs into CR/LF pairs.   * An output stream that filters LFs into CR/LF pairs.
48   *   *
49   * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>   * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
  * @author $Revision$ $Date$  
50   */   */
51  public class CRLFOutputStream extends FilterOutputStream  public class CRLFOutputStream
52      extends FilterOutputStream
53  {  {
54    
55    static final String US_ASCII = "US-ASCII";    static final String US_ASCII = "US-ASCII";
# Line 77  public class CRLFOutputStream extends Fi Line 77  public class CRLFOutputStream extends Fi
77    /**    /**
78     * Constructs a CR/LF output stream connected to the specified output stream.     * Constructs a CR/LF output stream connected to the specified output stream.
79     */     */
80    public CRLFOutputStream (OutputStream out)    public CRLFOutputStream(OutputStream out)
81      {    {
82        super (out);      super(out);
83        last = -1;      last = -1;
84      }    }
85    
86    /**    /**
87     * Writes a character to the underlying stream.     * Writes a character to the underlying stream.
88     * @exception IOException if an I/O error occurred     * @exception IOException if an I/O error occurred
89     */     */
90    public void write (int ch) throws IOException    public void write(int ch) throws IOException
91      {    {
92        if (ch == CR)      if (ch == CR)
93          {        {
94            out.write (CRLF);          out.write(CRLF);
95          }        }
96        else if (ch == LF)      else if (ch == LF)
97          {        {
98            if (last != CR)          if (last != CR)
99              {            {
100                out.write (CRLF);              out.write(CRLF);
101              }            }
102          }        }
103        else      else
104          {        {
105            out.write (ch);          out.write(ch);
106          }        }
107        last = ch;      last = ch;
108      }    }
109      
110    /**    /**
111     * Writes a byte array to the underlying stream.     * Writes a byte array to the underlying stream.
112     * @exception IOException if an I/O error occurred     * @exception IOException if an I/O error occurred
113     */     */
114    public void write(byte[] b) throws IOException    public void write(byte[] b)
115      {      throws IOException
116        write (b, 0, b.length);    {
117      }      write(b, 0, b.length);
118      }
119    
120    /**    /**
121     * Writes a portion of a byte array to the underlying stream.     * Writes a portion of a byte array to the underlying stream.
122     * @exception IOException if an I/O error occurred     * @exception IOException if an I/O error occurred
123     */     */
124    public void write (byte[] b, int off, int len) throws IOException    public void write(byte[] b, int off, int len)
125      {      throws IOException
126        int d = off;    {
127        len += off;      int d = off;
128        for (int i = off; i < len; i++)      len += off;
129          {      for (int i = off; i < len; i++)
130            switch (b[i])        {
131              {          switch (b[i])
132              case CR:            {
133                out.write (b, d, i - d);            case CR:
134                out.write (CRLF, 0, 2);              out.write (b, d, i - d);
135                d = i + 1;              out.write (CRLF, 0, 2);
136                break;              d = i + 1;
137              case LF:              break;
138                if (last != CR)            case LF:
139                  {              if (last != CR)
140                    out.write (b, d, i - d);                {
141                    out.write (CRLF, 0, 2);                  out.write (b, d, i - d);
142                  }                  out.write (CRLF, 0, 2);
143                d = i + 1;                }
144                break;              d = i + 1;
145              }              break;
146            last = b[i];            }
147          }          last = b[i];
148        if (len - d > 0)        }
149          {      if (len - d > 0)
150            out.write (b, d, len - d);        {
151          }          out.write (b, d, len - d);
152      }        }
153      }
154      
155    /**    /**
156     * Writes the specified ASCII string to the underlying stream.     * Writes the specified ASCII string to the underlying stream.
157     * @exception IOException if an I/O error occurred     * @exception IOException if an I/O error occurred
158     */     */
159    public void write (String text) throws IOException    public void write(String text)
160      {      throws IOException
161        try    {
162          {      try
163            byte[] bytes = text.getBytes (US_ASCII);        {
164            write (bytes, 0, bytes.length);          byte[] bytes = text.getBytes(US_ASCII);
165          }          write(bytes, 0, bytes.length);
166        catch (UnsupportedEncodingException e)          }
167          {      catch (UnsupportedEncodingException e)
168            throw new IOException ("The US-ASCII encoding is not supported " +        {
169                                   "on this system");          throw new IOException("The US-ASCII encoding is not supported " +
170          }                                "on this system");
171      }        }
172      }
173    
174    /**    /**
175     * Writes a newline to the underlying stream.     * Writes a newline to the underlying stream.
176     * @exception IOException if an I/O error occurred     * @exception IOException if an I/O error occurred
177     */     */
178    public void writeln () throws IOException    public void writeln()
179      {      throws IOException
180        out.write (CRLF, 0, 2);    {
181      }      out.write(CRLF, 0, 2);
182      }
183    
184  }  }
185    

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26