Received Byte array not always correct - java

I have an Arduino program that sends via Bluetooth a byte array to a Java program.
The Java program receives the data on JSSC. The array is 72 bytes long (18 floats a´ 4byte).
How JSSC recognizes the end of transmission, or know that a new byte array becomes available?
In most cases, the transfer is correct. But from time to time waste is received.
I've found this example here.
http://www.javaprogrammingforums.com/java-se-api-tutorials/5603-jssc-library-easy-work-serial-ports.html
This
if(event.getEventValue() == 10)
means an linefeed, but bytearray have no linefeeds.
Arduino:
Serial.write(bytearray, size of bytearray);
Java:
class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
int dataNumber = 0;
try {
if(event.isRXCHAR()){
if(event.getEventValue() >= BYTE_NUMBER){
receivedByte = usbPort.readBytes(BYTE_NUMBER);
if(receivedByte != null) {
isRequestOK = true;
byte[] myArray = new byte[FLOATSIZE];
for(int i = 0, y = 0; i < receivedByte.length; i++, y++) { //i < receivedByte.length-1
if(myArray == null) {
y = 0;
myArray = new byte[FLOATSIZE];
}
if((i + 1) % FLOATSIZE != 0) {
myArray[y] = receivedByte[i];
}
else {
myArray[y] = receivedByte[i];
receivedValue[dataNumber] = ByteBuffer.wrap(myArray).order(ByteOrder.LITTLE_ENDIAN).getFloat();
myArray = null;
dataNumber++;
}
}
}
}
}
} catch (SerialPortException e) { } ///InvocationTargetException oder NullPointerException
}//--------------------------- End of serialEvent -------------------------------------------------------------
}//--------------------------- End of SerialPortReader ------------------------------------------------------------
Can anybody help me?
Best regards Willi
P.S. This is my first question in this forum. Hopefully I have followed the rules.

Related

In Java, how do I efficiently trim 0's from the start and the end of a byte array

