1 36 package org.columba.ristretto.coder; 37 38 import java.io.FilterInputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 42 47 public class CanonizeFilterInputStream extends FilterInputStream { 48 49 private static final int NOOP = 0; 50 private static final int CRFOUND = 1; 51 private static final int PRINTLF = 2; 52 private static final int PRINTBUFFER = 3; 53 54 55 private int mode = 0; 56 private int buffer; 57 58 63 public CanonizeFilterInputStream(InputStream in) { 64 super(in); 65 } 66 67 70 public int read() throws IOException { 71 74 if( mode == PRINTLF ) { 76 mode = NOOP; 77 return '\n'; 78 } 79 80 if( mode == PRINTBUFFER ) { 82 mode = NOOP; 83 return buffer; 84 } 85 86 int read = in.read(); 89 90 if( read == '\r' ) { 92 mode = CRFOUND; 93 return read; 94 } 95 96 if( read != '\n' && mode == CRFOUND ) { 100 mode = PRINTBUFFER; 101 buffer = read; 102 return '\n'; 103 } 104 105 if( read == '\n' && mode != CRFOUND ) { 108 mode = PRINTLF; 109 return '\r'; 110 } 111 112 mode = NOOP; 114 return read; 115 } 116 117 118 121 public int read(byte[] b, int off, int len) throws IOException { 122 int read; 123 for( int i=0; i<len ; i++) { 124 read = in.read(); 125 if( read == -1) { 126 if( i == 0 ) return -1; 127 else return i; 128 } 129 b[off+i] = (byte) read; 130 } 131 132 return len; 133 } 134 135 } 136 | Popular Tags |