KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > util > SharedByteArrayInputStream


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)SharedByteArrayInputStream.java 1.4 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.util;
29
30 import java.io.*;
31 import javax.mail.internet.SharedInputStream JavaDoc;
32
33 /**
34  * A ByteArrayInputStream that implements the SharedInputStream interface,
35  * allowing the underlying byte array to be shared between multiple readers.
36  *
37  * @version 1.4, 05/08/29
38  * @author Bill Shannon
39  * @since JavaMail 1.4
40  */

41
42 public class SharedByteArrayInputStream extends ByteArrayInputStream
43                 implements SharedInputStream JavaDoc {
44     /**
45      * Position within shared buffer that this stream starts at.
46      */

47     protected int start = 0;
48
49     /**
50      * Create a SharedByteArrayInputStream representing the entire
51      * byte array.
52      *
53      * @param buf the byte array
54      */

55     public SharedByteArrayInputStream(byte[] buf) {
56     super(buf);
57     }
58
59     /**
60      * Create a SharedByteArrayInputStream representing the part
61      * of the byte array from <code>offset</code> for <code>length</code>
62      * bytes.
63      *
64      * @param buf the byte array
65      * @param offset offset in byte array to first byte to include
66      * @param length number of bytes to include
67      */

68     public SharedByteArrayInputStream(byte[] buf, int offset, int length) {
69     super(buf, offset, length);
70     start = offset;
71     }
72
73     /**
74      * Return the current position in the InputStream, as an
75      * offset from the beginning of the InputStream.
76      *
77      * @return the current position
78      */

79     public long getPosition() {
80     return pos - start;
81     }
82
83     /**
84      * Return a new InputStream representing a subset of the data
85      * from this InputStream, starting at <code>start</code> (inclusive)
86      * up to <code>end</code> (exclusive). <code>start</code> must be
87      * non-negative. If <code>end</code> is -1, the new stream ends
88      * at the same place as this stream. The returned InputStream
89      * will also implement the SharedInputStream interface.
90      *
91      * @param start the starting position
92      * @param end the ending position + 1
93      * @return the new stream
94      */

95     public InputStream JavaDoc newStream(long start, long end) {
96     if (start < 0)
97         throw new IllegalArgumentException JavaDoc("start < 0");
98     if (end == -1)
99         end = count - this.start;
100     return new SharedByteArrayInputStream(buf,
101                 this.start + (int)start, (int)(end - start));
102     }
103 }
104
Popular Tags