/[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.1 by dog, Sun Oct 19 08:51:37 2003 UTC revision 1.2 by dog, Sun Oct 19 16:16:50 2003 UTC
# Line 38  import java.io.UnsupportedEncodingExcept Line 38  import java.io.UnsupportedEncodingExcept
38   * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>   * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
39   * @author $Revision$ $Date$   * @author $Revision$ $Date$
40   */   */
41  public class CRLFOutputStream  public class CRLFOutputStream extends FilterOutputStream
   extends FilterOutputStream  
42  {  {
43    
44          static final String US_ASCII = "US-ASCII";    static final String US_ASCII = "US-ASCII";
45    
46    /**    /**
47     * The CR octet.     * The CR octet.
# Line 77  public class CRLFOutputStream Line 76  public class CRLFOutputStream
76     * Writes a character to the underlying stream.     * Writes a character to the underlying stream.
77     * @exception IOException if an I/O error occurred     * @exception IOException if an I/O error occurred
78     */     */
79    public void write(int ch)    public void write(int ch) throws IOException
     throws IOException  
80    {    {
81      if (ch==CR)      if (ch == CR)
82        out.write(CRLF);        out.write(CRLF);
83      else if (ch==LF)      else if (ch == LF)
84      {      {
85        if (last!=CR)        if (last != CR)
86          out.write(CRLF);          out.write(CRLF);
87      }      }
88      else      else
89        out.write(ch);          out.write(ch);
90      last = ch;      last = ch;
91    }    }
92    
# Line 96  public class CRLFOutputStream Line 94  public class CRLFOutputStream
94     * Writes a byte array to the underlying stream.     * Writes a byte array to the underlying stream.
95     * @exception IOException if an I/O error occurred     * @exception IOException if an I/O error occurred
96     */     */
97    public void write(byte b[])    public void write(byte b[]) throws IOException
     throws IOException  
98    {    {
99      write(b, 0, b.length);      write(b, 0, b.length);
100    }    }
# Line 106  public class CRLFOutputStream Line 103  public class CRLFOutputStream
103     * Writes a portion of a byte array to the underlying stream.     * Writes a portion of a byte array to the underlying stream.
104     * @exception IOException if an I/O error occurred     * @exception IOException if an I/O error occurred
105     */     */
106    public void write(byte b[], int off, int len)    public void write(byte b[], int off, int len) throws IOException
     throws IOException  
107    {    {
108      int d = off;      int d = off;
109      len += off;        len += off;
110      for (int i=off; i<len; i++)      for (int i = off; i < len; i++)
111      {      {
112        switch (b[i])        switch (b[i])
113        {        {
114          case CR:        case CR:
115            out.write(b, d, i-d);          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);            out.write(CRLF, 0, 2);
123            d = i+1;          }
124            break;          d = i + 1;
125          case LF:          break;
           if (last!=CR)  
           {  
             out.write(b, d, i-d);  
             out.write(CRLF, 0, 2);  
           }  
           d = i+1;  
           break;  
126        }        }
127        last = b[i];        last = b[i];
128      }      }
129      if (len-d>0)      if (len - d > 0)
130        out.write(b, d, len-d);        out.write(b, d, len - d);
131    }    }
132    
133    /**    /**
134     * Writes the specified ASCII string to the underlying stream.     * Writes the specified ASCII string to the underlying stream.
135     * @exception IOException if an I/O error occurred     * @exception IOException if an I/O error occurred
136     */     */
137    public void write(String text)    public void write(String text) throws IOException
     throws IOException  
138    {    {
139                  try      try
140                  {      {
141                          byte[] bytes = text.getBytes(US_ASCII);        byte[]bytes = text.getBytes(US_ASCII);
142                          write(bytes, 0, bytes.length);        write(bytes, 0, bytes.length);
143                  }      }
144                  catch (UnsupportedEncodingException e)      catch(UnsupportedEncodingException e)
145                  {      {
146                          throw new IOException("Not ASCII: "+text);        throw new IOException("Not ASCII: " + text);
147                  }      }
148    }    }
149    
150    /**    /**
151     * Writes a newline to the underlying stream.     * Writes a newline to the underlying stream.
152     * @exception IOException if an I/O error occurred     * @exception IOException if an I/O error occurred
153     */     */
154    public void writeln()    public void writeln() throws IOException
     throws IOException  
155    {    {
156      out.write(CRLF);      out.write(CRLF);
157    }    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

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