001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * @(#)SharedByteArrayInputStream.java 1.5 07/05/04
039: */
040:
041: package javax.mail.util;
042:
043: import java.io.*;
044: import javax.mail.internet.SharedInputStream;
045:
046: /**
047: * A ByteArrayInputStream that implements the SharedInputStream interface,
048: * allowing the underlying byte array to be shared between multiple readers.
049: *
050: * @version 1.5, 07/05/04
051: * @author Bill Shannon
052: * @since JavaMail 1.4
053: */
054:
055: public class SharedByteArrayInputStream extends ByteArrayInputStream
056: implements SharedInputStream {
057: /**
058: * Position within shared buffer that this stream starts at.
059: */
060: protected int start = 0;
061:
062: /**
063: * Create a SharedByteArrayInputStream representing the entire
064: * byte array.
065: *
066: * @param buf the byte array
067: */
068: public SharedByteArrayInputStream(byte[] buf) {
069: super (buf);
070: }
071:
072: /**
073: * Create a SharedByteArrayInputStream representing the part
074: * of the byte array from <code>offset</code> for <code>length</code>
075: * bytes.
076: *
077: * @param buf the byte array
078: * @param offset offset in byte array to first byte to include
079: * @param length number of bytes to include
080: */
081: public SharedByteArrayInputStream(byte[] buf, int offset, int length) {
082: super (buf, offset, length);
083: start = offset;
084: }
085:
086: /**
087: * Return the current position in the InputStream, as an
088: * offset from the beginning of the InputStream.
089: *
090: * @return the current position
091: */
092: public long getPosition() {
093: return pos - start;
094: }
095:
096: /**
097: * Return a new InputStream representing a subset of the data
098: * from this InputStream, starting at <code>start</code> (inclusive)
099: * up to <code>end</code> (exclusive). <code>start</code> must be
100: * non-negative. If <code>end</code> is -1, the new stream ends
101: * at the same place as this stream. The returned InputStream
102: * will also implement the SharedInputStream interface.
103: *
104: * @param start the starting position
105: * @param end the ending position + 1
106: * @return the new stream
107: */
108: public InputStream newStream(long start, long end) {
109: if (start < 0)
110: throw new IllegalArgumentException("start < 0");
111: if (end == -1)
112: end = count - this .start;
113: return new SharedByteArrayInputStream(buf, this .start
114: + (int) start, (int) (end - start));
115: }
116: }
|