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

📄 sharedfileinputstream.java

📁 java Email you can use it to send email to others
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License").  You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code.  If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license."  If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above.  However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. *//* * @(#)SharedFileInputStream.java	1.11 07/05/04 */package javax.mail.util;import java.io.*;import javax.mail.internet.SharedInputStream;/** * A <code>SharedFileInputStream</code> is a * <code>BufferedInputStream</code> that buffers * data from the file and supports the <code>mark</code> * and <code>reset</code> methods.  It also supports the * <code>newStream</code> method that allows you to create * other streams that represent subsets of the file. * A <code>RandomAccessFile</code> object is used to * access the file data. <p> * * Note that when the SharedFileInputStream is closed, * all streams created with the <code>newStream</code> * method are also closed.  This allows the creator of the * SharedFileInputStream object to control access to the * underlying file and ensure that it is closed when * needed, to avoid leaking file descriptors.  Note also * that this behavior contradicts the requirements of * SharedInputStream and may change in a future release. * * @author  Bill Shannon * @since   JavaMail 1.4 */public class SharedFileInputStream extends BufferedInputStream				implements SharedInputStream {    private static int defaultBufferSize = 2048;    /**     * The file containing the data.     * Shared by all related SharedFileInputStreams.     */    protected RandomAccessFile in;    /**     * The normal size of the read buffer.     */    protected int bufsize;    /**     * The file offset that corresponds to the first byte in     * the read buffer.     */    protected long bufpos;    /**     * The file offset of the start of data in this subset of the file.     */    protected long start = 0;    /**     * The amount of data in this subset of the file.     */    protected long datalen;    /**     * True if this is a top level stream created directly by "new".     * False if this is a derived stream created by newStream.     */    private boolean master = true;    /**     * A shared class that keeps track of the references     * to a particular file so it can be closed when the     * last reference is gone.     */    static class SharedFile {	private int cnt;	private RandomAccessFile in;	SharedFile(String file) throws IOException {	    this.in = new RandomAccessFile(file, "r");	}	SharedFile(File file) throws IOException {	    this.in = new RandomAccessFile(file, "r");	}	public RandomAccessFile open() {	    cnt++;	    return in;	}	public synchronized void close() throws IOException {	    if (cnt > 0 && --cnt <= 0)		in.close();	}	public synchronized void forceClose() throws IOException {	    if (cnt > 0) {		// normal case, close exceptions propagated		cnt = 0;		in.close();	    } else {		// should already be closed, ignore exception		try {		    in.close();		} catch (IOException ioex) { }	    }	}	protected void finalize() throws Throwable {	    super.finalize();	    in.close();	}    }    private SharedFile sf;    /**     * Check to make sure that this stream has not been closed     */    private void ensureOpen() throws IOException {	if (in == null)	    throw new IOException("Stream closed");    }    /**     * Creates a <code>SharedFileInputStream</code>     * for the file.     *     * @param   file   the file     */    public SharedFileInputStream(File file) throws IOException {	this(file, defaultBufferSize);    }    /**     * Creates a <code>SharedFileInputStream</code>     * for the named file     *     * @param   file   the file     */    public SharedFileInputStream(String file) throws IOException {	this(file, defaultBufferSize);    }    /**     * Creates a <code>SharedFileInputStream</code>     * with the specified buffer size.     *     * @param   file	the file     * @param   size   the buffer size.     * @exception IllegalArgumentException if size <= 0.     */    public SharedFileInputStream(File file, int size) throws IOException {	super(null);	// XXX - will it NPE?        if (size <= 0)            throw new IllegalArgumentException("Buffer size <= 0");	init(new SharedFile(file), size);    }    /**     * Creates a <code>SharedFileInputStream</code>     * with the specified buffer size.     *     * @param   file	the file     * @param   size   the buffer size.     * @exception IllegalArgumentException if size <= 0.     */    public SharedFileInputStream(String file, int size) throws IOException {	super(null);	// XXX - will it NPE?        if (size <= 0)            throw new IllegalArgumentException("Buffer size <= 0");	init(new SharedFile(file), size);    }    private void init(SharedFile sf, int size) throws IOException {	this.sf = sf;	this.in = sf.open();	this.start = 0;	this.datalen = in.length();	// XXX - file can't grow	this.bufsize = size;	buf = new byte[size];    }    /**     * Used internally by the <code>newStream</code> method.     */    private SharedFileInputStream(SharedFile sf, long start, long len,				int bufsize) {	super(null);	this.master = false;	this.sf = sf;	this.in = sf.open();	this.start = start;	this.bufpos = start;	this.datalen = len;	this.bufsize = bufsize;	buf = new byte[bufsize];    }    /**     * Fills the buffer with more data, taking into account     * shuffling and other tricks for dealing with marks.     * Assumes that it is being called by a synchronized method.     * This method also assumes that all data has already been read in,     * hence pos > count.     */    private void fill() throws IOException {	if (markpos < 0) {	    pos = 0;		/* no mark: throw away the buffer */	    bufpos += count;	} else if (pos >= buf.length)	/* no room left in buffer */	    if (markpos > 0) {	/* can throw away early part of the buffer */		int sz = pos - markpos;		System.arraycopy(buf, markpos, buf, 0, sz);		pos = sz;		bufpos += markpos;		markpos = 0;	    } else if (buf.length >= marklimit) {		markpos = -1;	/* buffer got too big, invalidate mark */		pos = 0;	/* drop buffer contents */		bufpos += count;	    } else {		/* grow buffer */		int nsz = pos * 2;		if (nsz > marklimit)		    nsz = marklimit;		byte nbuf[] = new byte[nsz];		System.arraycopy(buf, 0, nbuf, 0, pos);		buf = nbuf;	    }        count = pos;	in.seek(bufpos + pos);	// limit to datalen	int len = buf.length - pos;	if (bufpos - start + pos + len > datalen)

⌨️ 快捷键说明

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