NSP: read/write(byte[]...) funktionen hinzugefügt

thobaben_serialize
Harald Wolff 2016-12-14 10:53:38 +01:00
parent 28bbe1bc5d
commit 85551c9000
1 changed files with 43 additions and 0 deletions

View File

@ -253,6 +253,11 @@ public class NewSerialPort {
private static native int nsp_read(long nsp);
private static native int nsp_write(long nsp,int ch);
private static native int nsp_read_bytes(long nsp,byte[] bytes,int offset,int len);
private static native int nsp_write_bytes(long nsp,byte[] bytes,int offset,int len);
static {
NativeLoader.loadLibrary("nsp");
@ -284,6 +289,27 @@ public class NewSerialPort {
};
return ch;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int nRead = nsp_read_bytes(nsp, b, off, len);
if (nRead < 0){
if (nRead == -11){ // -EAGAIN (timeout, keine zeichen verfügbar)
return 0;
};
throw new IOException(String.format("nsp_read_bytes() returned %d", nRead));
}
return nRead;
}
@Override
public int read(byte[] b) throws IOException {
int nRead = nsp_read_bytes(nsp, b, 0, b.length);
if (nRead < 0){
throw new IOException(String.format("nsp_read_bytes() returned %d", nRead));
}
return nRead;
}
}
public class NewSerialPortOutputStream extends OutputStream
@ -321,6 +347,23 @@ public class NewSerialPort {
}
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
int nWritten = nsp_write_bytes(nsp, b, off, len);
if (nWritten != len){
throw new IOException(String.format("nsp_write_bytes() returned %d", nWritten));
}
}
@Override
public void write(byte[] b) throws IOException {
int nWritten = nsp_write_bytes(nsp, b, 0, b.length);
if (nWritten != b.length){
throw new IOException(String.format("nsp_write_bytes() returned %d", nWritten));
}
}
}
static public String[] getPortNames(){