https://github.com/bugadani/serialparser
A simple library to parse a stream of bytes
https://github.com/bugadani/serialparser
Last synced: about 1 month ago
JSON representation
A simple library to parse a stream of bytes
- Host: GitHub
- URL: https://github.com/bugadani/serialparser
- Owner: bugadani
- Created: 2015-11-02T15:15:01.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-05-13T07:50:30.000Z (about 10 years ago)
- Last Synced: 2025-03-02T15:28:29.014Z (over 1 year ago)
- Language: Java
- Size: 23.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
SerialParser
========
SerialParser is a small library that can be used to detect specific data frames in a byte array, e.g. while reading a
stream.
Currently SerialParser can be used to detect:
- Fixed length data frames with one or more header bytes and an optional terminating byte
- Variable length data frames bounded by one or more header bytes and one terminating byte.
Note: detecting variable length frames require an explicitly set buffer size large enough to hold the longest matched frame.
This means that the largest variable size frame detected can have the length of the buffer size, including the framing bytes.
Example
---------
The following example will call the listener object with "matched" as the second argument (converted to a byte array).
SerialParser.FrameMatchListener listener = new SerialParser.FrameMatchListener() {
public void onFrameMatched(SerialParser.FrameDefinition frame, byte[] data) {
//data will be byte[]{'m', 'a', 't', 'c', 'h', 'e', 'd'}
}
};
//Create a parser to match a variable length frame
SerialParser parser = new SerialParser
.Builder()
.setBufferSize(9) //large enough to match "+matched;"
.addFrameDefinition(
new SerialParser.FrameDefinition(1, "+")
.setTerminatingByte((byte) ';')
.addListener(listener)
)
.build();
parser.add("not matched text +matched;".getBytes());
Installation
------------
SerialParser is available as a Maven repository through jitpack.io
### As a Gradle dependency
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.bugadani:SerialParser:7500c51'
}
### Maven
jitpack.io
https://jitpack.io
com.github.bugadani
SerialParser
7500c51