001: package com.quadcap.io;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.*;
042:
043: import com.quadcap.util.Debug;
044: import com.quadcap.util.Util;
045:
046: /**
047: * A filter output stream that performs dot-stuffing.
048: *
049: * @author Stan Bailes
050: */
051:
052: public class DotStuffOutputStream extends OutputStream {
053: OutputStream out;
054:
055: static final int INIT = 0;
056: static final int SEENCR = 1;
057: static final int SEENLF = 2;
058:
059: static final byte CR = (byte) '\r';
060: static final byte LF = (byte) '\n';
061:
062: static final byte[] endTag = { CR, LF, (byte) '.', CR, LF };
063:
064: int state = SEENLF;
065:
066: public DotStuffOutputStream(OutputStream os) {
067: this .out = os;
068: }
069:
070: public void write(int b) throws IOException {
071: switch (state) {
072: case INIT:
073: if (b == CR)
074: state = SEENCR;
075: break;
076: case SEENCR:
077: if (b == LF)
078: state = SEENLF;
079: else if (b != CR)
080: state = INIT;
081: break;
082: case SEENLF:
083: if (b == '.') {
084: out.write('.');
085: state = INIT;
086: } else if (b == CR) {
087: state = SEENCR;
088: } else {
089: state = INIT;
090: }
091: break;
092: }
093: out.write(b);
094: }
095:
096: public void write(byte[] buf, int offset, int len)
097: throws IOException {
098: for (int i = 0; i < len; i++) {
099: this .write(buf[i]);
100: }
101: }
102:
103: public void writeFinal() throws IOException {
104: int start = 0;
105: int cnt = endTag.length - start;
106: out.write(endTag, start, cnt);
107: out.flush();
108: }
109: }
|