Skip to content

Commit a7b9e08

Browse files
Added additional readHeader with Checking and setting sync and endianness.
1 parent 729baa7 commit a7b9e08

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

src/java/pt/lsts/imc/IMCDefinition.java

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,28 @@ public void readHeader(IMCInputStream iis, IMCMessage header)
452452
deserializeFields(header, iis);
453453
}
454454

455+
/**
456+
* Read a message header from the given IMCInputStream
457+
* This method will create a new Header object checking the
458+
* sync word and endianness (setting endianness in the stream).
459+
*
460+
* @param iis
461+
* Where to read the header from
462+
* @return The read header
463+
* @throws IOException
464+
* In case of any IO error (like end of input)
465+
*/
466+
public Header readHeader(IMCInputStream iis) throws IOException {
467+
Header header = createHeader();
468+
// Let us try to check if the first byte could be the synch number
469+
// This avoids desynchronization if we are reading a continuous stream with errors
470+
long sync = getSyncNumberAndSetEndianness(iis);
471+
// if we are here, we have a valid sync word
472+
header.setValue("sync", syncWord);
473+
deserializeAllFieldsBut(header, iis, "sync");
474+
return header;
475+
}
476+
455477
/**
456478
* Retrieve the next message from the given IMCInputStream
457479
*
@@ -467,26 +489,8 @@ public IMCMessage nextMessage(IMCInputStream input) throws IOException {
467489

468490
// Let us try to check if the first byte could be the synch number
469491
// This avoids desynchronization if we are reading a continuous stream with errors
470-
long syncFirstByte = input.readUnsignedByte();
471-
if (!(syncFirstByte == ((syncWord & 0xFF00) >> 8)
472-
|| syncFirstByte == ((swappedWord & 0xFF00) >> 8))) {
473-
// If we are here probably this is not a synch word
474-
if (input.available() == 0 && syncFirstByte == 0xFF)
475-
return null;
476-
else
477-
throw new IOException("Unrecognized Sync word: "
478-
+ String.format("%02X", syncFirstByte) + "??");
479-
}
480-
481-
long sync = ((syncFirstByte & 0xFF) << 8) + input.readUnsignedByte(); // input.readUnsignedShort();
482-
if (sync == syncWord)
483-
input.setBigEndian(true);
484-
else if (sync == swappedWord)
485-
input.setBigEndian(false);
486-
else
487-
throw new IOException("Unrecognized Sync word: "
488-
+ String.format("%02X", sync));
489-
492+
long sync = getSyncNumberAndSetEndianness(input);
493+
// if we are here, we have a valid sync word
490494
header.setValue("sync", syncWord);
491495

492496
deserializeAllFieldsBut(header, input, "sync");
@@ -507,6 +511,27 @@ else if (sync == swappedWord)
507511
}
508512
}
509513

514+
private long getSyncNumberAndSetEndianness(IMCInputStream input) throws IOException {
515+
long syncFirstByte = input.readUnsignedByte();
516+
if (!(syncFirstByte == ((syncWord & 0xFF00) >> 8)
517+
|| syncFirstByte == ((swappedWord & 0xFF00) >> 8))) {
518+
// If we are here probably this is not a synch word
519+
if (input.available() == 0 && syncFirstByte == 0xFF)
520+
throw new IOException("Unrecognized Sync word: input size left not enough to read the sync.");
521+
else
522+
throw new IOException("Unrecognized Sync word: " + String.format("%02X", syncFirstByte) + "??");
523+
}
524+
525+
long sync = ((syncFirstByte & 0xFF) << 8) + input.readUnsignedByte(); // input.readUnsignedShort();
526+
if (sync == syncWord)
527+
input.setBigEndian(true);
528+
else if (sync == swappedWord)
529+
input.setBigEndian(false);
530+
else
531+
throw new IOException("Unrecognized Sync word: " + String.format("%02X", sync));
532+
return sync;
533+
}
534+
510535
/**
511536
* Retrieve the next message in the given InputStream. This is done by
512537
* converting the InputStream in an IMCInputStream.

0 commit comments

Comments
 (0)