View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http2.impl.io;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.util.ArrayDeque;
33  import java.util.Queue;
34  
35  class MultiByteArrayInputStream extends InputStream {
36  
37      private final Queue<byte[]> bufs;
38      private byte[] current;
39      private int pos;
40  
41      public MultiByteArrayInputStream(final byte[]... bufs) {
42          super();
43          this.bufs = new ArrayDeque<>();
44          for (final byte[] buf: bufs) {
45              if (buf.length > 0) {
46                  this.bufs.add(buf);
47              }
48          }
49      }
50  
51      private void advance() {
52          if (this.current != null) {
53              if (this.pos >= this.current.length) {
54                  this.current = null;
55              }
56          }
57          if (this.current == null) {
58              this.current = this.bufs.poll();
59              this.pos = 0;
60          }
61      }
62  
63      @Override
64      public int read() throws IOException {
65          advance();
66          if (this.current == null) {
67              return -1;
68          }
69          return this.current[this.pos++];
70      }
71  
72      @Override
73      public int read(final byte b[], final int off, final int len) throws IOException {
74          if (b == null) {
75              throw new NullPointerException();
76          } else if ((off < 0) || (off > b.length) || (len < 0) ||
77                 ((off + len) > b.length) || ((off + len) < 0)) {
78              throw new IndexOutOfBoundsException();
79          }
80          advance();
81          if (this.current == null) {
82              return -1;
83          }
84          final int chunk = Math.min(this.current.length - this.pos, len);
85          if (chunk <= 0) {
86              return 0;
87          }
88          System.arraycopy(this.current, this.pos, b, off, chunk);
89          this.pos += chunk;
90          return chunk;
91      }
92  
93      @Override
94      public long skip(final long n) {
95          advance();
96          final int chunk = Math.min(this.current.length - this.pos, (int) n);
97          if (chunk <= 0) {
98              return 0;
99          }
100         this.pos += chunk;
101         return chunk;
102     }
103 
104     @Override
105     public int available() {
106         advance();
107         return this.current != null ? this.current.length - this.pos : 0;
108     }
109 
110     @Override
111     public boolean markSupported() {
112         return false;
113     }
114 
115 }