Skip to content

Commit

Permalink
FILEUPLOAD-316 - Add EOS constant that represent the end of the stream
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Apr 23, 2021
1 parent 289acf5 commit f9a40de
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/main/java/org/apache/commons/fileupload2/MultipartStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
*/
public class MultipartStream {

/**
* The index value when the end of the stream has been reached {@code -1}.
* @since 2.0
*/
public static final int EOS = -1;

/**
* Internal class, which is used to invoke the
* {@link ProgressListener}.
Expand Down Expand Up @@ -426,7 +432,7 @@ public byte readByte() throws IOException {
head = 0;
// Refill.
tail = input.read(buffer, head, bufSize);
if (tail == -1) {
if (tail == EOS) {
// No more data available.
throw new IOException("No more data is available");
}
Expand Down Expand Up @@ -708,7 +714,7 @@ public static boolean arrayequals(final byte[] a,
* @param pos The starting position for searching.
*
* @return The position of byte found, counting from beginning of the
* {@code buffer}, or {@code -1} if not found.
* {@code buffer}, or {@code EOS (-1)} if not found.
*/
protected int findByte(final byte value,
final int pos) {
Expand All @@ -718,15 +724,15 @@ protected int findByte(final byte value,
}
}

return -1;
return EOS;
}

/**
* Searches for the {@code boundary} in the {@code buffer}
* region delimited by {@code head} and {@code tail}.
*
* @return The position of the boundary found, counting from the
* beginning of the {@code buffer}, or {@code -1} if
* beginning of the {@code buffer}, or {@code EOS (-1)} if
* not found.
*/
protected int findSeparator() {
Expand All @@ -744,7 +750,7 @@ protected int findSeparator() {
return bufferPos - boundaryLength;
}
}
return -1;
return EOS;
}

/**
Expand Down Expand Up @@ -887,7 +893,7 @@ public int available() throws IOException {
* Returns the next byte in the stream.
*
* @return The next byte in the stream, as a non-negative
* integer, or -1 for EOF.
* integer, or {@code EOF (-1)} for EOS.
* @throws IOException An I/O error occurred.
*/
@Override
Expand All @@ -896,7 +902,7 @@ public int read() throws IOException {
throw new FileItemStream.ItemSkippedException();
}
if (available() == 0 && makeAvailable() == 0) {
return -1;
return EOS;
}
++total;
final int b = buffer[head++];
Expand All @@ -913,7 +919,7 @@ public int read() throws IOException {
* @param off Offset of the first byte in the buffer.
* @param len Maximum number of bytes to read.
* @return Number of bytes, which have been actually read,
* or -1 for EOF.
* or {@code EOF (-1)} for EOS.
* @throws IOException An I/O error occurred.
*/
@Override
Expand All @@ -928,7 +934,7 @@ public int read(final byte[] b, final int off, final int len) throws IOException
if (res == 0) {
res = makeAvailable();
if (res == 0) {
return -1;
return EOS;
}
}
res = Math.min(res, len);
Expand Down Expand Up @@ -1009,7 +1015,7 @@ public long skip(final long bytes) throws IOException {
* @throws IOException An I/O error occurred.
*/
private int makeAvailable() throws IOException {
if (pos != -1) {
if (pos != EOS) {
return 0;
}

Expand All @@ -1023,7 +1029,7 @@ private int makeAvailable() throws IOException {

for (;;) {
final int bytesRead = input.read(buffer, tail, bufSize - tail);
if (bytesRead == -1) {
if (bytesRead == EOS) {
// The last pad amount is left in the buffer.
// Boundary can't be in there so signal an error
// condition.
Expand All @@ -1038,7 +1044,7 @@ private int makeAvailable() throws IOException {
findSeparator();
final int av = available();

if (av > 0 || pos != -1) {
if (av > 0 || pos != EOS) {
return av;
}
}
Expand Down

0 comments on commit f9a40de

Please sign in to comment.