aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/CharacterDelimiterFrameDecoder.java
blob: 9f4e29a6f025c4bedc6d5690f98d89cb51e80747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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));
    }

}