diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-01-15 11:16:17 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-01-15 11:16:17 +1300 |
commit | 5bdf64907c2935111f89844640f9ab96d119dc9d (patch) | |
tree | 4fed964a9d6107d577c6e409bf9afd53d27c3c75 /src/org/traccar/CharacterDelimiterFrameDecoder.java | |
parent | 9546b1abf9f296865cddebb06d1f4cadcd793b71 (diff) | |
download | trackermap-server-5bdf64907c2935111f89844640f9ab96d119dc9d.tar.gz trackermap-server-5bdf64907c2935111f89844640f9ab96d119dc9d.tar.bz2 trackermap-server-5bdf64907c2935111f89844640f9ab96d119dc9d.zip |
Create char delimiter frame decoder
Diffstat (limited to 'src/org/traccar/CharacterDelimiterFrameDecoder.java')
-rw-r--r-- | src/org/traccar/CharacterDelimiterFrameDecoder.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/org/traccar/CharacterDelimiterFrameDecoder.java b/src/org/traccar/CharacterDelimiterFrameDecoder.java new file mode 100644 index 000000000..9f4e29a6f --- /dev/null +++ b/src/org/traccar/CharacterDelimiterFrameDecoder.java @@ -0,0 +1,42 @@ +package org.traccar; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder; + +public class CharacterDelimiterFrameDecoder extends DelimiterBasedFrameDecoder { + + private static ChannelBuffer createDelimiter(char delimiter) { + byte buf[] = { (byte) delimiter }; + return ChannelBuffers.wrappedBuffer(buf); + } + + private static ChannelBuffer createDelimiter(String delimiter) { + byte buf[] = new byte[delimiter.length()]; + for (int i = 0; i < delimiter.length(); i++) { + buf[i] = (byte) delimiter.charAt(i); + } + return ChannelBuffers.wrappedBuffer(buf); + } + + private static ChannelBuffer[] convertDelimiters(String[] delimiters) { + ChannelBuffer result[] = new ChannelBuffer[delimiters.length]; + for (int i = 0; i < delimiters.length; i++) { + result[i] = createDelimiter(delimiters[i]); + } + return result; + } + + public CharacterDelimiterFrameDecoder(int maxFrameLength, char delimiter) { + super(maxFrameLength, createDelimiter(delimiter)); + } + + public CharacterDelimiterFrameDecoder(int maxFrameLength, String delimiter) { + super(maxFrameLength, createDelimiter(delimiter)); + } + + public CharacterDelimiterFrameDecoder(int maxFrameLength, String... delimiters) { + super(maxFrameLength, convertDelimiters(delimiters)); + } + +} |