Run dos2unix on BoundedInputStream.java

This commit is contained in:
Daniel Martí 2015-10-08 20:01:58 +02:00
parent 3eb758f1b2
commit 04ea84640e

View File

@ -1,230 +1,230 @@
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 * The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with * (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.commons.io.input; package org.apache.commons.io.input;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
/** /**
* This is a stream that will only supply bytes up to a certain length - if its * This is a stream that will only supply bytes up to a certain length - if its
* position goes above that, it will stop. * position goes above that, it will stop.
* <p> * <p>
* This is useful to wrap ServletInputStreams. The ServletInputStream will block * This is useful to wrap ServletInputStreams. The ServletInputStream will block
* if you try to read content from it that isn't there, because it doesn't know * if you try to read content from it that isn't there, because it doesn't know
* whether the content hasn't arrived yet or whether the content has finished. * whether the content hasn't arrived yet or whether the content has finished.
* So, one of these, initialized with the Content-length sent in the * So, one of these, initialized with the Content-length sent in the
* ServletInputStream's header, will stop it blocking, providing it's been sent * ServletInputStream's header, will stop it blocking, providing it's been sent
* with a correct content length. * with a correct content length.
* *
* @version $Id: BoundedInputStream.java 1307462 2012-03-30 15:13:11Z ggregory $ * @version $Id: BoundedInputStream.java 1307462 2012-03-30 15:13:11Z ggregory $
* @since 2.0 * @since 2.0
*/ */
public class BoundedInputStream extends InputStream { public class BoundedInputStream extends InputStream {
/** the wrapped input stream */ /** the wrapped input stream */
private final InputStream in; private final InputStream in;
/** the max length to provide */ /** the max length to provide */
private final long max; private final long max;
/** the number of bytes already returned */ /** the number of bytes already returned */
private long pos = 0; private long pos = 0;
/** the marked position */ /** the marked position */
private long mark = -1; private long mark = -1;
/** flag if close shoud be propagated */ /** flag if close shoud be propagated */
private boolean propagateClose = true; private boolean propagateClose = true;
/** /**
* Creates a new <code>BoundedInputStream</code> that wraps the given input * Creates a new <code>BoundedInputStream</code> that wraps the given input
* stream and limits it to a certain size. * stream and limits it to a certain size.
* *
* @param in The wrapped input stream * @param in The wrapped input stream
* @param size The maximum number of bytes to return * @param size The maximum number of bytes to return
*/ */
public BoundedInputStream(InputStream in, long size) { public BoundedInputStream(InputStream in, long size) {
// Some badly designed methods - eg the servlet API - overload length // Some badly designed methods - eg the servlet API - overload length
// such that "-1" means stream finished // such that "-1" means stream finished
this.max = size; this.max = size;
this.in = in; this.in = in;
} }
/** /**
* Creates a new <code>BoundedInputStream</code> that wraps the given input * Creates a new <code>BoundedInputStream</code> that wraps the given input
* stream and is unlimited. * stream and is unlimited.
* *
* @param in The wrapped input stream * @param in The wrapped input stream
*/ */
public BoundedInputStream(InputStream in) { public BoundedInputStream(InputStream in) {
this(in, -1); this(in, -1);
} }
/** /**
* Invokes the delegate's <code>read()</code> method if * Invokes the delegate's <code>read()</code> method if
* the current position is less than the limit. * the current position is less than the limit.
* @return the byte read or -1 if the end of stream or * @return the byte read or -1 if the end of stream or
* the limit has been reached. * the limit has been reached.
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
@Override @Override
public int read() throws IOException { public int read() throws IOException {
if (max >= 0 && pos >= max) { if (max >= 0 && pos >= max) {
return -1; return -1;
} }
int result = in.read(); int result = in.read();
pos++; pos++;
return result; return result;
} }
/** /**
* Invokes the delegate's <code>read(byte[])</code> method. * Invokes the delegate's <code>read(byte[])</code> method.
* @param b the buffer to read the bytes into * @param b the buffer to read the bytes into
* @return the number of bytes read or -1 if the end of stream or * @return the number of bytes read or -1 if the end of stream or
* the limit has been reached. * the limit has been reached.
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
@Override @Override
public int read(byte[] b) throws IOException { public int read(byte[] b) throws IOException {
return this.read(b, 0, b.length); return this.read(b, 0, b.length);
} }
/** /**
* Invokes the delegate's <code>read(byte[], int, int)</code> method. * Invokes the delegate's <code>read(byte[], int, int)</code> method.
* @param b the buffer to read the bytes into * @param b the buffer to read the bytes into
* @param off The start offset * @param off The start offset
* @param len The number of bytes to read * @param len The number of bytes to read
* @return the number of bytes read or -1 if the end of stream or * @return the number of bytes read or -1 if the end of stream or
* the limit has been reached. * the limit has been reached.
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
@Override @Override
public int read(byte[] b, int off, int len) throws IOException { public int read(byte[] b, int off, int len) throws IOException {
if (max >= 0 && pos >= max) { if (max >= 0 && pos >= max) {
return -1; return -1;
} }
long maxRead = max >= 0 ? Math.min(len, max - pos) : len; long maxRead = max >= 0 ? Math.min(len, max - pos) : len;
int bytesRead = in.read(b, off, (int) maxRead); int bytesRead = in.read(b, off, (int) maxRead);
if (bytesRead == -1) { if (bytesRead == -1) {
return -1; return -1;
} }
pos += bytesRead; pos += bytesRead;
return bytesRead; return bytesRead;
} }
/** /**
* Invokes the delegate's <code>skip(long)</code> method. * Invokes the delegate's <code>skip(long)</code> method.
* @param n the number of bytes to skip * @param n the number of bytes to skip
* @return the actual number of bytes skipped * @return the actual number of bytes skipped
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
@Override @Override
public long skip(long n) throws IOException { public long skip(long n) throws IOException {
long toSkip = max >= 0 ? Math.min(n, max - pos) : n; long toSkip = max >= 0 ? Math.min(n, max - pos) : n;
long skippedBytes = in.skip(toSkip); long skippedBytes = in.skip(toSkip);
pos += skippedBytes; pos += skippedBytes;
return skippedBytes; return skippedBytes;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public int available() throws IOException { public int available() throws IOException {
if (max >= 0 && pos >= max) { if (max >= 0 && pos >= max) {
return 0; return 0;
} }
return in.available(); return in.available();
} }
/** /**
* Invokes the delegate's <code>toString()</code> method. * Invokes the delegate's <code>toString()</code> method.
* @return the delegate's <code>toString()</code> * @return the delegate's <code>toString()</code>
*/ */
@Override @Override
public String toString() { public String toString() {
return in.toString(); return in.toString();
} }
/** /**
* Invokes the delegate's <code>close()</code> method * Invokes the delegate's <code>close()</code> method
* if {@link #isPropagateClose()} is {@code true}. * if {@link #isPropagateClose()} is {@code true}.
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (propagateClose) { if (propagateClose) {
in.close(); in.close();
} }
} }
/** /**
* Invokes the delegate's <code>reset()</code> method. * Invokes the delegate's <code>reset()</code> method.
* @throws IOException if an I/O error occurs * @throws IOException if an I/O error occurs
*/ */
@Override @Override
public synchronized void reset() throws IOException { public synchronized void reset() throws IOException {
in.reset(); in.reset();
pos = mark; pos = mark;
} }
/** /**
* Invokes the delegate's <code>mark(int)</code> method. * Invokes the delegate's <code>mark(int)</code> method.
* @param readlimit read ahead limit * @param readlimit read ahead limit
*/ */
@Override @Override
public synchronized void mark(int readlimit) { public synchronized void mark(int readlimit) {
in.mark(readlimit); in.mark(readlimit);
mark = pos; mark = pos;
} }
/** /**
* Invokes the delegate's <code>markSupported()</code> method. * Invokes the delegate's <code>markSupported()</code> method.
* @return true if mark is supported, otherwise false * @return true if mark is supported, otherwise false
*/ */
@Override @Override
public boolean markSupported() { public boolean markSupported() {
return in.markSupported(); return in.markSupported();
} }
/** /**
* Indicates whether the {@link #close()} method * Indicates whether the {@link #close()} method
* should propagate to the underling {@link InputStream}. * should propagate to the underling {@link InputStream}.
* *
* @return {@code true} if calling {@link #close()} * @return {@code true} if calling {@link #close()}
* propagates to the <code>close()</code> method of the * propagates to the <code>close()</code> method of the
* underlying stream or {@code false} if it does not. * underlying stream or {@code false} if it does not.
*/ */
public boolean isPropagateClose() { public boolean isPropagateClose() {
return propagateClose; return propagateClose;
} }
/** /**
* Set whether the {@link #close()} method * Set whether the {@link #close()} method
* should propagate to the underling {@link InputStream}. * should propagate to the underling {@link InputStream}.
* *
* @param propagateClose {@code true} if calling * @param propagateClose {@code true} if calling
* {@link #close()} propagates to the <code>close()</code> * {@link #close()} propagates to the <code>close()</code>
* method of the underlying stream or * method of the underlying stream or
* {@code false} if it does not. * {@code false} if it does not.
*/ */
public void setPropagateClose(boolean propagateClose) { public void setPropagateClose(boolean propagateClose) {
this.propagateClose = propagateClose; this.propagateClose = propagateClose;
} }
} }