1 package com.quadcap.io; 2 3 40 41 import java.io.*; 42 43 import com.quadcap.util.Debug; 44 import com.quadcap.util.Util; 45 46 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 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: switch (c) { 102 case DT: state = S1; break; 103 case CR: state = S3; return CR; 104 case LF: return LF; default: state = S4; return c; 106 } 107 break; 108 case S1: 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: 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: state = (c == LF) ? INIT : S4; 124 return c; 125 case S4: 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 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 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 |