Merge pull request #69 from kevinherron/iostream

Improvements to SerialInputStream and SerialOutputStream
This commit is contained in:
Will Hedgecock 2018-01-03 10:56:40 -06:00 committed by GitHub
commit 6bd2a9995f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 76 additions and 20 deletions

View File

@ -27,12 +27,12 @@ package com.fazecast.jSerialComm;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Date;
@ -1037,6 +1037,11 @@ public final class SerialPort
// InputStream interface class
private final class SerialPortInputStream extends InputStream
{
// a shared, re-usable, single-byte scratch buffer to avoid allocating
// a new byte[] when reading a single byte. not thread safe, but neither
// SerialPort nor its Input/Output streams are to begin with.
private final byte[] singleByteBuffer = new byte[1];
public SerialPortInputStream() {}
@Override
@ -1051,23 +1056,30 @@ public final class SerialPort
@Override
public final int read() throws IOException
{
byte[] buffer = new byte[1];
int bytesRead;
while (isOpened)
{
bytesRead = readBytes(portHandle, buffer, 1l);
bytesRead = readBytes(portHandle, singleByteBuffer, 1L);
if (bytesRead > 0)
return ((int)buffer[0] & 0x000000FF);
try { Thread.sleep(1); } catch (Exception e) {}
return ((int) singleByteBuffer[0] & 0xFF);
sleep1();
}
throw new IOException("This port appears to have been shutdown or disconnected.");
}
@Override
public final int read(byte[] b) throws IOException
{
return read(b, 0, b.length);
if (!isOpened)
throw new IOException("This port appears to have been shutdown or disconnected.");
if (b.length == 0)
return 0;
return readBytes(portHandle, b, b.length);
}
@Override
@ -1100,36 +1112,80 @@ public final class SerialPort
// OutputStream interface class
private final class SerialPortOutputStream extends OutputStream
{
// a shared, re-usable, single-byte scratch buffer to avoid allocating
// a new byte[] when writing a single byte. not thread safe, but neither
// SerialPort nor its Input/Output streams are to begin with.
private final byte[] singleByteBuffer = new byte[1];
public SerialPortOutputStream() {}
@Override
public final void write(int b) throws IOException
{
if (!isOpened)
throw new IOException("This port appears to have been shutdown or disconnected.");
int bytesWritten = 0;
byte[] buffer = new byte[1];
buffer[0] = (byte)(b & 0x000000FF);
if (writeBytes(portHandle, buffer, 1l) < 0)
throw new IOException("This port appears to have been shutdown or disconnected.");
do {
if (!isOpened)
throw new IOException("This port appears to have been shutdown or disconnected.");
singleByteBuffer[0] = (byte) (b & 0xFF);
bytesWritten += writeBytes(portHandle, singleByteBuffer, 1L);
if (bytesWritten < 0)
throw new IOException("This port appears to have been shutdown or disconnected.");
if (bytesWritten == 0) sleep1();
} while (bytesWritten < 1);
}
@Override
public final void write(byte[] b) throws IOException
{
write(b, 0, b.length);
int bytesWritten = 0;
byte[] buffer = b;
do {
if (!isOpened)
throw new IOException("This port appears to have been shutdown or disconnected.");
int written = writeBytes(portHandle, buffer, 1L);
if (written < 0)
throw new IOException("This port appears to have been shutdown or disconnected.");
bytesWritten += written;
if (written == 0)
{
sleep1();
}
else
{
buffer = new byte[b.length - bytesWritten];
System.arraycopy(b, bytesWritten, buffer, 0, buffer.length);
}
} while (bytesWritten < b.length);
}
@Override
public final void write(byte[] b, int off, int len) throws IOException
{
if (!isOpened)
throw new IOException("This port appears to have been shutdown or disconnected.");
byte[] buffer = new byte[len];
System.arraycopy(b, off, buffer, 0, len);
if (writeBytes(portHandle, buffer, len) < 0)
throw new IOException("This port appears to have been shutdown or disconnected.");
if (off == 0 && len == b.length)
{
write(b);
}
else
{
byte[] buffer = new byte[len];
System.arraycopy(b, off, buffer, 0, len);
write(buffer);
}
}
}
private static void sleep1() {
try { Thread.sleep(1); } catch (Exception ignored) {}
}
}