001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.gui.composer.util;
019:
020: import java.io.FilterInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: public class QuoteFilterInputStream extends FilterInputStream {
025: private static final int QUOTE = 0;
026: private static final int BODY = 1;
027: private static final int BODYSTART = 2;
028: private byte[] quotePrefix;
029: private int mode;
030: private int quotePos;
031: private int preRead;
032:
033: /**
034: * @param arg0
035: */
036: public QuoteFilterInputStream(InputStream arg0, String prefix)
037: throws IOException {
038: super (arg0);
039:
040: quotePrefix = prefix.getBytes();
041:
042: preRead = arg0.read();
043:
044: if (preRead == -1) {
045: mode = BODYSTART;
046: } else {
047: // First print a quote
048: mode = QUOTE;
049: quotePos = 0;
050: }
051: }
052:
053: /**
054: * @param arg0
055: */
056: public QuoteFilterInputStream(InputStream arg0) throws IOException {
057: this (arg0, "> ");
058: }
059:
060: /*
061: * (non-Javadoc)
062: *
063: * @see java.io.InputStream#read()
064: */
065: public int read() throws IOException {
066: int result = -1;
067:
068: switch (mode) {
069: case QUOTE: {
070: if (quotePos < quotePrefix.length) {
071: result = (int) quotePrefix[quotePos++];
072: } else {
073: // reset
074: mode = BODY;
075: quotePos = 0;
076: result = preRead;
077: }
078:
079: break;
080: }
081:
082: case BODYSTART: {
083: mode = BODY;
084: result = preRead;
085:
086: break;
087: }
088:
089: case BODY: {
090: result = in.read();
091:
092: break;
093: }
094: }
095:
096: // Do we have to insert a quoteprefix?
097: if (result == '\n') {
098: preRead = in.read();
099:
100: if (preRead == -1) {
101: mode = BODYSTART;
102: } else {
103: mode = QUOTE;
104: }
105: }
106:
107: return result;
108: }
109:
110: /*
111: * (non-Javadoc)
112: *
113: * @see java.io.InputStream#read(byte[], int, int)
114: */
115: public int read(byte[] arg0, int arg1, int arg2) throws IOException {
116: int next;
117:
118: for (int i = 0; i < arg2; i++) {
119: next = read();
120:
121: if (next == -1) {
122: if (i == 0) {
123: return -1;
124: } else {
125: return i;
126: }
127: }
128:
129: arg0[arg1 + i] = (byte) next;
130: }
131:
132: return arg2;
133: }
134: }
|