From 85551c90008a8be640533416356f1c2e909f92e5 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Wed, 14 Dec 2016 10:53:38 +0100 Subject: [PATCH] =?UTF-8?q?NSP:=20read/write(byte[]...)=20funktionen=20hin?= =?UTF-8?q?zugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hwo/io/NewSerialPort/NewSerialPort.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/org/hwo/io/NewSerialPort/NewSerialPort.java b/src/org/hwo/io/NewSerialPort/NewSerialPort.java index ac2d5c4..abeebf6 100644 --- a/src/org/hwo/io/NewSerialPort/NewSerialPort.java +++ b/src/org/hwo/io/NewSerialPort/NewSerialPort.java @@ -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(){