Skip to content

Commit

Permalink
Added the ability to drain all input into the void
Browse files Browse the repository at this point in the history
- Made read(), write() and available() public
  • Loading branch information
Ratler committed May 15, 2011
1 parent 93e5a82 commit 5a5a22a
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/main/java/org/unitedid/yhsm/internal/DeviceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import gnu.io.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unitedid.yhsm.utility.Utils;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -59,7 +60,7 @@ public class DeviceHandler {
}
}

protected void write(byte[] data) {
public void write(byte[] data) {
try {
writtenBytes += data.length;
writeStream.write(data);
Expand All @@ -68,7 +69,7 @@ protected void write(byte[] data) {
}
}

protected byte[] read(int readNumBytes) {
public byte[] read(int readNumBytes) {
byte[] data = new byte[readNumBytes];
try {
readBytes += readStream.read(data, 0, readNumBytes);
Expand All @@ -79,7 +80,7 @@ protected byte[] read(int readNumBytes) {
return data;
}

protected int available() {
public int available() {
try {
return readStream.available();
} catch (IOException e) {
Expand All @@ -88,6 +89,25 @@ protected int available() {
return 0;
}

public boolean drain() {
try {
byte[] buffer = new byte[0];
while(readStream.available() > 0) {
byte[] b = read(1);
if ((char)b[0] == '\r') {
log.info("Drained: {}", new String(buffer, 0, buffer.length)); //TODO: Do we really need to log this? If not the loop can be simplified.
buffer = new byte[0];
} else {
buffer = Utils.concatAllArrays(buffer, b);
}
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}

return true;
}

public void flush() throws IOException {
writeStream.flush();
}
Expand Down

0 comments on commit 5a5a22a

Please sign in to comment.