Skip to content
Permalink
Browse files
Merge pull request #132 from arturobernalg/feature/end_of_stream
Add EOS constant that represent  the end of the stream
  • Loading branch information
sebbASF committed Apr 1, 2022
2 parents dbb30c7 + 7f1953d commit c7a48d03aac8ae69a28778c7c6982e7fcc34c842
Showing 2 changed files with 20 additions and 11 deletions.
@@ -101,6 +101,13 @@ public class CryptoInputStream extends InputStream implements

private static final int MIN_BUFFER_SIZE = 512;

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

/**
* Constructs a {@link CryptoInputStream}.
*
@@ -213,7 +220,7 @@ protected CryptoInputStream(final Input input, final CryptoCipher cipher, final
* Overrides the {@link java.io.InputStream#read()}. Reads the next byte of
* data from the input stream.
*
* @return the next byte of data, or {@code -1} if the end of the
* @return the next byte of data, or {@code EOS (-1)} if the end of the
* stream is reached.
* @throws IOException if an I/O error occurs.
*/
@@ -223,7 +230,7 @@ public int read() throws IOException {
while ((n = read(oneByteBuf, 0, 1)) == 0) { //NOPMD
/* no op */
}
return (n == -1) ? -1 : oneByteBuf[0] & 0xff;
return (n == EOS) ? EOS : oneByteBuf[0] & 0xff;
}

/**
@@ -380,7 +387,7 @@ public boolean isOpen() {
* sequence of bytes from this channel into the given buffer.
*
* @param dst The buffer into which bytes are to be transferred.
* @return The number of bytes read, possibly zero, or {@code -1} if the
* @return The number of bytes read, possibly zero, or {@code EOS (-1)} if the
* channel has reached end-of-stream.
* @throws IOException if an I/O error occurs.
*/
@@ -397,7 +404,7 @@ public int read(final ByteBuffer dst) throws IOException {
}

if (nd < 0) {
return -1;
return EOS;
}
}

@@ -485,7 +492,7 @@ protected void initCipher() throws IOException {
*/
protected int decryptMore() throws IOException {
if (finalDone) {
return -1;
return EOS;
}

final int n = input.read(inBuffer);
@@ -500,7 +507,7 @@ protected int decryptMore() throws IOException {
}

// End of the stream
return -1;
return EOS;
} else if (n == 0) {
// No data is read, but the stream is not end yet
return 0;
@@ -21,7 +21,9 @@
import java.io.InputStream;
import java.nio.ByteBuffer;

/**
import static org.apache.commons.crypto.stream.CryptoInputStream.EOS;

/**
* The StreamInput class takes a {@code InputStream} object and wraps it as
* {@code Input} object acceptable by {@code CryptoInputStream}.
*/
@@ -50,7 +52,7 @@ public StreamInput(final InputStream inputStream, final int bufferSize) {
* @param dst The buffer into which bytes are to be transferred.
*
* @return the total number of bytes read into the buffer, or
* {@code -1} if there is no more data because the end of the
* {@code EOS (-1)} if there is no more data because the end of the
* stream has been reached.
* @throws IOException if an I/O error occurs.
*/
@@ -60,9 +62,9 @@ public int read(final ByteBuffer dst) throws IOException {
int read = 0;
while (remaining > 0) {
final int n = in.read(buf, 0, Math.min(remaining, bufferSize));
if (n == -1) {
if (n == EOS) {
if (read == 0) {
read = -1;
read = EOS;
}
break;
} else if (n > 0) {
@@ -119,7 +121,7 @@ public int available() throws IOException {
* @param offset the start offset in array buffer.
* @param length the maximum number of bytes to read.
* @return the total number of bytes read into the buffer, or
* {@code -1} if there is no more data because the end of the
* {@code EOS (-1)} if there is no more data because the end of the
* stream has been reached.
* @throws IOException if an I/O error occurs.
*/

0 comments on commit c7a48d0

Please sign in to comment.