1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Bug 482946: Implement InputStream.available()

Change-Id: Iac15816fd4d68206184549c7b47670f9d3137527
Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@st.com>
This commit is contained in:
Torbjörn SVENSSON 2019-09-03 17:21:29 +02:00
parent 1c4271836b
commit 764e42ef50
5 changed files with 38 additions and 0 deletions

View file

@ -26,6 +26,7 @@
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/ioctl.h>
#else
#define WIN32_LEAN_AND_MEAN
#define UNICODE
@ -251,6 +252,32 @@ JNIEXPORT void JNICALL FUNC(close0)(JNIEnv *env, jobject jobj, jlong handle)
#endif
}
JNIEXPORT jint JNICALL FUNC(available0)(JNIEnv * env, jobject jobj, jlong jhandle)
{
#ifndef __MINGW32__
int result = 0;
if (ioctl(jhandle, FIONREAD, &result ) < 0) {
throwIOException(env, "Error calling ioctl");
return 0;
}
return result;
#else
COMSTAT stat;
DWORD errCode;
#ifdef _WIN64
HANDLE handle = (HANDLE)jhandle;
#else
HANDLE handle = (HANDLE)(unsigned)jhandle;
#endif
if (ClearCommError(handle, &errCode, &stat) == 0) {
throwIOException(env, "Error calling ClearCommError");
return -1;
}
return (int)stat.cbInQue;
#endif
}
JNIEXPORT jint JNICALL FUNC(read1)(JNIEnv * env, jobject jobj, jlong jhandle, jbyteArray bytes, jint offset, jint size)
{
#ifndef __MINGW32__

View file

@ -131,6 +131,15 @@ public class SerialPort {
public void close() throws IOException {
SerialPort.this.close();
}
@Override
public int available() throws IOException {
if (isOpen()) {
return available0(handle);
} else {
return 0;
}
}
};
private OutputStream outputStream = new OutputStream() {
@ -208,6 +217,8 @@ public class SerialPort {
private native int read1(long handle, byte[] b, int off, int len) throws IOException;
private native int available0(long handle) throws IOException;
private native void write0(long handle, int b) throws IOException;
private native void write1(long handle, byte[] b, int off, int len) throws IOException;