/[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.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 37  import java.io.IOException; Line 37  import java.io.IOException;
37   * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>   * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
38   * @version $Revision$ $Date$   * @version $Revision$ $Date$
39   */   */
40  public class CRLFInputStream  public class CRLFInputStream extends FilterInputStream
   extends FilterInputStream  
41  {  {
42    
43    /**    /**
# Line 51  public class CRLFInputStream Line 50  public class CRLFInputStream
50     */     */
51    public static final int LF = 10;    public static final int LF = 10;
52    
53          /**          /**
54           * Buffer.           * Buffer.
55           */           */
56          protected int buf = -1;    protected int buf = -1;
57    
58          /**          /**
59           * Buffer at time of mark.           * Buffer at time of mark.
60           */           */
61          protected int markBuf = -1;    protected int markBuf = -1;
62            
63    /**    /**
64     * Constructs a CR/LF input stream connected to the specified input     * Constructs a CR/LF input stream connected to the specified input
65     * stream.     * stream.
66     */     */
67    public CRLFInputStream(InputStream in)    public CRLFInputStream(InputStream in)
68    {    {
69      super(in);      super(in);
70    }    }
# Line 75  public class CRLFInputStream Line 74  public class CRLFInputStream
74     * Returns -1 if the end of the stream has been reached.     * Returns -1 if the end of the stream has been reached.
75     * @exception IOException if an I/O error occurs     * @exception IOException if an I/O error occurs
76     */     */
77    public int read()    public int read() throws IOException
     throws IOException  
78    {    {
79                  int c;      int c;
80                  if (buf!=-1)      if (buf != -1)
81                  {      {
82                          c = buf;        c = buf;
83                          buf = -1;        buf = -1;
84                          return c;        return c;
85                  }      }
86                  else      else
87                  {      {
88                          c = super.read();        c = super.read();
89                          if (c==CR)        if (c == CR)
90                          {        {
91                                  buf = super.read();          buf = super.read();
92                                  if (buf==LF)          if (buf == LF)
93                                  {          {
94                                          c = buf;            c = buf;
95                                          buf = -1;            buf = -1;
96                                  }          }
97                          }        }
98                  }      }
99      return c;      return c;
100    }    }
101    
# Line 107  public class CRLFInputStream Line 105  public class CRLFInputStream
105     * Returns -1 if the end of the stream has been reached.     * Returns -1 if the end of the stream has been reached.
106     * @exception IOException if an I/O error occurs     * @exception IOException if an I/O error occurs
107     */     */
108    public int read(byte[] b)    public int read(byte[]b) throws IOException
     throws IOException  
109    {    {
110      return read(b, 0, b.length);      return read(b, 0, b.length);
111    }    }
# Line 119  public class CRLFInputStream Line 116  public class CRLFInputStream
116     * Returns -1 if the end of the stream has been reached.     * Returns -1 if the end of the stream has been reached.
117     * @exception IOException if an I/O error occurs     * @exception IOException if an I/O error occurs
118     */     */
119    public int read(byte[] b, int off, int len)    public int read(byte[]b, int off, int len) throws IOException
     throws IOException  
120    {    {
121                  int shift = 0;      int shift = 0;
122                  if (buf!=-1)      if (buf != -1)
123                  {      {
124                          // Push buf onto start of byte array        // Push buf onto start of byte array
125                          b[off] = (byte)buf;        b[off] = (byte) buf;
126                          off++;        off++;
127                          len--;        len--;
128                          buf = -1;        buf = -1;
129                          shift++;        shift++;
130                  }      }
131      int l = super.read(b, off, len);      int l = super.read(b, off, len);
132      l = removeCRLF(b, off-shift, l);      l = removeCRLF(b, off - shift, l);
133      return l;      return l;
134          }    }
135    
136          /**          /**
137           * Indicates whether this stream supports the mark and reset methods.           * Indicates whether this stream supports the mark and reset methods.
138           */           */
139          public boolean markSupported()    public boolean markSupported()
140          {    {
141                  return in.markSupported();      return in.markSupported();
142          }    }
143    
144          /**          /**
145           * Marks the current position in this stream.           * Marks the current position in this stream.
146           */           */
147          public void mark(int readlimit)    public void mark(int readlimit)
148          {    {
149                  in.mark(readlimit);      in.mark(readlimit);
150                  markBuf = buf;      markBuf = buf;
151          }    }
152    
153          /**          /**
154           * 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
155           * called.           * called.
156           */           */
157          public void reset()    public void reset() throws IOException
158                  throws IOException    {
159          {      in.reset();
160                  in.reset();      buf = markBuf;
161                  buf = markBuf;    }
162          }  
163              private int removeCRLF(byte[]b, int off, int len)
164    private int removeCRLF(byte[] b, int off, int len)    {
165    {      int end = off + len;
166                  int end = off+len;      for (int i = off; i < end; i++)
167                  for (int i=off; i<end; i++)      {
168                  {        if (b[i] == CR)
169                          if (b[i]==CR)        {
170                          {          if (i + 1 == end)
171                                  if (i+1==end)          {
172                                  {            // This is the last byte, impossible to determine whether CRLF
173                                          // This is the last byte, impossible to determine whether CRLF            buf = CR;
174                                          buf = CR;            len--;
175                                          len--;          }
176                                  }          else if (b[i + 1] == LF)
177                                  else if (b[i+1]==LF)          {
178                                  {            // Shift left
179                                          // Shift left            end--;
180                                          end--;            for (int j = i; j < end; j++)
181                                          for (int j=i; j<end; j++)              b[j] = b[j + 1];
182                                                  b[j] = b[j+1];            len--;
183                                          len--;            end = off + len;
184                                          end = off+len;          }
185                                  }        }
186                          }      }
                 }  
187      return len;      return len;
188    }    }
189    

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