⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mimemultipart.java

📁 java Email you can use it to send email to others
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		 * Strip trailing whitespace.  Can't use trim method		 * because it's too aggressive.  Some bogus MIME		 * messages will include control characters in the		 * boundary string.		 */		int i;		for (i = line.length() - 1; i >= 0; i--) {		    char c = line.charAt(i);		    if (!(c == ' ' || c == '\t'))			break;		}		line = line.substring(0, i + 1);		if (boundary != null) {		    if (line.equals(boundary))			break;		} else {		    /*		     * Boundary hasn't been defined, does this line		     * look like a boundary?  If so, assume it is		     * the boundary and save it.		     */		    if (line.startsWith("--")) {			boundary = line;			break;		    }		}		// save the preamble after skipping blank lines		if (line.length() > 0) {		    // if we haven't figured out what the line seprator		    // is, do it now		    if (lineSeparator == null) {			try {			    lineSeparator =				System.getProperty("line.separator", "\n");			} catch (SecurityException ex) {			    lineSeparator = "\n";			}		    }		    // accumulate the preamble		    if (preamblesb == null)			preamblesb = new StringBuffer(line.length() + 2);		    preamblesb.append(line).append(lineSeparator);		}	    }	    if (line == null)		throw new MessagingException("Missing start boundary");	    if (preamblesb != null)		preamble = preamblesb.toString();	    // save individual boundary bytes for comparison later	    byte[] bndbytes = ASCIIUtility.getBytes(boundary);	    int bl = bndbytes.length;	    /*	     * Compile Boyer-Moore parsing tables.	     */	    // initialize Bad Character Shift table	    int[] bcs = new int[256];	    for (int i = 0; i < bl; i++)		bcs[bndbytes[i]] = i + 1;	    // initialize Good Suffix Shift table	    int[] gss = new int[bl];	NEXT:	    for (int i = bl; i > 0; i--) {		int j;	// the beginning index of the suffix being considered		for (j = bl - 1; j >= i; j--) {		    // Testing for good suffix		    if (bndbytes[j] == bndbytes[j - i]) {			// bndbytes[j..len] is a good suffix			gss[j - 1] = i;		    } else {		       // No match. The array has already been		       // filled up with correct values before.		       continue NEXT;		    }		}		while (j > 0)		    gss[--j] = i;	    }	    gss[bl - 1] = 1; 	    /*	     * Read and process body parts until we see the	     * terminating boundary line (or EOF).	     */	    boolean done = false;	getparts:	    while (!done) {		InternetHeaders headers = null;		if (sin != null) {		    start = sin.getPosition();		    // skip headers		    while ((line = lin.readLine()) != null && line.length() > 0)			;		    if (line == null) {			if (!ignoreMissingEndBoundary)			    throw new MessagingException(					"missing multipart end boundary");			// assume there's just a missing end boundary			complete = false;			break getparts;		    }		} else {		    // collect the headers for this body part		    headers = createInternetHeaders(in);		}		if (!in.markSupported())		    throw new MessagingException("Stream doesn't support mark");		ByteArrayOutputStream buf = null;		// if we don't have a shared input stream, we copy the data		if (sin == null)		    buf = new ByteArrayOutputStream();		else		    end = sin.getPosition();		int b;		/*		 * These buffers contain the bytes we're checking		 * for a match.  inbuf is the current buffer and		 * previnbuf is the previous buffer.  We need the		 * previous buffer to check that we're preceeded		 * by an EOL.		 */		// XXX - a smarter algorithm would use a sliding window		//	 over a larger buffer		byte[] inbuf = new byte[bl];		byte[] previnbuf = new byte[bl];		int inSize = 0;		// number of valid bytes in inbuf		int prevSize = 0;	// number of valid bytes in previnbuf		int eolLen;		boolean first = true;		/*		 * Read and save the content bytes in buf.		 */		for (;;) {		    in.mark(bl + 4 + 1000); // bnd + "--\r\n" + lots of LWSP		    eolLen = 0;		    inSize = readFully(in, inbuf, 0, bl);		    if (inSize < bl) {			// hit EOF			if (!ignoreMissingEndBoundary)			    throw new MessagingException(					"missing multipart end boundary");			if (sin != null)			    end = sin.getPosition();			complete = false;			done = true;			break;		    }		    // check whether inbuf contains a boundary string		    int i;		    for (i = bl - 1; i >= 0; i--) {			if (inbuf[i] != bndbytes[i])			    break;		    }		    if (i < 0) {	// matched all bytes			eolLen = 0;			if (!first) {			    // working backwards, find out if we were preceeded			    // by an EOL, and if so find its length			    b = previnbuf[prevSize - 1];			    if (b == '\r' || b == '\n') {				eolLen = 1;				if (b == '\n' && prevSize >= 2) {				    b = previnbuf[prevSize - 2];				    if (b == '\r')					eolLen = 2;				}			    }			}			if (first || eolLen > 0) {	// yes, preceed by EOL			    if (sin != null) {				// update "end", in case this really is				// a valid boundary				end = sin.getPosition() - bl - eolLen;			    }			    // matched the boundary, check for last boundary			    int b2 = in.read();			    if (b2 == '-') {				if (in.read() == '-') {				    complete = true;				    done = true;				    break;	// ignore trailing text				}			    }			    // skip linear whitespace			    while (b2 == ' ' || b2 == '\t')				b2 = in.read();			    // check for end of line			    if (b2 == '\n')				break;	// got it!  break out of the loop			    if (b2 == '\r') {				in.mark(1);				if (in.read() != '\n')				    in.reset();				break;	// got it!  break out of the loop			    }			}			i = 0;		    }		    /*		     * Get here if boundary didn't match,		     * wasn't preceeded by EOL, or wasn't		     * followed by whitespace or EOL.		     */		    // compute how many bytes we can skip		    int skip = Math.max(i + 1 - bcs[inbuf[i] & 0x7f], gss[i]);		    // want to keep at least two characters		    if (skip < 2) {			// only skipping one byte, save one byte			// from previous buffer as well			// first, write out bytes we're done with			if (sin == null && prevSize > 1)			    buf.write(previnbuf, 0, prevSize - 1);			in.reset();			skipFully(in, 1);			if (prevSize >= 1) {	// is there a byte to save?			    // yes, save one from previous and one from current			    previnbuf[0] = previnbuf[prevSize - 1];			    previnbuf[1] = inbuf[0];			    prevSize = 2;			} else {			    // no previous bytes to save, can only save current			    previnbuf[0] = inbuf[0];			    prevSize = 1;			}		    } else {			// first, write out data from previous buffer before			// we dump it			if (prevSize > 0 && sin == null)			    buf.write(previnbuf, 0, prevSize);			// all the bytes we're skipping are saved in previnbuf			prevSize = skip;			in.reset();			skipFully(in, prevSize);			// swap buffers			byte[] tmp = inbuf;			inbuf = previnbuf;			previnbuf = tmp;		    }		    first = false;		}		/*		 * Create a MimeBody element to represent this body part.		 */		MimeBodyPart part;		if (sin != null) {		    part = createMimeBodyPart(sin.newStream(start, end));		} else {		    // write out data from previous buffer, not including EOL		    if (prevSize - eolLen > 0)			buf.write(previnbuf, 0, prevSize - eolLen);		    // if we didn't find a trailing boundary,		    // the current buffer has data we need too		    if (!complete && inSize > 0)			buf.write(inbuf, 0, inSize);		    part = createMimeBodyPart(headers, buf.toByteArray());		}		super.addBodyPart(part);	    }	} catch (IOException ioex) {	    throw new MessagingException("IO Error", ioex);	} finally {	    try {		in.close();	    } catch (IOException cex) {		// ignore	    }	}	parsed = true;    }    /**     * Read data from the input stream to fill the buffer starting     * at the specified offset with the specified number of bytes.     * If len is zero, return zero.  If at EOF, return -1.  Otherwise,     * return the number of bytes read.  Call the read method on the     * input stream as many times as necessary to read len bytes.     *     * @param	in	InputStream to read from     * @param	buf	buffer to read into     * @param	off	offset in the buffer for first byte     * @param	len	number of bytes to read     * @return		-1 on EOF, otherwise number of bytes read     * @exception	IOException	on I/O errors     */    private static int readFully(InputStream in, byte[] buf, int off, int len)				throws IOException {	if (len == 0)	    return 0;	int total = 0;	while (len > 0) {	    int bsize = in.read(buf, off, len);	    if (bsize <= 0)	// should never be zero		break;	    off += bsize;	    total += bsize;	    len -= bsize;	}	return total > 0 ? total : -1;    }    /**     * Skip the specified number of bytes, repeatedly calling     * the skip method as necessary.     */    private void skipFully(InputStream in, long offset) throws IOException {	while (offset > 0) {	    long cur = in.skip(offset);	    if (cur <= 0)		throw new EOFException("can't skip");	    offset -= cur;	}    }    /**     * Create and return an InternetHeaders object that loads the     * headers from the given InputStream.  Subclasses can override     * this method to return a subclass of InternetHeaders, if     * necessary.  This implementation simply constructs and returns     * an InternetHeaders object.     *     * @param	is	the InputStream to read the headers from     * @exception  	MessagingException     * @since		JavaMail 1.2     */    protected InternetHeaders createInternetHeaders(InputStream is)				throws MessagingException {	return new InternetHeaders(is);    }    /**     * Create and return a MimeBodyPart object to represent a     * body part parsed from the InputStream.  Subclasses can override     * this method to return a subclass of MimeBodyPart, if     * necessary.  This implementation simply constructs and returns     * a MimeBodyPart object.     *     * @param	headers		the headers for the body part     * @param	content		the content of the body part     * @exception  		MessagingException     * @since			JavaMail 1.2     */    protected MimeBodyPart createMimeBodyPart(InternetHeaders headers,				byte[] content) throws MessagingException {	return new MimeBodyPart(headers, content);    }    /**     * Create and return a MimeBodyPart object to represent a     * body part parsed from the InputStream.  Subclasses can override     * this method to return a subclass of MimeBodyPart, if     * necessary.  This implementation simply constructs and returns     * a MimeBodyPart object.     *     * @param	is		InputStream containing the body part     * @exception  		MessagingException     * @since			JavaMail 1.2     */    protected MimeBodyPart createMimeBodyPart(InputStream is)				throws MessagingException {	return new MimeBodyPart(is);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -