001    /**
002     *
003     * Copyright 2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package javax.mail.util;
019    
020    import java.io.ByteArrayInputStream;
021    import java.io.IOException;
022    import java.io.InputStream;
023    
024    import javax.mail.internet.SharedInputStream;
025    
026    public class SharedByteArrayInputStream extends ByteArrayInputStream implements SharedInputStream {
027    
028        /**
029         * Position within shared buffer that this stream starts at.
030         */
031        protected int start;
032    
033        /**
034         * Create a SharedByteArrayInputStream that shares the entire
035         * buffer.
036         *
037         * @param buf    The input data.
038         */
039        public SharedByteArrayInputStream(byte[] buf) {
040            this(buf, 0, buf.length);
041        }
042    
043    
044        /**
045         * Create a SharedByteArrayInputStream using a subset of the
046         * array data.
047         *
048         * @param buf    The source data array.
049         * @param offset The starting offset within the array.
050         * @param length The length of data to use.
051         */
052        public SharedByteArrayInputStream(byte[] buf, int offset, int length) {
053            super(buf, offset, length);
054            start = offset;
055        }
056    
057    
058        /**
059         * Get the position within the output stream, adjusted by the
060         * starting offset.
061         *
062         * @return The adjusted position within the stream.
063         */
064        public long getPosition() {
065            return pos - start;
066        }
067    
068    
069        /**
070         * Create a new input stream from this input stream, accessing
071         * a subset of the data.  Think of it as a substring operation
072         * for a stream.
073         *
074         * The starting offset must be non-negative.  The end offset can
075         * by -1, which means use the remainder of the stream.
076         *
077         * @param offset The starting offset.
078         * @param end    The end offset (which can be -1).
079         *
080         * @return An InputStream configured to access the indicated data subrange.
081         */
082        public InputStream newStream(long offset, long end) {
083            if (offset < 0) {
084                throw new IllegalArgumentException("Starting position must be non-negative");
085            }
086            if (end == -1) {
087                end = count - start;
088            }
089            return new SharedByteArrayInputStream(buf, start + (int)offset, (int)(end - offset));
090        }
091    }