📄 messageinputstream.java
字号:
/* * MessageInputStream.java * Copyright (C) 1999 dog <dog@dog.net.uk> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * You may retrieve the latest version of this library from * http://www.dog.net.uk/knife/ */package dog.mail.util;import java.io.*;/** * A utility class for feeding message contents to messages. * * @author dog@dog.net.uk * @version 1.0 */public class MessageInputStream extends FilterInputStream { /** * The stream termination octet. */ public static final int END = 46; /** * The line termination octet. */ public static final int LF = 10; boolean done = false, debug = false; int last = -1; // register for the last octet read int bufsize; // the buffer size for the pushback (CRLF) stream /** * Constructs a message input stream connected to the specified pushback input stream. */ public MessageInputStream(PushbackInputStream in) { super(in); } /** * Constructs a message input stream connected to the specified pushback input stream. */ public MessageInputStream(PushbackInputStream in, boolean debug) { super(in); this.debug = debug; } /** * Reads the next byte of data from this message input stream. * Returns -1 if the end of the message stream has been reached. * @exception IOException if an I/O error occurs */ public int read() throws IOException { if (done) return -1; int ch = in.read(); if (ch==END && last==LF) { int ch2 = in.read(); // look ahead for LF if (ch2==LF) { done = true; return -1; // swallow the END and LF } else ((PushbackInputStream)in).unread(ch2); } last = ch; return ch; } /** * Reads up to b.length bytes of data from this input stream into * an array of bytes. * Returns -1 if the end of the stream has been reached. * @exception IOException if an I/O error occurs */ public int read(byte[] b) throws IOException { return read(b, 0, b.length); } /** * Reads up to len bytes of data from this input stream into an * array of bytes, starting at the specified offset. * Returns -1 if the end of the stream has been reached. * @exception IOException if an I/O error occurs */ public int read(byte[] b, int off, int len) throws IOException { if (done) return -1; int l = in.read(b, off, len); if (debug) System.err.println("DEBUG: stream: read "+len+" bytes"); int i = indexOfEnd(b, off, l); if (i>=0) { ((PushbackInputStream)in).unread(b, i+off+2, (l-(i+2))); done = true; return i; } else if (b[l-1]==LF) { ((PushbackInputStream)in).unread(b, l-1, 1); Thread.yield(); return l - 1; } else if ((l >= 2) && (b[l-2]==LF && b[l-1]==END)) { ((PushbackInputStream)in).unread(b, l-2, 2); Thread.yield(); return l - 2; } return l; } // Discover the index of END in a byte array. int indexOfEnd(byte[] b, int off, int len) { for (int i=off+2; i<(off+len); i++) if (b[i]==LF && b[i-1]==END && b[i-2]==LF) return i-off-1; return -1; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -