checking corner cases

rabidaudio-outputstream-fix
Felipe Herranz 2019-07-07 13:09:12 +02:00
commit 1e68f744c5
2 zmienionych plików z 50 dodań i 0 usunięć

Wyświetl plik

@ -54,6 +54,31 @@ public class SerialInputStream extends InputStream
return device.syncRead(b, timeout);
}
@Override
public int read(byte b[], int off, int len)
{
if(off < 0 ){
throw new IndexOutOfBoundsException("Offset must be >= 0");
}
if(len < 0){
throw new IndexOutOfBoundsException("Length must positive");
}
if(len > b.length - off) {
throw new IndexOutOfBoundsException("Length greater than b.length - off");
}
if (off == 0 && len == b.length) {
return read(b);
}
byte[] slice = new byte[len];
int ret = device.syncRead(slice, timeout);
System.arraycopy(slice, 0, b, off, ret);
return ret;
}
@Override
public int available() throws IOException {
if(bufferSize > 0)

Wyświetl plik

@ -25,6 +25,31 @@ public class SerialOutputStream extends OutputStream
device.syncWrite(b, timeout);
}
@Override
public void write(byte b[], int off, int len)
{
if(off < 0 ){
throw new IndexOutOfBoundsException("Offset must be >= 0");
}
if(len < 0){
throw new IndexOutOfBoundsException("Length must positive");
}
if(off + len > b.length) {
throw new IndexOutOfBoundsException("off + length greater than buffer length");
}
if (off == 0 && len == b.length) {
write(b);
return;
}
byte[] slice = new byte[len];
System.arraycopy(b, off, slice, 0, len);
device.syncWrite(slice, timeout);
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}