KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > io > DotStuffInputStream


1 package com.quadcap.io;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.*;
42
43 import com.quadcap.util.Debug;
44 import com.quadcap.util.Util;
45
46 /**
47  * A filter input stream that performs dot-unstuffing.
48  *
49  * @author Stan Bailes
50  */

51
52 public class DotStuffInputStream extends InputStream {
53     InputStream in;
54     
55     static final byte CR = (byte)'\r';
56     static final byte LF = (byte)'\n';
57     static final byte DT = (byte)'.';
58
59     static final int INIT = 0;
60     static final int END = 1;
61     static final int S1 = 2;
62     static final int S2 = 3;
63     static final int S3 = 4;
64     static final int S4 = 5;
65
66     int state = INIT;
67     int savec = -1;
68
69     public DotStuffInputStream(InputStream in) throws IOException {
70     this.in = in;
71     }
72
73     /**
74      * Reads the next byte of data from this input stream. The value
75      * byte is returned as an <code>int</code> in the range
76      * <code>0</code> to <code>255</code>. If no byte is available
77      * because the end of the stream has been reached, the value
78      * <code>-1</code> is returned. This method blocks until input data
79      * is available, the end of the stream is detected, or an exception
80      * is thrown.
81      * <p>
82      * The <code>read</code> method of <code>DotStuffInputStream</code>
83      * returns -1 when the characteristic 'single dot on a line by itself'
84      * end signature, and removes all leading dots otherwise.
85      *
86      * @return the next byte of data, or <code>-1</code> if the end of the
87      * stream is reached.
88      * @exception IOException if an I/O error occurs.
89      */

90     public int read() throws IOException {
91     int c = savec;
92         if (savec >= 0) {
93             savec = -1;
94             return c;
95         }
96
97         while (state != END) {
98             c = in.read();
99             switch (state) {
100             case INIT: // at beginning of line
101
switch (c) {
102                 case DT: state = S1; break;
103                 case CR: state = S3; return CR;
104                 case LF: return LF; // state still INIT
105
default: state = S4; return c;
106                 }
107                 break;
108             case S1: // seen leading DT
109
switch (c) {
110                 case LF: state = END; break;
111                 case CR: state = S2; break;
112                 default: state = S4; return c;
113                 }
114                 break;
115             case S2: // seen DT, CR. Better have LF next.
116
switch (c) {
117                 case LF: state = END; break;
118                 case CR: state = S3; return CR;
119                 default: state = S4; return c;
120                 }
121                 break;
122             case S3: // seen CR
123
state = (c == LF) ? INIT : S4;
124                 return c;
125             case S4: // just chugging along
126
switch (c) {
127                 case CR: state = S3; return CR;
128                 case LF: state = INIT; return LF;
129                 default: return c;
130                 }
131             }
132         }
133         return -1;
134     }
135
136     /**
137      * Reads up to <code>len</code> bytes of data from this input stream
138      * into an array of bytes. This method blocks until some input is
139      * available.
140      *
141      * @param b the buffer into which the data is read.
142      * @param off the start offset of the data.
143      * @param len the maximum number of bytes read.
144      * @return the total number of bytes read into the buffer, or
145      * <code>-1</code> if there is no more data because the end of
146      * the stream has been reached.
147      * @exception IOException if an I/O error occurs.
148      */

149     public int read(byte b[], int off, int len) throws IOException {
150     int cnt = 0;
151     int c;
152     while (cnt < len && (c = read()) >= 0) {
153         b[off + cnt++] = (byte)c;
154     }
155     return cnt;
156     }
157
158     /**
159      * Skips over and discards <code>n</code> bytes of data from the
160      * input stream. The <code>skip</code> method may, for a variety of
161      * reasons, end up skipping over some smaller number of bytes,
162      * possibly <code>0</code>. The actual number of bytes skipped is
163      * returned.
164      *
165      * @param n the number of bytes to be skipped.
166      * @return the actual number of bytes skipped.
167      * @exception IOException if an I/O error occurs.
168      */

169     public long skip(long n) throws IOException {
170     int cnt = 0;
171     while (cnt < n && read() >= 0) cnt++;
172     return cnt;
173     }
174
175     
176 }
177
Popular Tags