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

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

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

revision 1.4 by dog, Thu Oct 21 15:21:55 2004 UTC revision 1.5 by dog, Thu Nov 25 22:15:05 2004 UTC
# Line 1  Line 1 
1  /*  /*
2   * $Id$   * CRLFInputStream.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 46  import java.io.IOException; Line 46  import java.io.IOException;
46   * An input stream that filters out CR/LF pairs into LFs.   * An input stream that filters out CR/LF pairs into LFs.
47   *   *
48   * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>   * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
  * @version $Revision$ $Date$  
49   */   */
50  public class CRLFInputStream extends FilterInputStream  public class CRLFInputStream
51      extends FilterInputStream
52  {  {
53    
54    /**    /**
# Line 75  public class CRLFInputStream extends Fil Line 75  public class CRLFInputStream extends Fil
75     * Constructs a CR/LF input stream connected to the specified input     * Constructs a CR/LF input stream connected to the specified input
76     * stream.     * stream.
77     */     */
78    public CRLFInputStream (InputStream in)    public CRLFInputStream(InputStream in)
79      {    {
80        super (in);      super(in);
81      }    }
82    
83    /**    /**
84     * Reads the next byte of data from this input stream.     * Reads the next byte of data from this input stream.
85     * Returns -1 if the end of the stream has been reached.     * Returns -1 if the end of the stream has been reached.
86     * @exception IOException if an I/O error occurs     * @exception IOException if an I/O error occurs
87     */     */
88    public int read () throws IOException    public int read()
89      {      throws IOException
90        int c;    {
91        if (buf != -1)      int c;
92          {      if (buf != -1)
93            c = buf;        {
94            buf = -1;          c = buf;
95            return c;          buf = -1;
96          }          return c;
97        else        }
98          {      else
99            c = super.read ();        {
100            if (c == CR)          c = super.read();
101              {          if (c == CR)
102                buf = super.read ();            {
103                if (buf == LF)              buf = super.read();
104                  {              if (buf == LF)
105                    c = buf;                {
106                    buf = -1;                  c = buf;
107                  }                  buf = -1;
108              }                }
109          }            }
110        return c;        }
111      }      return c;
112      }
113      
114    /**    /**
115     * Reads up to b.length bytes of data from this input stream into     * Reads up to b.length bytes of data from this input stream into
116     * an array of bytes.     * an array of bytes.
117     * Returns -1 if the end of the stream has been reached.     * Returns -1 if the end of the stream has been reached.
118     * @exception IOException if an I/O error occurs     * @exception IOException if an I/O error occurs
119     */     */
120    public int read (byte[] b) throws IOException    public int read(byte[] b)
121      {      throws IOException
122        return read (b, 0, b.length);    {
123      }      return read(b, 0, b.length);
124      }
125    
126    /**    /**
127     * Reads up to len bytes of data from this input stream into an     * Reads up to len bytes of data from this input stream into an
# Line 127  public class CRLFInputStream extends Fil Line 129  public class CRLFInputStream extends Fil
129     * Returns -1 if the end of the stream has been reached.     * Returns -1 if the end of the stream has been reached.
130     * @exception IOException if an I/O error occurs     * @exception IOException if an I/O error occurs
131     */     */
132    public int read (byte[] b, int off, int len) throws IOException    public int read(byte[] b, int off, int len)
133      {      throws IOException
134        int shift = 0;    {
135        if (buf != -1)      int shift = 0;
136          {      if (buf != -1)
137            // Push buf onto start of byte array        {
138            b[off] = (byte) buf;          // Push buf onto start of byte array
139            off++;          b[off] = (byte) buf;
140            len--;          off++;
141            buf = -1;          len--;
142            shift++;          buf = -1;
143          }          shift++;
144        int l = super.read (b, off, len);        }
145        l = removeCRLF (b, off - shift, l);      int l = super.read(b, off, len);
146        return l;      l = removeCRLF(b, off - shift, l);
147      }      return l;
148      }
149    
150    /**    /**
151     * Indicates whether this stream supports the mark and reset methods.     * Indicates whether this stream supports the mark and reset methods.
152     */     */
153    public boolean markSupported ()    public boolean markSupported()
154      {    {
155        return in.markSupported ();      return in.markSupported();
156      }    }
157    
158    /**    /**
159     * Marks the current position in this stream.     * Marks the current position in this stream.
160     */     */
161    public void mark (int readlimit)    public void mark(int readlimit)
162      {    {
163        in.mark (readlimit);      in.mark(readlimit);
164        markBuf = buf;      markBuf = buf;
165      }    }
166    
167    /**    /**
168     * Repositions this stream to the position at the time the mark method was     * Repositions this stream to the position at the time the mark method was
169     * called.     * called.
170     */     */
171    public void reset () throws IOException    public void reset()
172      {      throws IOException
173        in.reset ();    {
174        buf = markBuf;      in.reset();
175      }      buf = markBuf;
176      }
177    private int removeCRLF (byte[] b, int off, int len)  
178      {    private int removeCRLF(byte[] b, int off, int len)
179        int end = off + len;    {
180        for (int i = off; i < end; i++)      int end = off + len;
181          {      for (int i = off; i < end; i++)
182            if (b[i] == CR)        {
183              {          if (b[i] == CR)
184                if (i + 1 == end)            {
185                  {              if (i + 1 == end)
186                    // This is the last byte, impossible to determine whether CRLF                {
187                    buf = CR;                  // This is the last byte, impossible to determine whether CRLF
188                    len--;                  buf = CR;
189                  }                  len--;
190                else if (b[i + 1] == LF)                }
191                  {              else if (b[i + 1] == LF)
192                    // Shift left                {
193                    end--;                  // Shift left
194                    for (int j = i; j < end; j++)                  end--;
195                      {                  for (int j = i; j < end; j++)
196                        b[j] = b[j + 1];                    {
197                      }                      b[j] = b[j + 1];
198                      }
199                    len--;                    len--;
200                    end = off + len;                    end = off + len;
201                  }                }
202              }            }
203          }        }
204        return len;      return len;
205      }    }
206    
207  }  }
208    

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

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