Example usage for io.vertx.core.buffer Buffer getByte

List of usage examples for io.vertx.core.buffer Buffer getByte

Introduction

In this page you can find the example usage for io.vertx.core.buffer Buffer getByte.

Prototype

byte getByte(int pos);

Source Link

Document

Returns the byte at position pos in the Buffer.

Usage

From source file:io.fabric8.msg.jnatsd.BufferSupport.java

License:Apache License

static public Buffer trimFront(Buffer self) {
    int length = self.length();
    int pos = 0;//w w  w. j av a  2s .  c  om
    while ((pos < length) && (self.getByte(pos) <= ' ')) {
        pos++;
    }
    return (pos == 0) ? self : self.getBuffer(pos, length);
}

From source file:io.fabric8.msg.jnatsd.BufferSupport.java

License:Apache License

static public Buffer trimEnd(Buffer self) {
    int length = self.length();
    int pos = length;
    while (pos > 0 && (self.getByte(pos - 1) <= ' ')) {
        pos--;//  w  ww .  j  a v a2 s .  co  m
    }
    return (pos == length - 1) ? self : self.getBuffer(0, pos);
}

From source file:io.fabric8.msg.jnatsd.BufferSupport.java

License:Apache License

static public int indexOf(Buffer self, int start, int end, byte value) {
    int max = Math.min(end, self.length());
    for (; start < end; start++) {
        if (self.getByte(start) == value) {
            return start;
        }//from  w w w . j a v a2 s .c o m
    }
    return -1;
}

From source file:io.fabric8.msg.jnatsd.BufferSupport.java

License:Apache License

static public boolean matches(Buffer self, int pos, Buffer needle) {
    int needleLength = needle.length();
    for (int i = 0; i < needleLength; i++) {
        if (self.getByte(pos + i) != needle.getByte(i)) {
            return false;
        }//w  w  w . ja  v  a  2  s .c o m
    }
    return true;
}

From source file:io.fabric8.msg.jnatsd.BufferSupport.java

License:Apache License

static public Buffer[] split(Buffer self, byte separator) {
    ArrayList<Buffer> rc = new ArrayList<>();
    int pos = 0;/*  w  w w .  j a v  a2 s  . c  o m*/
    int nextStart = pos;
    int end = self.length();
    while (pos < end) {
        if (self.getByte(pos) == separator) {
            if (nextStart < pos) {
                rc.add(self.getBuffer(nextStart, pos));
            }
            nextStart = pos + 1;
        }
        pos++;
    }
    if (nextStart < pos) {
        rc.add(self.getBuffer(nextStart, pos));
    }
    return rc.toArray(new Buffer[rc.size()]);
}

From source file:io.fabric8.msg.jnatsd.protocol.CommandFactory.java

License:Apache License

static AbstractCommand getPub(RoutingMap routingMap, boolean pedantic, Buffer buffer, final int start,
        final int end) {
    int pos = start + 3;
    BufferWrapper subject;/*from   w w w . ja v  a  2s .  c o  m*/
    BufferWrapper replyTo = null;
    BufferWrapper sizeBuffer;

    ///
    // We know a Pub message will look like this:
    //   PUB <subject> [reply-to] <#bytes>\r\n[payload]\r\n
    //
    int firstNewLine = -1;
    int secondNewLine = -1;
    for (; pos < end; pos++) {
        byte b = buffer.getByte(pos);
        if (b == NEWLINE) {
            if (firstNewLine == -1) {
                firstNewLine = pos;
            } else if (secondNewLine == -1) {
                secondNewLine = pos;
                break;
            }
        }
    }

    if (secondNewLine >= 0) {

        int messageStart = start + 3;
        int messageEnd = secondNewLine;

        BufferTokenizer tokenizer = new BufferTokenizer(buffer, messageStart, firstNewLine);
        subject = tokenizer.nextToken();
        Address address = new Address(subject);
        Collection<Subscription> matches = routingMap.getMatches(address);

        Pub pub = new Pub();
        pub.setSubject(address);
        pub.setMatches(matches);
        if (matches != null && !matches.isEmpty()) {
            if (tokenizer.countTokens() >= 2) {
                replyTo = tokenizer.nextToken();
                sizeBuffer = tokenizer.nextToken();
            } else {
                sizeBuffer = tokenizer.nextToken();
            }

            int payloadSize = sizeBuffer.parseToInt();
            int offset = firstNewLine + 1;
            BufferWrapper payload = null;
            int payloadEnd = payloadSize + offset;

            if (payloadEnd < buffer.length()) {
                payload = BufferWrapper.bufferWrapper(buffer, offset, payloadEnd);
            }

            if (replyTo != null) {
                pub.setReplyTo(replyTo);
            }
            pub.bytesRead(messageEnd - start);
            pub.setNoBytesBuffer(sizeBuffer);
            pub.setPayloadSize(payloadSize);
            pub.setPayload(payload);
            return pub;
        } else {
            pub.bytesRead(secondNewLine - start);
            return pub;
        }
    } else if (pedantic && firstNewLine > 0) {
        //Parse the header - which should throw exception if not valid
        BufferTokenizer tokenizer = new BufferTokenizer(buffer, start + 3, firstNewLine);
        subject = tokenizer.nextToken();
        Address address = new Address(subject);

        if (tokenizer.countTokens() >= 2) {
            replyTo = tokenizer.nextToken();
            sizeBuffer = tokenizer.nextToken();
        } else {
            sizeBuffer = tokenizer.nextToken();
        }

        int size = sizeBuffer.parseToInt();

    }
    return null;
}

