KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > composer > util > QuoteFilterInputStream


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.gui.composer.util;
19
20 import java.io.FilterInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24
25 public class QuoteFilterInputStream extends FilterInputStream JavaDoc {
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     /**
35      * @param arg0
36      */

37     public QuoteFilterInputStream(InputStream JavaDoc arg0, String JavaDoc prefix)
38         throws IOException JavaDoc {
39         super(arg0);
40
41         quotePrefix = prefix.getBytes();
42
43         preRead = arg0.read();
44
45         if (preRead == -1) {
46             mode = BODYSTART;
47         } else {
48             // First print a quote
49
mode = QUOTE;
50             quotePos = 0;
51         }
52     }
53
54     /**
55      * @param arg0
56      */

57     public QuoteFilterInputStream(InputStream JavaDoc arg0) throws IOException JavaDoc {
58         this(arg0, "> ");
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see java.io.InputStream#read()
65      */

66     public int read() throws IOException JavaDoc {
67         int result = -1;
68
69         switch (mode) {
70         case QUOTE: {
71             if (quotePos < quotePrefix.length) {
72                 result = (int) quotePrefix[quotePos++];
73             } else {
74                 // reset
75
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         // Do we have to insert a quoteprefix?
98
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     /*
112      * (non-Javadoc)
113      *
114      * @see java.io.InputStream#read(byte[], int, int)
115      */

116     public int read(byte[] arg0, int arg1, int arg2) throws IOException JavaDoc {
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