View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package javax.mail.util;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import javax.mail.internet.SharedInputStream;
27  
28  public class SharedByteArrayInputStream extends ByteArrayInputStream implements SharedInputStream {
29  
30      /**
31       * Position within shared buffer that this stream starts at.
32       */
33      protected int start;
34  
35      /**
36       * Create a SharedByteArrayInputStream that shares the entire
37       * buffer.
38       *
39       * @param buf    The input data.
40       */
41      public SharedByteArrayInputStream(byte[] buf) {
42          this(buf, 0, buf.length);
43      }
44  
45  
46      /**
47       * Create a SharedByteArrayInputStream using a subset of the
48       * array data.
49       *
50       * @param buf    The source data array.
51       * @param offset The starting offset within the array.
52       * @param length The length of data to use.
53       */
54      public SharedByteArrayInputStream(byte[] buf, int offset, int length) {
55          super(buf, offset, length);
56          start = offset;
57      }
58  
59  
60      /**
61       * Get the position within the output stream, adjusted by the
62       * starting offset.
63       *
64       * @return The adjusted position within the stream.
65       */
66      public long getPosition() {
67          return pos - start;
68      }
69  
70  
71      /**
72       * Create a new input stream from this input stream, accessing
73       * a subset of the data.  Think of it as a substring operation
74       * for a stream.
75       *
76       * The starting offset must be non-negative.  The end offset can
77       * by -1, which means use the remainder of the stream.
78       *
79       * @param offset The starting offset.
80       * @param end    The end offset (which can be -1).
81       *
82       * @return An InputStream configured to access the indicated data subrange.
83       */
84      public InputStream newStream(long offset, long end) {
85          if (offset < 0) {
86              throw new IllegalArgumentException("Starting position must be non-negative");
87          }
88          if (end == -1) {
89              end = count - start;
90          }
91          return new SharedByteArrayInputStream(buf, start + (int)offset, (int)(end - offset));
92      }
93  }