For reasons outside my control, I need to parse a huge file that has an universe of empty bytes at the beginning and the end of the file, and a very small portion that is actually valid (5 KBs at most). This is the code I came up with:
#NonNull
public static byte[] readFileToByteArray(#NonNull File file, boolean bTrimNulls) throws IOException {
byte[] buffer = new byte[(int) file.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
if (fis.read(buffer) == -1) {
throw new IOException("EOF reached while trying to read the whole file");
}
} finally {
closeSafely(fis);
}
if (!bTrimNulls) {
return buffer;
}
int nFirstValidByteIndex = 0;
for (int i = 0; i < buffer.length; i++) {
if (buffer[i] != 0) {
nFirstValidByteIndex = i;
break;
}
}
int nLastValidByteIndex = 0;
for (int i = buffer.length - 1; i > 0; i--) {
if (buffer[i] != 0) {
nLastValidByteIndex = i;
break;
}
}
return copyBufferRange(buffer, nFirstValidByteIndex, nLastValidByteIndex + 1);
}
Is there any better alternative to this?
EDIT: The valid bytes in the buffer correspond to an XML file.
I think your solution is rather efficient. In fact, you are looking from both ends of the array the indexes of the first 1's and then create a subarray of data.
Why do you feel you need to improve your algorithm?
Careful: premature optimization is the root of all evil (or at least most of it) in programming, quote by Donald Knuth
Your code has a time complexity of n, which can be too much for large files as you said. Fortunately we know that the non-zero part has a maximum size m, so we can search the file in steps of m. If we miss (hit a zero in the middle of the payload), we need to repeat it until we found it. So the complexity goes to around n/m if the probability of a zero in the payload is sufficiently low.
import java.util.Arrays;
import java.util.Random;
class Test
{
public static int findNonZero(byte[] sparse, int max)
{
// looks quadratic but isn't in practice if the probability of zero in the payload is low, i.e. 1/256 for random values
for(int offset=0;offset<max;offset++)
{
for(int i=0;(i+offset)<sparse.length; i+=max)
{
if(sparse[i+offset]!=0)
{
return i+offset;
}
}
}
// in production code you could handle this differently but this is just an example
throw new RuntimeException("Nonzero value not found");
}
public static byte[] trim(byte[] sparse, int max)
{
int index = findNonZero(sparse, max);
// go to the left and go to the right until you find (max) zeroes
int from = ...
int to = ...
return Arrays.copyOfRange(sparse, from, to);
}
public static void main(String[] args)
{
// create test data
int size = 5000;
byte[] test = new byte[1_000_000_000];
byte[] payload = new byte[size];
Random r = new Random();
r.nextBytes(payload);
payload[0]=(byte)(r.nextInt(Byte.MAX_VALUE-1)+1); // ensure start isnt zero
payload[payload.length-1]=(byte)(r.nextInt(Byte.MAX_VALUE-1)+1); // ensure end isnt zero
System.arraycopy(payload, 0, test, r.nextInt(test.length-size), size);
System.out.println(Arrays.equals(payload,trim(test,size)));
}
}
I left the last part for you, where you need to go to the left and go to the right until you find (max) zeroes and determine from and to indices.
You could further improve the real-world performance by setting subsequent offsets further apart, for example offset_1 = 0, offset_2 = max/2, offset_3 = 1/4 max, offset_4 = 3/4 max and so on.
The code is fine. For really large files one would use a limited buffer, a FileChannel,
a SeekableByteChannel with a ByteBuffer.
Just the code could be a bit nicer. A parameter Path instead of File would be more general and more modern.
public static byte[] readFileToByteArray(#NonNull File file, boolean trimNulls)
throws IOException {
Path path = file.toPath();
byte[] content = Files.readAllBytes(path);
if (trimNulls) {
int start = 0;
while (start < content.length && content[start] == 0) {
++start;
}
int end = content.length;
while (end > start && content[end - 1] == 0) {
--end;
}
content = Arrays.copyOfRange(content, start, end);
}
return content;
}

Integer.toBinaryString() Loses Leading 0's [duplicate]

This question already has answers here:
How to get 0-padded binary representation of an integer in java?
(17 answers)
Closed 5 years ago.
I'm currently working with a Huffman Tree to compress/decompress text files. Currently my problem is that when writing bytes and reading them, I lose any leading 0's in my numbers.
In my OutputStream class, my writeBit()method, I am fed one bit at a time and when my count of bits reaches 8, I write the byte to the file. Currently using a String to build this binary number, although the problem occurs when actually writing the bit.
HuffmanOutputStream.java:
/**
* Created by Sully on 3/20/2017.
*/
import java.io.IOException;
public class HuffmanOutputStream extends BitOutputStream {
private int count = 0;
private String bytes = "";
public HuffmanOutputStream(String filename, String tree, int totalChars) {
super(filename);
try {
d.writeUTF(tree);
d.writeInt(totalChars);
} catch (IOException e) {
}
}
public void writeBit(int bit) {
//PRE bit == 0 || bit == 1
if (count < 8) {
bytes += bit;
count++;
}
try {
if (count == 8) {
d.writeByte(Integer.parseInt(bytes, 2));
count = 0;
bytes = "";
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
}
}
An example of when things go wrong, for my text file, the first byte that I construct is 01100001, although when I use Integer.parseInt(byte,2), the integer given is 97, which when it is then read as a binary number, only returns 1100001. As Huffman Trees rely on these 0's being included, how can I keep this 0 in place? Also to make sure that it is read correctly with the 0's remaining in place?
HuffmanInputStream.java:
/**
* Created by Sully on 3/20/2017.
*/
import java.io.IOException;
public class HuffmanInputStream extends BitInputStream {
private String tree;
private int totalChars;
private int currentByte;
private int bitCount;
private static final int BYTE_SIZE = 8;
private int[] bufferedBits = new int[BYTE_SIZE];
public HuffmanInputStream(String filename) {
super(filename);
try {
tree = d.readUTF();
totalChars = d.readInt();
currentByte = 0;
bitCount = 8;
} catch (IOException e) {
}
}
public int readBit() {
if (currentByte == -1) {
return -1;
}
if (bitCount == 8) {
try {
currentByte = d.read();
if(currentByte == -1){
return -1;
}
String binary = Integer.toBinaryString(currentByte);
for (int x = 0; x < binary.length(); x++) {
bufferedBits[x] = Character.valueOf(binary.charAt(x));
}
bitCount = 0;
} catch (IOException e) {
e.printStackTrace();
}
}
int val = bufferedBits[bitCount];
bitCount++;
return val % 2;
}
public String getTree() {
return tree;
}
public int totalChars() {
return totalChars;
}
public void close() {
try {
d.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I know it's a bit lengthy of a question but any help is greatly appreciated!
I assume that you're looking to have enough leading 0s to make the length of the String that is returned from Integer#toBinaryString 8; the following code will achieve this for you:
String binary = String.format("%8s", Integer.toBinaryString(currentByte)).replace(' ', '0');

calling a method(constructor) from main & file format

I have a constructor ID3 and I need to start by executing it from the main. Is it possible?
I tried doing this:
public class ID3
{
public static void main(String[] args) throws Exception
{
System.out.print("\f"); //clears the screen
ID3 instance = new ID3("data.txt", 5 , 14 , "", 5);
instance.ID3("data.txt", 3 , 5 , " ", 2); //error given here since this line had to be removed
}
public ID3(String fName, int numAttributes, int testCases, String delimiter, int limitSplits) throws IOException, FileNotFoundException
{
fileName = fName;
n = numAttributes;
t = testCases;
numSplits = limitSplits;
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
//Parse the first line to see if continuous or discrete attributes.
firstLine = new String[n];
firstLine = in.readLine().split(delimiter);
int i, j, lineCount = 0;
for(i=0; i<n; i++)
unusedAttr.add(new Integer(i));
input = new String[t][n+1];
String line;
int invalidLines = 0;
while((lineCount + invalidLines)<t)
{
try
{
input[lineCount] = (in.readLine()).split(delimiter);
}
catch(NullPointerException e)
{
invalidLines++;continue;
}
if (Array.getLength(input[lineCount]) != n+1 || (Array.get(input[lineCount],n).toString().compareTo("?") == 0)) //number of attributes provided in the line is incorrect.
{
invalidLines++;continue;
}
lineCount++;
}
if(invalidLines == t)
{
System.out.println("All lines invalid - Check the supplied attribute number");
System.exit(0);
}
if (invalidLines > 0)
System.out.println("Not Considering "+invalidLines+" invalid training cases");
if(numSplits > maxSplits || numSplits > (t/2))
{
System.out.println("numSplits should be less than or equal to "+Math.min(t/2,limitSplits));
System.exit(1);
}
t = testCases - invalidLines;
thresholdVal = new String[n][numSplits - 1];
boolean allCont = false;
if(Array.getLength(firstLine) == 1)
{
if(firstLine[0].compareTo("c") == 0)
allCont = true;
else if(firstLine[0].compareTo("d") == 0)
return;
else
{
System.out.println("Invalid first line - it should be c or d");
System.exit(1);
}
}
for(i=0; i<n; i++)
{
if(allCont || firstLine[i].compareTo("c") == 0) //Continuous Attribute
{
for(j=0; j<numSplits-1; j++)
thresholdVal[i][j] = calculateThreshold(i,j);
}
else if(firstLine[i].compareTo("d") != 0)
{
System.out.println("Invalid first line - Training data (it should specify if the attributes are c or d)");
System.exit(1);
}
}
for(i=0; i<t; i++)
{
for(j=0; j<n; j++)
{
if(allCont || firstLine[j].compareTo("c") == 0)
input[i][j] = makeContinuous(input[i][j], j);
}
}
}
The code for the constructor is shown above, however it finds the file but doesn't process the data and prints the errors out. How should the file be exactly?
Used text file has:
d
Span Shape Slab
long square waffle
long rectangle waffle
short square two-way
short rectangle one-way
You are already calling the constructor here - ID3 instance = new ID3("data.txt", 5 , 14 , "", 5);. You can't call it as a regular method. Just remove the instance.ID3("data.txt", 5 , 14 , "", 5); line.
You cannot call constructors like regular methods. The constructor is automatically called when you create an instance of a class,i.e,when you do
ID3 instance = new ID3("data.txt", 5 , 14 , "", 5);
Contructors are not methods. One of the key feature of a method is that it should have a return type (event if it is 'void').
Here, you do not need to explicitly call the constructor again. The functionality you implement in the constructor will be executed at instantiation itself. However, this is not recommended and is bug-prone. You should only be instantiating any variables. The actual functionality should be defined in another method.

I am trying to solve '15 puzzle', but I get 'OutOfMemoryError' [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
Is there a way that I can optimize this code as to not run out of memory?
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Stack;
public class TilePuzzle {
private final static byte ROWS = 4;
private final static byte COLUMNS = 4;
private static String SOLUTION = "123456789ABCDEF0";
private static byte RADIX = 16;
private char[][] board = new char[ROWS][COLUMNS];
private byte x; // Row of the space ('0')
private byte y; // Column of the space ('0') private String representation;
private boolean change = false; // Has the board changed after the last call to toString?
private TilePuzzle() {
this(SOLUTION);
int times = 1000;
Random rnd = new Random();
while(times-- > 0) {
try {
move((byte)rnd.nextInt(4));
}
catch(RuntimeException e) {
}
}
this.representation = asString();
}
public TilePuzzle(String representation) {
this.representation = representation;
final byte SIZE = (byte)SOLUTION.length();
if (representation.length() != SIZE) {
throw new IllegalArgumentException("The board must have " + SIZE + "numbers.");
}
boolean[] used = new boolean[SIZE];
byte idx = 0;
for (byte i = 0; i < ROWS; ++i) {
for (byte j = 0; j < COLUMNS; ++j) {
char digit = representation.charAt(idx++);
byte number = (byte)Character.digit(digit, RADIX);
if (number < 0 || number >= SIZE) {
throw new IllegalArgumentException("The character " + digit + " is not valid.");
} else if(used[number]) {
throw new IllegalArgumentException("The character " + digit + " is repeated.");
}
used[number] = true;
board[i][j] = digit;
if (digit == '0') {
x = i;
y = j;
}
}
}
}
/**
* Swap position of the space ('0') with the number that's up to it.
*/
public void moveUp() {
try {
move((byte)(x - 1), y);
} catch(IllegalArgumentException e) {
throw new RuntimeException("Move prohibited " + e.getMessage());
}
}
/**
* Swap position of the space ('0') with the number that's down to it.
*/
public void moveDown() {
try {
move((byte)(x + 1), y);
} catch(IllegalArgumentException e) {
throw new RuntimeException("Move prohibited " + e.getMessage());
}
}
/**
* Swap position of the space ('0') with the number that's left to it.
*/
public void moveLeft() {
try {
move(x, (byte)(y - 1));
} catch(IllegalArgumentException e) {
throw new RuntimeException("Move prohibited " + e.getMessage());
}
}
/**
* Swap position of the space ('0') with the number that's right to it.
*/
public void moveRight() {
try {
move(x, (byte)(y + 1));
} catch(IllegalArgumentException e) {
throw new RuntimeException("Move prohibited " + e.getMessage());
}
}
private void move(byte movement) {
switch(movement) {
case 0: moveUp(); break;
case 1: moveRight(); break;
case 2: moveDown(); break;
case 3: moveLeft(); break;
}
}
private boolean areValidCoordinates(byte x, byte y) {
return (x >= 0 && x < ROWS && y >= 0 && y < COLUMNS);
}
private void move(byte nx, byte ny) {
if (!areValidCoordinates(nx, ny)) {
throw new IllegalArgumentException("(" + nx + ", " + ny + ")");
}
board[x][y] = board[nx][ny];
board[nx][ny] = '0';
x = nx;
y = ny;
change = true;
}
public String printableString() {
StringBuilder sb = new StringBuilder();
for (byte i = 0; i < ROWS; ++i) {
for (byte j = 0; j < COLUMNS; ++j) {
sb.append(board[i][j] + " ");
}
sb.append("\r\n");
}
return sb.toString();
}
private String asString() {
StringBuilder sb = new StringBuilder();
for (byte i = 0; i < ROWS; ++i) {
for (byte j = 0; j < COLUMNS; ++j) {
sb.append(board[i][j]);
}
}
return sb.toString();
}
public String toString() {
if (change) {
representation = asString();
}
return representation;
}
private static byte[] whereShouldItBe(char digit) {
byte idx = (byte)SOLUTION.indexOf(digit);
return new byte[] { (byte)(idx / ROWS), (byte)(idx % ROWS) };
}
private static byte manhattanDistance(byte x, byte y, byte x2, byte y2) {
byte dx = (byte)Math.abs(x - x2);
byte dy = (byte)Math.abs(y - y2);
return (byte)(dx + dy);
}
private byte heuristic() {
byte total = 0;
for (byte i = 0; i < ROWS; ++i) {
for (byte j = 0; j < COLUMNS; ++j) {
char digit = board[i][j];
byte[] coordenates = whereShouldItBe(digit);
byte distance = manhattanDistance(i, j, coordenates[0], coordenates[1]);
total += distance;
}
}
return total;
}
private class Node implements Comparable<Node> {
private String puzzle;
private byte moves; // Number of moves from original configuration
private byte value; // The value of the heuristic for this configuration.
public Node(String puzzle, byte moves, byte value) {
this.puzzle = puzzle;
this.moves = moves;
this.value = value;
}
#Override
public int compareTo(Node o) {
return (value + moves) - (o.value + o.moves);
}
}
private void print(Map<String, String> antecessor) {
Stack toPrint = new Stack();
toPrint.add(SOLUTION);
String before = antecessor.get(SOLUTION);
while (!before.equals("")) {
toPrint.add(before);
before = antecessor.get(before);
}
while (!toPrint.isEmpty()) {
System.out.println(new TilePuzzle(toPrint.pop()).printableString());
}
}
private byte solve() {
if(toString().equals(SOLUTION)) {
return 0;
}
PriorityQueue<Node> toProcess = new PriorityQueue();
Node initial = new Node(toString(), (byte)0, heuristic());
toProcess.add(initial);
Map<String, String> antecessor = new HashMap<String, String>();
antecessor.put(toString(), "");
while(!toProcess.isEmpty()) {
Node actual = toProcess.poll();
for (byte i = 0; i < 4; ++i) {
TilePuzzle t = new TilePuzzle(actual.puzzle);
try {
t.move(i);
} catch(RuntimeException e) {
continue;
}
if (t.toString().equals(SOLUTION)) {
antecessor.put(SOLUTION, actual.puzzle);
print(antecessor);
return (byte)(actual.moves + 1);
} else if (!antecessor.containsKey(t.toString())) {
byte v = t.heuristic();
Node neighbor = new Node(t.toString(), (byte)(actual.moves + 1), v);
toProcess.add(neighbor);
antecessor.put(t.toString(), actual.puzzle);
}
}
}
return -1;
}
public static void main(String... args) {
TilePuzzle puzzle = new TilePuzzle();
System.out.println(puzzle.solve());
}
}
The problem
The root cause is the tons of String objects you are creating and storing in the toProcess Queue and the antecessor Map. Why are you doing that?
Look at your algorithm. See if you really need to store >2 million nodes and 5 million strings in each.
The investigation
This was hard to spot because the program is complex. Actually, I didn't even try to understand all of the code. Instead, I used VisualVM – a Java profiler, sampler, and CPU/memory usage monitor.
I launched it:
And took a look at the memory usage. The first thing I noticed was the (obvious) fact that you're creating tons of objects.
This is an screenshot of the app:
As you can see, the amount of memory used is tremendous. In as few as 40 seconds, 2 GB were consumed and the entire heap was filled.
A dead end
I initially thought the problem had something to do with the Node class, because even though it implements Comparable, it doesn't implement equals. So I provided the method:
public boolean equals( Object o ) {
if( o instanceof Node ) {
Node other = ( Node ) o;
return this.value == other.value && this.moves == other.moves;
}
return false;
}
But that was not the problem.
The actual problem turned out to be the one stated at the top.
The workaround
As previously stated, the real solution is to rethink your algorithm. Whatever else can be done, in the meantime, will only delay the problem.
But workarounds can be useful. One is to reuse the strings you're generating. You're very intensively using the TilePuzzle.toString() method; this ends up creating duplicate strings quite often.
Since you're generating string permutations, you may create many 12345ABCD strings in matter of seconds. If they are the same string, there is no point in creating millions of instances with the same value.
The String.intern() method allows strings to be reused. The doc says:
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals() method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
For a regular application, using String.intern() could be a bad idea because it doesn't let instances be reclaimed by the GC. But in this case, since you're holding the references in your Map and Queue anyway, it makes sense.
So making this change:
public String toString() {
if (change) {
representation = asString();
}
return representation.intern(); // <-- Use intern
}
Pretty much solves the memory problem.
This is a screenshot after the change:
Now, the heap usage doesn't reach 100 MB even after a couple of minutes.
Extra remarks
Remark #1
You're using an exception to validate if the movement is valid or not, which is okay; but when you catch them, you're just ignoring them:
try {
t.move(i);
} catch(RuntimeException e) {
continue;
}
If you're not using them anyway, you can save a lot of computation by not creating the exceptions in the first place. Otherwise you're creating millions of unused exceptions.
Make this change:
if (!areValidCoordinates(nx, ny)) {
// REMOVE THIS LINE:
// throw new IllegalArgumentException("(" + nx + ", " + ny + ")");
// ADD THIS LINE:
return;
}
And use validation instead:
// REMOVE THESE LINES:
// try {
// t.move(i);
// } catch(RuntimeException e) {
// continue;
// }
// ADD THESE LINES:
if(t.isValidMovement(i)){
t.move(i);
} else {
continue;
}
Remark #2
You're creating a new Random object for every new TilePuzzle instance. It would be better if you used just one for the whole program. After all, you are only using a single thread.
Remark #3
The workaround solved the heap memory problem, but created another one involving PermGen. I simply increased the PermGen size, like this:
java -Xmx1g -Xms1g -XX:MaxPermSize=1g TilePuzzle
Remark #4
The output was sometimes 49 and sometimes 50. The matrices were printed like:
1 2 3 4
5 6 7 8
9 A B C
D E 0 F
1 2 3 4
5 6 7 8
9 A B C
D E F 0
... 50 times

Wrapping BSD select() with JNA

I need to wrap a BSD-like C socket API to Java with JNA. It has basically the same functions as standard BSD socket API.
Wrapping select() is problematic because of the fd_set-structure required in its arguments and the FD_* masking functions (macros) that are needed to handle fd_sets. I tried to crawl through the header files (e.g. sys/select.h in Ubuntu 8.04) but the definitions are not so straightforward. Especially I found it difficult to find the implementation of FD_*-macros, which is needed when wrapping them with JNA's InvocationMapper.
Note: I'm not trying to wrap the standard TCP or unix-socket API, but a custom one. Thus built-in sockets in Java do not fit the bill.
Especially I found it difficult to find the implementation of FD_*-macros, which is needed when wrapping them with JNA's InvocationMapper.
The C pre-processor cpp is useful to find out how macros are expanded. Write a dummy program that uses the relevant macros (it should be lexically correct, but needn't compile), run it through cpp and watch what happens.
I use a byte array for the fd_set structure and some arithmetic to find the right byte position within the array:
private static final int FD_SETSIZE = 1024;
private static final boolean isBigEndian = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
private static interface Libc extends Library {
int select (int nfds, byte[] readfds, byte[] writefds, byte[] errfds, TimeVal timeout);
//...
}
private static class FdSet {
byte[] a;
FdSet() {
a = new byte[FD_SETSIZE / 8]; }
void set (int fd) {
a[getBytePos(fd)] |= getBitMask(fd); }
boolean isSet (int fd) {
return (a[getBytePos(fd)] & getBitMask(fd)) != 0; }
private static int getBytePos (int fd) {
if (fd < 0 || fd >= LibcDefs.FD_SETSIZE) {
throw new RuntimeException("File handle out of range for fd_set."); }
if (isBigEndian) {
return (fd / 8 / Native.LONG_SIZE + 1) * Native.LONG_SIZE - 1 -
fd / 8 % Native.LONG_SIZE; }
else {
return fd / 8; }}
private static int getBitMask (int fd) {
return 1 << (fd % 8); }}
private static class TimeVal extends Structure {
public NativeLong tv_sec;
public NativeLong tv_usec;
TimeVal (int ms) {
set(ms); }
void set (int ms) {
tv_sec.setValue(ms / 1000);
tv_usec.setValue(ms % 1000 * 1000); }
#Override protected List<?> getFieldOrder() {
return Arrays.asList("tv_sec", "tv_usec"); }}
public boolean waitInputReady (int timeoutMs) throws IOException {
TimeVal timeVal = (timeoutMs < 0) ? null : new TimeVal(timeoutMs);
FdSet rxSet = new FdSet();
FdSet errorSet = new FdSet();
rxSet.set(fileHandle);
errorSet.set(fileHandle);
int rc = libc.select(fileHandle + 1, rxSet.a, null, errorSet.a, timeVal);
checkSelectErrors(rc, errorSet);
if (rc == 0) {
return false; }
if (!rxSet.isSet(fileHandle)) {
throw new RuntimeException("rxSet bit is not set after select()."); }
return true; }
public boolean waitOutputReady (int timeoutMs) throws IOException {
TimeVal timeVal = (timeoutMs < 0) ? null : new TimeVal(timeoutMs);
FdSet txSet = new FdSet();
FdSet errorSet = new FdSet();
txSet.set(fileHandle);
errorSet.set(fileHandle);
int rc = libc.select(fileHandle + 1, null, txSet.a, errorSet.a, timeVal);
checkSelectErrors(rc, errorSet);
if (rc == 0) {
return false; }
if (!txSet.isSet(fileHandle)) {
throw new RuntimeException("txSet bit is not set after select()."); }
return true; }
private void checkSelectErrors (int rc, FdSet errorSet) throws IOException {
if (rc == -1) {
throw new IOException("Error in select(), errno=" + Native.getLastError() + "."); }
boolean error = errorSet.isSet(fileHandle);
if (!(rc == 0 && !error || rc == 1 || rc == 2 && error)) {
throw new RuntimeException("Invalid return code received from select(), rc=" + rc + ", error=" + error + "."); }
if (error) {
throw new IOException("Channel error state detected"); }}

Categories