Quadcap Embeddable Server

com/quadcap/io/DotStuffInputStream.java

Go to the documentation of this file.
00001 package com.quadcap.io; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.*; 00042 00043 import com.quadcap.util.Debug; 00044 import com.quadcap.util.Util; 00045 00046 /** 00047 * A filter input stream that performs dot-unstuffing. 00048 * 00049 * @author Stan Bailes 00050 */ 00051 00052 public class DotStuffInputStream extends InputStream { 00053 InputStream in; 00054 00055 static final byte CR = (byte)'\r'; 00056 static final byte LF = (byte)'\n'; 00057 static final byte DT = (byte)'.'; 00058 00059 static final int INIT = 0; 00060 static final int END = 1; 00061 static final int S1 = 2; 00062 static final int S2 = 3; 00063 static final int S3 = 4; 00064 static final int S4 = 5; 00065 00066 int state = INIT; 00067 int savec = -1; 00068 00069 public DotStuffInputStream(InputStream in) throws IOException { 00070 this.in = in; 00071 } 00072 00073 /** 00074 * Reads the next byte of data from this input stream. The value 00075 * byte is returned as an <code>int</code> in the range 00076 * <code>0</code> to <code>255</code>. If no byte is available 00077 * because the end of the stream has been reached, the value 00078 * <code>-1</code> is returned. This method blocks until input data 00079 * is available, the end of the stream is detected, or an exception 00080 * is thrown. 00081 * <p> 00082 * The <code>read</code> method of <code>DotStuffInputStream</code> 00083 * returns -1 when the characteristic 'single dot on a line by itself' 00084 * end signature, and removes all leading dots otherwise. 00085 * 00086 * @return the next byte of data, or <code>-1</code> if the end of the 00087 * stream is reached. 00088 * @exception IOException if an I/O error occurs. 00089 */ 00090 public int read() throws IOException { 00091 int c = savec; 00092 if (savec >= 0) { 00093 savec = -1; 00094 return c; 00095 } 00096 00097 while (state != END) { 00098 c = in.read(); 00099 switch (state) { 00100 case INIT: // at beginning of line 00101 switch (c) { 00102 case DT: state = S1; break; 00103 case CR: state = S3; return CR; 00104 case LF: return LF; // state still INIT 00105 default: state = S4; return c; 00106 } 00107 break; 00108 case S1: // seen leading DT 00109 switch (c) { 00110 case LF: state = END; break; 00111 case CR: state = S2; break; 00112 default: state = S4; return c; 00113 } 00114 break; 00115 case S2: // seen DT, CR. Better have LF next. 00116 switch (c) { 00117 case LF: state = END; break; 00118 case CR: state = S3; return CR; 00119 default: state = S4; return c; 00120 } 00121 break; 00122 case S3: // seen CR 00123 state = (c == LF) ? INIT : S4; 00124 return c; 00125 case S4: // just chugging along 00126 switch (c) { 00127 case CR: state = S3; return CR; 00128 case LF: state = INIT; return LF; 00129 default: return c; 00130 } 00131 } 00132 } 00133 return -1; 00134 } 00135 00136 /** 00137 * Reads up to <code>len</code> bytes of data from this input stream 00138 * into an array of bytes. This method blocks until some input is 00139 * available. 00140 * 00141 * @param b the buffer into which the data is read. 00142 * @param off the start offset of the data. 00143 * @param len the maximum number of bytes read. 00144 * @return the total number of bytes read into the buffer, or 00145 * <code>-1</code> if there is no more data because the end of 00146 * the stream has been reached. 00147 * @exception IOException if an I/O error occurs. 00148 */ 00149 public int read(byte b[], int off, int len) throws IOException { 00150 int cnt = 0; 00151 int c; 00152 while (cnt < len && (c = read()) >= 0) { 00153 b[off + cnt++] = (byte)c; 00154 } 00155 return cnt; 00156 } 00157 00158 /** 00159 * Skips over and discards <code>n</code> bytes of data from the 00160 * input stream. The <code>skip</code> method may, for a variety of 00161 * reasons, end up skipping over some smaller number of bytes, 00162 * possibly <code>0</code>. The actual number of bytes skipped is 00163 * returned. 00164 * 00165 * @param n the number of bytes to be skipped. 00166 * @return the actual number of bytes skipped. 00167 * @exception IOException if an I/O error occurs. 00168 */ 00169 public long skip(long n) throws IOException { 00170 int cnt = 0; 00171 while (cnt < n && read() >= 0) cnt++; 00172 return cnt; 00173 } 00174 00175 00176 }