|
Sea 0.4.0 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object gov.lbl.dsd.sea.nio.util.COBSCodec
Encoder/Decoder implementing Consistent Overhead Byte Stuffing (COBS) for efficient, reliable, unambigous packet framing regardless of packet content, making it is easy for applications to recover from malformed packet payloads.
For details, see the paper . In case the link is broken, get it from the paper's author .
Quoting from the paper: "When packet data is sent over any serial medium, a protocol is needed by which to demarcate packet boundaries. This is done by using a special bit-sequence or character value to indicate where the boundaries between packets fall. Data stuffing is the process that transforms the packet data before transmission to eliminate any accidental occurrences of that special framing marker, so that when the receiver detects the marker, it knows, without any ambiguity, that it does indeed indicate a boundary between packets.
COBS takes an input consisting of bytes in the range [0,255] and produces an output consisting of bytes only in the range [1,255]. Having eliminated all zero bytes from the data, a zero byte can now be used unambiguously to mark boundaries between packets.
This allows the receiver to synchronize reliably with the beginning of the next packet, even after an error. It also allows new listeners to join a broadcast stream at any time and without failing to receive and decode the very next error free packet.
With COBS all packets up to 254 bytes in length are encoded with an overhead of exactly one byte. For packets over 254 bytes in length the overhead is at most one byte for every 254 bytes of packet data. The maximum overhead is therefore roughly 0.4% of the packet size, rounded up to a whole number of bytes. COBS encoding has low overhead (on average 0.23% of the packet size, rounded up to a whole number of bytes) and furthermore, for packets of any given length, the amount of overhead is virtually constant, regardless of the packet contents."
This class implements the original COBS algorithm, not the COBS/ZPE variant.
There holds: decode(encode(src)) = src
.
Performance Note: The JDK 1.5 server VM runs decode(encode(src))
at about 125 MB/s throughput on a commodity PC (2 GHz Pentium 4). Encoding is
the bottleneck, decoding is extremely cheap. Obviously, this is way more
efficient than Base64 encoding or similar application level byte stuffing
mechanisms.
Method Summary | |
static byte[] |
decode(byte[] src)
Returns the decoded representation of the given bytes. |
static void |
decode(byte[] src,
int from,
int to,
ArrayByteList dest)
Adds (appends) the decoded representation of the range src[from..to)
to the given destination list. |
static byte[] |
encode(byte[] src)
Returns the encoded representation of the given bytes. |
static void |
encode(byte[] src,
int from,
int to,
ArrayByteList dest)
Adds (appends) the encoded representation of the range src[from..to)
to the given destination list. |
static int |
maxEncodedSize(int size)
Returns the maximum amount of bytes an ecoding of size bytes takes in the worst case. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
public static byte[] encode(byte[] src)
src
- the bytes to encode
public static void encode(byte[] src, int from, int to, ArrayByteList dest)
src[from..to)
to the given destination list.
src
- the bytes to encodefrom
- the first byte to encode (inclusive)to
- the last byte to encode (exclusive)dest
- the destination list to append topublic static int maxEncodedSize(int size)
size
bytes takes in the worst case.
public static byte[] decode(byte[] src) throws IOException
src
- the bytes to decode
IOException
public static void decode(byte[] src, int from, int to, ArrayByteList dest) throws IOException
src[from..to)
to the given destination list.
src
- the bytes to decodefrom
- the first byte to decode (inclusive)to
- the last byte to decode (exclusive)dest
- the destination list to append to
IOException
- if src data is corrupt (encoded erroneously)
|
Sea 0.4.0 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |