|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.commons.io.input; |
| 18 | + |
| 19 | +import java.io.InputStream; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +import static java.lang.Math.min; |
| 23 | + |
| 24 | +/** |
| 25 | + * This is an alternative to {@link java.io.ByteArrayInputStream} |
| 26 | + * which removes the synchronization overhead for non-concurrent |
| 27 | + * access; as such this class is not thread-safe. |
| 28 | + * |
| 29 | + * @since 2.7 |
| 30 | + */ |
| 31 | +//@NotThreadSafe |
| 32 | +public class UnsynchronizedByteArrayInputStream extends InputStream { |
| 33 | + |
| 34 | + public static final int END_OF_STREAM = -1; |
| 35 | + |
| 36 | + /** |
| 37 | + * The underlying data buffer. |
| 38 | + */ |
| 39 | + private final byte[] data; |
| 40 | + |
| 41 | + /** |
| 42 | + * End Of Data. |
| 43 | + * |
| 44 | + * Similar to data.length, |
| 45 | + * i.e. the last readable offset + 1. |
| 46 | + */ |
| 47 | + private final int eod; |
| 48 | + |
| 49 | + /** |
| 50 | + * Current offset in the data buffer. |
| 51 | + */ |
| 52 | + private int offset; |
| 53 | + |
| 54 | + /** |
| 55 | + * The current mark (if any). |
| 56 | + */ |
| 57 | + private int markedOffset; |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates a new byte array input stream. |
| 61 | + * |
| 62 | + * @param data the buffer |
| 63 | + */ |
| 64 | + public UnsynchronizedByteArrayInputStream(final byte[] data) { |
| 65 | + Objects.requireNonNull(data); |
| 66 | + this.data = data; |
| 67 | + this.offset = 0; |
| 68 | + this.eod = data.length; |
| 69 | + this.markedOffset = this.offset; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates a new byte array input stream. |
| 74 | + * |
| 75 | + * @param data the buffer |
| 76 | + * @param offset the offset into the buffer |
| 77 | + * |
| 78 | + * @throws IllegalArgumentException if the offset is less than zero |
| 79 | + */ |
| 80 | + public UnsynchronizedByteArrayInputStream(final byte[] data, final int offset) { |
| 81 | + Objects.requireNonNull(data); |
| 82 | + if (offset < 0) { |
| 83 | + throw new IllegalArgumentException("offset cannot be negative"); |
| 84 | + } |
| 85 | + this.data = data; |
| 86 | + this.offset = min(offset, data.length > 0 ? data.length: offset); |
| 87 | + this.eod = data.length; |
| 88 | + this.markedOffset = this.offset; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * Creates a new byte array input stream. |
| 94 | + * |
| 95 | + * @param data the buffer |
| 96 | + * @param offset the offset into the buffer |
| 97 | + * @param length the length of the buffer |
| 98 | + * |
| 99 | + * @throws IllegalArgumentException if the offset or length less than zero |
| 100 | + */ |
| 101 | + public UnsynchronizedByteArrayInputStream(final byte[] data, final int offset, final int length) { |
| 102 | + Objects.requireNonNull(data); |
| 103 | + if (offset < 0) { |
| 104 | + throw new IllegalArgumentException("offset cannot be negative"); |
| 105 | + } |
| 106 | + if (length < 0) { |
| 107 | + throw new IllegalArgumentException("length cannot be negative"); |
| 108 | + } |
| 109 | + this.data = data; |
| 110 | + this.offset = min(offset, data.length > 0 ? data.length : offset); |
| 111 | + this.eod = min(this.offset + length, data.length); |
| 112 | + this.markedOffset = this.offset; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public int available() { |
| 117 | + return offset < eod ? eod - offset : 0; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public int read() { |
| 122 | + return offset < eod ? data[offset++] & 0xff : END_OF_STREAM; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public int read(final byte[] b) { |
| 127 | + Objects.requireNonNull(b); |
| 128 | + return read(b, 0, b.length); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public int read(final byte[] b, final int off, final int len) { |
| 133 | + Objects.requireNonNull(b); |
| 134 | + if (off < 0 || len < 0 || off + len > b.length) { |
| 135 | + throw new IndexOutOfBoundsException(); |
| 136 | + } |
| 137 | + |
| 138 | + if (offset >= eod) { |
| 139 | + return END_OF_STREAM; |
| 140 | + } |
| 141 | + |
| 142 | + int actualLen = eod - offset; |
| 143 | + if (len < actualLen) { |
| 144 | + actualLen = len; |
| 145 | + } |
| 146 | + if (actualLen <= 0) { |
| 147 | + return 0; |
| 148 | + } |
| 149 | + System.arraycopy(data, offset, b, off, actualLen); |
| 150 | + offset += actualLen; |
| 151 | + return actualLen; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public long skip(final long n) { |
| 156 | + if(n < 0) { |
| 157 | + throw new IllegalArgumentException("Skipping backward is not supported"); |
| 158 | + } |
| 159 | + |
| 160 | + long actualSkip = eod - offset; |
| 161 | + if (n < actualSkip) { |
| 162 | + actualSkip = n; |
| 163 | + } |
| 164 | + |
| 165 | + offset += actualSkip; |
| 166 | + return actualSkip; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public boolean markSupported() { |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void mark(final int readlimit) { |
| 176 | + this.markedOffset = this.offset; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void reset() { |
| 181 | + this.offset = this.markedOffset; |
| 182 | + } |
| 183 | +} |
0 commit comments