Skip to content
Permalink
Browse files
Merge pull request #75 from arturobernalg/feature/end_of_stream
Add EOS constant. The end of the stream or the file
  • Loading branch information
sebbASF committed Apr 15, 2022
2 parents eb0de26 + b5f0ca8 commit 69d88f0b284525b259f2870aa08b70a53dfdaac5
Showing 8 changed files with 37 additions and 19 deletions.
@@ -26,6 +26,7 @@

import org.apache.commons.net.SocketClient;
import org.apache.commons.net.io.SocketInputStream;
import org.apache.commons.net.util.NetConstants;

/**
* RExecClient implements the rexec() facility that first appeared in
@@ -271,7 +272,7 @@ public void rexec(final String username, final String password,
if (ch > 0) {
final StringBuilder buffer = new StringBuilder();

while ((ch = _input_.read()) != -1 && ch != '\n') {
while ((ch = _input_.read()) != NetConstants.EOS && ch != '\n') {
buffer.append((char)ch);
}

@@ -22,6 +22,7 @@
import java.io.OutputStream;

import org.apache.commons.net.io.Util;
import org.apache.commons.net.util.NetConstants;

/**
* This is a utility class providing a reader/writer capability required
@@ -50,7 +51,7 @@ public static void readWrite(final InputStream remoteInput,

try
{
while (!Thread.interrupted() && (ch = localInput.read()) != -1)
while (!Thread.interrupted() && (ch = localInput.read()) != NetConstants.EOS)
{
remoteOutput.write(ch);
remoteOutput.flush();
@@ -60,8 +61,7 @@ public static void readWrite(final InputStream remoteInput,
{
//e.printStackTrace();
}
})
;
});


writer = new Thread(() -> {
@@ -17,6 +17,8 @@

package org.apache.commons.net.io;

import org.apache.commons.net.util.NetConstants;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
@@ -55,7 +57,7 @@ public String readLine() throws IOException {
int intch;
boolean prevWasCR = false;
synchronized(lock) { // make thread-safe (hopefully!)
while((intch = read()) != -1)
while((intch = read()) != NetConstants.EOS)
{
if (prevWasCR && intch == LF) {
return sb.substring(0, sb.length()-1);
@@ -17,6 +17,8 @@

package org.apache.commons.net.io;

import org.apache.commons.net.util.NetConstants;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
@@ -107,19 +109,19 @@ public void close() throws IOException
public int read() throws IOException {
synchronized (lock) {
if (eof) {
return -1; // Don't allow read past EOF
return NetConstants.EOS; // Don't allow read past EOF
}
int chint = super.read();
if (chint == -1) { // True EOF
if (chint == NetConstants.EOS) { // True EOF
eof = true;
return -1;
return NetConstants.EOS;
}
if (atBeginning) {
atBeginning = false;
if (chint == DOT) { // Have DOT
mark(2); // need to check for CR LF or DOT
chint = super.read();
if (chint == -1) { // Should not happen
if (chint == NetConstants.EOS) { // Should not happen
// new Throwable("Trailing DOT").printStackTrace();
eof = true;
return DOT; // return the trailing DOT
@@ -130,7 +132,7 @@ public int read() throws IOException {
}
if (chint == CR) { // Have DOT CR
chint = super.read();
if (chint == -1) { // Still only DOT CR - should not happen
if (chint == NetConstants.EOS) { // Still only DOT CR - should not happen
//new Throwable("Trailing DOT CR").printStackTrace();
reset(); // So CR is picked up next time
return DOT; // return the trailing DOT
@@ -139,7 +141,7 @@ public int read() throws IOException {
atBeginning = true;
eof = true;
// Do we need to clear the mark somehow?
return -1;
return NetConstants.EOS;
}
}
// Should not happen - lone DOT at beginning
@@ -205,7 +207,7 @@ public int read(final char[] buffer, int offset, int length) throws IOException
{
if ((ch = read()) == -1)
{
return -1;
return NetConstants.EOS;
}

final int off = offset;
@@ -231,7 +233,7 @@ public String readLine() throws IOException {
final StringBuilder sb = new StringBuilder();
int intch;
synchronized(lock) { // make thread-safe (hopefully!)
while((intch = read()) != -1)
while((intch = read()) != NetConstants.EOS)
{
if (intch == LF && atBeginning) {
return sb.substring(0, sb.length()-1);
@@ -17,6 +17,8 @@

package org.apache.commons.net.io;

import org.apache.commons.net.util.NetConstants;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
@@ -171,7 +173,7 @@ public int read(final byte buffer[], int offset, final int length) throws IOExce


if ((ch = readInt()) == -1) {
return -1;
return NetConstants.EOS;
}

off = offset;
@@ -17,6 +17,8 @@

package org.apache.commons.net.io;

import org.apache.commons.net.util.NetConstants;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -169,8 +171,8 @@ public int read(final byte[] buffer, int offset, int length) throws IOException
length = 1;
}

if ((ch = read()) == -1) {
return -1;
if ((ch = read()) == NetConstants.EOS) {
return NetConstants.EOS;
}

off = offset;
@@ -179,7 +181,7 @@ public int read(final byte[] buffer, int offset, int length) throws IOException
{
buffer[offset++] = (byte)ch;
}
while (--length > 0 && (ch = read()) != -1);
while (--length > 0 && (ch = read()) != NetConstants.EOS);

return offset - off;
}
@@ -17,6 +17,8 @@

package org.apache.commons.net.io;

import org.apache.commons.net.util.NetConstants;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
@@ -168,7 +170,7 @@ public static long copyReader(final Reader source, final Writer dest,

try
{
while ((numChars = source.read(buffer)) != -1)
while ((numChars = source.read(buffer)) != NetConstants.EOS)
{
// Technically, some read(char[]) methods may return 0 and we cannot
// accept that as an indication of EOF.
@@ -343,7 +345,7 @@ public static long copyStream(final InputStream source, final OutputStream dest,

try
{
while ((numBytes = source.read(buffer)) != -1)
while ((numBytes = source.read(buffer)) != NetConstants.EOS)
{
// Technically, some read(byte[]) methods may return 0 and we cannot
// accept that as an indication of EOF.
@@ -40,6 +40,13 @@ public class NetConstants {
*/
public static final X509Certificate[] EMPTY_X509_CERTIFICATE_ARRAY = new X509Certificate[0];

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

/**
* Prevents instantiation.
*/

0 comments on commit 69d88f0

Please sign in to comment.