2010-07-12 12:35:46 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2010 Mickael Guessant
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
package davmail.exchange;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.PushbackInputStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace double dot lines with single dot in input stream.
|
|
|
|
* A line with a single dot means end of stream
|
|
|
|
*/
|
|
|
|
public class DoubleDotInputStream extends PushbackInputStream {
|
2010-07-19 05:22:42 -04:00
|
|
|
final int[] buffer = new int[4];
|
2010-07-12 12:35:46 -04:00
|
|
|
int index = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public DoubleDotInputStream(InputStream in) {
|
2010-07-13 13:03:29 -04:00
|
|
|
super(in, 4);
|
2010-07-12 12:35:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push current byte to buffer and read next byte.
|
|
|
|
*
|
|
|
|
* @return next byte
|
|
|
|
* @throws IOException on error
|
|
|
|
*/
|
|
|
|
protected int readNextByte() throws IOException {
|
|
|
|
int b = super.read();
|
|
|
|
buffer[++index] = b;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int read() throws IOException {
|
|
|
|
int b = super.read();
|
|
|
|
if (b == '\r') {
|
|
|
|
// \r\n
|
|
|
|
if (readNextByte() == '\n') {
|
|
|
|
// \r\n.
|
|
|
|
if (readNextByte() == '.') {
|
|
|
|
// \r\n.\r
|
|
|
|
if (readNextByte() == '\r') {
|
|
|
|
// \r\n.\r\n
|
|
|
|
if (readNextByte() == '\n') {
|
|
|
|
// end of stream
|
|
|
|
index = -1;
|
|
|
|
b = -1;
|
|
|
|
}
|
|
|
|
// \r\n..
|
|
|
|
} else if (buffer[index] == '.') {
|
|
|
|
// \r\n..\r
|
|
|
|
if ((readNextByte()) == '\r') {
|
|
|
|
// replace double dot
|
|
|
|
buffer[--index] = '\r';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// push back characters
|
|
|
|
if (index >= 0) {
|
2011-06-08 15:58:29 -04:00
|
|
|
while (index >= 0) {
|
2010-07-12 12:35:46 -04:00
|
|
|
unread(buffer[index--]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|