1 package org.columba.mail.gui.composer.util; 19 20 import java.io.FilterInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 24 25 public class QuoteFilterInputStream extends FilterInputStream { 26 private static final int QUOTE = 0; 27 private static final int BODY = 1; 28 private static final int BODYSTART = 2; 29 private byte[] quotePrefix; 30 private int mode; 31 private int quotePos; 32 private int preRead; 33 34 37 public QuoteFilterInputStream(InputStream arg0, String prefix) 38 throws IOException { 39 super(arg0); 40 41 quotePrefix = prefix.getBytes(); 42 43 preRead = arg0.read(); 44 45 if (preRead == -1) { 46 mode = BODYSTART; 47 } else { 48 mode = QUOTE; 50 quotePos = 0; 51 } 52 } 53 54 57 public QuoteFilterInputStream(InputStream arg0) throws IOException { 58 this(arg0, "> "); 59 } 60 61 66 public int read() throws IOException { 67 int result = -1; 68 69 switch (mode) { 70 case QUOTE: { 71 if (quotePos < quotePrefix.length) { 72 result = (int) quotePrefix[quotePos++]; 73 } else { 74 mode = BODY; 76 quotePos = 0; 77 result = preRead; 78 } 79 80 break; 81 } 82 83 case BODYSTART: { 84 mode = BODY; 85 result = preRead; 86 87 break; 88 } 89 90 case BODY: { 91 result = in.read(); 92 93 break; 94 } 95 } 96 97 if (result == '\n') { 99 preRead = in.read(); 100 101 if (preRead == -1) { 102 mode = BODYSTART; 103 } else { 104 mode = QUOTE; 105 } 106 } 107 108 return result; 109 } 110 111 116 public int read(byte[] arg0, int arg1, int arg2) throws IOException { 117 int next; 118 119 for (int i = 0; i < arg2; i++) { 120 next = read(); 121 122 if (next == -1) { 123 if (i == 0) { 124 return -1; 125 } else { 126 return i; 127 } 128 } 129 130 arg0[arg1 + i] = (byte) next; 131 } 132 133 return arg2; 134 } 135 } 136 | Popular Tags |