Skip to content

Package: SharedByteArrayInputStream

SharedByteArrayInputStream

nameinstructionbranchcomplexitylinemethod
SharedByteArrayInputStream(byte[])
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
SharedByteArrayInputStream(byte[], int, int)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getPosition()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
newStream(long, long)
M: 35 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package jakarta.mail.util;
18:
19: import java.io.*;
20:
21: import jakarta.mail.internet.SharedInputStream;
22:
23: /**
24: * A ByteArrayInputStream that implements the SharedInputStream interface,
25: * allowing the underlying byte array to be shared between multiple readers.
26: *
27: * @author Bill Shannon
28: * @since JavaMail 1.4
29: */
30:
31: public class SharedByteArrayInputStream extends ByteArrayInputStream
32:                                 implements SharedInputStream {
33: /**
34: * Position within shared buffer that this stream starts at.
35: */
36: protected int start = 0;
37:
38: /**
39: * Create a SharedByteArrayInputStream representing the entire
40: * byte array.
41: *
42: * @param        buf        the byte array
43: */
44: public SharedByteArrayInputStream(byte[] buf) {
45:         super(buf);
46: }
47:
48: /**
49: * Create a SharedByteArrayInputStream representing the part
50: * of the byte array from <code>offset</code> for <code>length</code>
51: * bytes.
52: *
53: * @param        buf        the byte array
54: * @param        offset        offset in byte array to first byte to include
55: * @param        length        number of bytes to include
56: */
57: public SharedByteArrayInputStream(byte[] buf, int offset, int length) {
58:         super(buf, offset, length);
59:         start = offset;
60: }
61:
62: /**
63: * Return the current position in the InputStream, as an
64: * offset from the beginning of the InputStream.
65: *
66: * @return the current position
67: */
68: @Override
69: public long getPosition() {
70:         return pos - start;
71: }
72:
73: /**
74: * Return a new InputStream representing a subset of the data
75: * from this InputStream, starting at <code>start</code> (inclusive)
76: * up to <code>end</code> (exclusive). <code>start</code> must be
77: * non-negative. If <code>end</code> is -1, the new stream ends
78: * at the same place as this stream. The returned InputStream
79: * will also implement the SharedInputStream interface.
80: *
81: * @param        start        the starting position
82: * @param        end        the ending position + 1
83: * @return                the new stream
84: */
85: @Override
86: public InputStream newStream(long start, long end) {
87:•        if (start < 0)
88:          throw new IllegalArgumentException("start < 0");
89:•        if (end == -1)
90:          end = count - this.start;
91:         return new SharedByteArrayInputStream(buf,
92:                                 this.start + (int)start, (int)(end - start));
93: }
94: }