From source file:io.fabric8.msg.jnatsd.protocol.CommandFactory.java

License:Apache License

static AbstractCommand getCommand(Buffer buffer, boolean pedantic, final int start, final int end)
        throws ProtocolException {
    AbstractCommand command;//from   www. j  a  va2s.com
    int pos = start;
    StringBuffer stringBuffer = new StringBuffer(20);
    for (; pos < end; pos++) {
        byte b = buffer.getByte(pos);
        stringBuffer.append((char) b);

        if (b == NEWLINE && stringBuffer.length() > 1) {
            String strCommand = stringBuffer.toString();
            if (strCommand.startsWith("CO")) {
                command = new Connect();
            } else if (strCommand.startsWith("PU")) {
                command = new Pub();
            } else if (strCommand.startsWith("PI")) {
                command = new Ping();

            } else if (strCommand.startsWith("PO")) {
                command = new Pong();

            } else if (strCommand.startsWith("SU")) {
                command = new Sub();
            } else if (strCommand.startsWith("UN")) {
                command = new UnSub();
            } else {
                throw new ProtocolException("Unexpected command: " + +strCommand.length() + " len " + strCommand
                        + " COMPLETE BUFFER = " + buffer.toString());
            }
            command.build(buffer, start, pos);
            command.bytesRead(pos - start);
            CommandInfo commandInfo = new CommandInfo();
            commandInfo.setCommand(command);
            return command;
        }
    }
    return null;
}

From source file:io.fabric8.msg.jnatsd.protocol.CommandFactory.java

License:Apache License

private static boolean isPub(Buffer buffer, int start, int end) {
    return (start + 1) < end && buffer.getByte(start) == 'P' && buffer.getByte(start + 1) == 'U';
}

From source file:io.fabric8.msg.jnatsd.protocol.ProtocolHelper.java

License:Apache License

public static int skipWhiteSpace(Buffer buffer, int start, int end) {
    int i = start;
    while (i < end) {
        if (isWhiteSpace(buffer.getByte(i))) {
            i++;//from www  . ja  va  2s.c o  m
        } else {
            break;
        }
    }
    return i;
}

From source file:io.reactiverse.pgclient.impl.codec.util.Util.java

License:Apache License

public static int writeHexString(Buffer buffer, ByteBuf to) {
    int len = buffer.length();
    for (int i = 0; i < len; i++) {
        final int b = Byte.toUnsignedInt(buffer.getByte(i));
        final int firstDigit = b >> 4;
        final byte firstHexDigit = (byte) bin2hex(firstDigit);
        final int secondDigit = b & FIRST_HALF_BYTE_MASK;
        final byte secondHexDigit = (byte) bin2hex(secondDigit);
        to.writeByte(firstHexDigit);/* ww  w  .j a va  2 s.  c o  m*/
        to.writeByte(secondHexDigit);
    }
    return len;
}