kopia lustrzana https://github.com/felHR85/UsbSerial
checking corner cases
commit
1e68f744c5
|
@ -54,6 +54,31 @@ public class SerialInputStream extends InputStream
|
||||||
return device.syncRead(b, timeout);
|
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
|
@Override
|
||||||
public int available() throws IOException {
|
public int available() throws IOException {
|
||||||
if(bufferSize > 0)
|
if(bufferSize > 0)
|
||||||
|
|
|
@ -25,6 +25,31 @@ public class SerialOutputStream extends OutputStream
|
||||||
device.syncWrite(b, timeout);
|
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) {
|
public void setTimeout(int timeout) {
|
||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue