| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BoundedInputStream |
|
| 2.0;2 |
| 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.IOException; | |
| 20 | import java.io.InputStream; | |
| 21 | ||
| 22 | /** | |
| 23 | * This is a stream that will only supply bytes up to a certain length - if its | |
| 24 | * position goes above that, it will stop. | |
| 25 | * <p> | |
| 26 | * This is useful to wrap ServletInputStreams. The ServletInputStream will block | |
| 27 | * if you try to read content from it that isn't there, because it doesn't know | |
| 28 | * whether the content hasn't arrived yet or whether the content has finished. | |
| 29 | * So, one of these, initialized with the Content-length sent in the | |
| 30 | * ServletInputStream's header, will stop it blocking, providing it's been sent | |
| 31 | * with a correct content length. | |
| 32 | * | |
| 33 | * @version $Id: BoundedInputStream.java 1415850 2012-11-30 20:51:39Z ggregory $ | |
| 34 | * @since 2.0 | |
| 35 | */ | |
| 36 | public class BoundedInputStream extends InputStream { | |
| 37 | ||
| 38 | /** the wrapped input stream */ | |
| 39 | private final InputStream in; | |
| 40 | ||
| 41 | /** the max length to provide */ | |
| 42 | private final long max; | |
| 43 | ||
| 44 | /** the number of bytes already returned */ | |
| 45 | 8 | private long pos = 0; |
| 46 | ||
| 47 | /** the marked position */ | |
| 48 | 8 | private long mark = -1; |
| 49 | ||
| 50 | /** flag if close shoud be propagated */ | |
| 51 | 8 | private boolean propagateClose = true; |
| 52 | ||
| 53 | /** | |
| 54 | * Creates a new <code>BoundedInputStream</code> that wraps the given input | |
| 55 | * stream and limits it to a certain size. | |
| 56 | * | |
| 57 | * @param in The wrapped input stream | |
| 58 | * @param size The maximum number of bytes to return | |
| 59 | */ | |
| 60 | 8 | public BoundedInputStream(final InputStream in, final long size) { |
| 61 | // Some badly designed methods - eg the servlet API - overload length | |
| 62 | // such that "-1" means stream finished | |
| 63 | 8 | this.max = size; |
| 64 | 8 | this.in = in; |
| 65 | 8 | } |
| 66 | ||
| 67 | /** | |
| 68 | * Creates a new <code>BoundedInputStream</code> that wraps the given input | |
| 69 | * stream and is unlimited. | |
| 70 | * | |
| 71 | * @param in The wrapped input stream | |
| 72 | */ | |
| 73 | public BoundedInputStream(final InputStream in) { | |
| 74 | 1 | this(in, -1); |
| 75 | 1 | } |
| 76 | ||
| 77 | /** | |
| 78 | * Invokes the delegate's <code>read()</code> method if | |
| 79 | * the current position is less than the limit. | |
| 80 | * @return the byte read or -1 if the end of stream or | |
| 81 | * the limit has been reached. | |
| 82 | * @throws IOException if an I/O error occurs | |
| 83 | */ | |
| 84 | @Override | |
| 85 | public int read() throws IOException { | |
| 86 | 30 | if (max >= 0 && pos >= max) { |
| 87 | 2 | return -1; |
| 88 | } | |
| 89 | 28 | final int result = in.read(); |
| 90 | 28 | pos++; |
| 91 | 28 | return result; |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * Invokes the delegate's <code>read(byte[])</code> method. | |
| 96 | * @param b the buffer to read the bytes into | |
| 97 | * @return the number of bytes read or -1 if the end of stream or | |
| 98 | * the limit has been reached. | |
| 99 | * @throws IOException if an I/O error occurs | |
| 100 | */ | |
| 101 | @Override | |
| 102 | public int read(final byte[] b) throws IOException { | |
| 103 | 9 | return this.read(b, 0, b.length); |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Invokes the delegate's <code>read(byte[], int, int)</code> method. | |
| 108 | * @param b the buffer to read the bytes into | |
| 109 | * @param off The start offset | |
| 110 | * @param len The number of bytes to read | |
| 111 | * @return the number of bytes read or -1 if the end of stream or | |
| 112 | * the limit has been reached. | |
| 113 | * @throws IOException if an I/O error occurs | |
| 114 | */ | |
| 115 | @Override | |
| 116 | public int read(final byte[] b, final int off, final int len) throws IOException { | |
| 117 | 9 | if (max>=0 && pos>=max) { |
| 118 | 3 | return -1; |
| 119 | } | |
| 120 | 6 | final long maxRead = max>=0 ? Math.min(len, max-pos) : len; |
| 121 | 6 | final int bytesRead = in.read(b, off, (int)maxRead); |
| 122 | ||
| 123 | 6 | if (bytesRead==-1) { |
| 124 | 2 | return -1; |
| 125 | } | |
| 126 | ||
| 127 | 4 | pos+=bytesRead; |
| 128 | 4 | return bytesRead; |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * Invokes the delegate's <code>skip(long)</code> method. | |
| 133 | * @param n the number of bytes to skip | |
| 134 | * @return the actual number of bytes skipped | |
| 135 | * @throws IOException if an I/O error occurs | |
| 136 | */ | |
| 137 | @Override | |
| 138 | public long skip(final long n) throws IOException { | |
| 139 | 0 | final long toSkip = max>=0 ? Math.min(n, max-pos) : n; |
| 140 | 0 | final long skippedBytes = in.skip(toSkip); |
| 141 | 0 | pos+=skippedBytes; |
| 142 | 0 | return skippedBytes; |
| 143 | } | |
| 144 | ||
| 145 | /** | |
| 146 | * {@inheritDoc} | |
| 147 | */ | |
| 148 | @Override | |
| 149 | public int available() throws IOException { | |
| 150 | 0 | if (max>=0 && pos>=max) { |
| 151 | 0 | return 0; |
| 152 | } | |
| 153 | 0 | return in.available(); |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * Invokes the delegate's <code>toString()</code> method. | |
| 158 | * @return the delegate's <code>toString()</code> | |
| 159 | */ | |
| 160 | @Override | |
| 161 | public String toString() { | |
| 162 | 0 | return in.toString(); |
| 163 | } | |
| 164 | ||
| 165 | /** | |
| 166 | * Invokes the delegate's <code>close()</code> method | |
| 167 | * if {@link #isPropagateClose()} is {@code true}. | |
| 168 | * @throws IOException if an I/O error occurs | |
| 169 | */ | |
| 170 | @Override | |
| 171 | public void close() throws IOException { | |
| 172 | 0 | if (propagateClose) { |
| 173 | 0 | in.close(); |
| 174 | } | |
| 175 | 0 | } |
| 176 | ||
| 177 | /** | |
| 178 | * Invokes the delegate's <code>reset()</code> method. | |
| 179 | * @throws IOException if an I/O error occurs | |
| 180 | */ | |
| 181 | @Override | |
| 182 | public synchronized void reset() throws IOException { | |
| 183 | 0 | in.reset(); |
| 184 | 0 | pos = mark; |
| 185 | 0 | } |
| 186 | ||
| 187 | /** | |
| 188 | * Invokes the delegate's <code>mark(int)</code> method. | |
| 189 | * @param readlimit read ahead limit | |
| 190 | */ | |
| 191 | @Override | |
| 192 | public synchronized void mark(final int readlimit) { | |
| 193 | 0 | in.mark(readlimit); |
| 194 | 0 | mark = pos; |
| 195 | 0 | } |
| 196 | ||
| 197 | /** | |
| 198 | * Invokes the delegate's <code>markSupported()</code> method. | |
| 199 | * @return true if mark is supported, otherwise false | |
| 200 | */ | |
| 201 | @Override | |
| 202 | public boolean markSupported() { | |
| 203 | 0 | return in.markSupported(); |
| 204 | } | |
| 205 | ||
| 206 | /** | |
| 207 | * Indicates whether the {@link #close()} method | |
| 208 | * should propagate to the underling {@link InputStream}. | |
| 209 | * | |
| 210 | * @return {@code true} if calling {@link #close()} | |
| 211 | * propagates to the <code>close()</code> method of the | |
| 212 | * underlying stream or {@code false} if it does not. | |
| 213 | */ | |
| 214 | public boolean isPropagateClose() { | |
| 215 | 0 | return propagateClose; |
| 216 | } | |
| 217 | ||
| 218 | /** | |
| 219 | * Set whether the {@link #close()} method | |
| 220 | * should propagate to the underling {@link InputStream}. | |
| 221 | * | |
| 222 | * @param propagateClose {@code true} if calling | |
| 223 | * {@link #close()} propagates to the <code>close()</code> | |
| 224 | * method of the underlying stream or | |
| 225 | * {@code false} if it does not. | |
| 226 | */ | |
| 227 | public void setPropagateClose(final boolean propagateClose) { | |
| 228 | 0 | this.propagateClose = propagateClose; |
| 229 | 0 | } |
| 230 | } |