Example usage for com.fasterxml.jackson.core.json ByteSourceJsonBootstrapper detectEncoding

List of usage examples for com.fasterxml.jackson.core.json ByteSourceJsonBootstrapper detectEncoding

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core.json ByteSourceJsonBootstrapper detectEncoding.

Prototype

public JsonEncoding detectEncoding() throws IOException, JsonParseException 

Source Link

Document

Method that should be called after constructing an instace.

Usage

From source file:org.opendaylight.groupbasedpolicy.jsonrpc.JsonRpcDecoder.java

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {

    logger.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(),
            recordsRead, lastRecordBytes);

    if (lastRecordBytes == 0) {
        if (buf.readableBytes() < 4) {
            return; //wait for more data
        }//from w w  w.  j a v a  2 s. c om

        skipSpaces(buf);

        byte[] buff = new byte[4];
        buf.getBytes(buf.readerIndex(), buff);
        ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(jacksonIOContext, buff, 0, 4);
        JsonEncoding jsonEncoding = strapper.detectEncoding();
        if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
            throw new InvalidEncodingException(jsonEncoding.getJavaName(), "currently only UTF-8 is supported");
        }
    }

    int i = lastRecordBytes + buf.readerIndex();

    for (; i < buf.writerIndex(); i++) {
        switch (buf.getByte(i)) {
        case '{':
            if (!inS)
                leftCurlies++;
            break;
        case '}':
            if (!inS)
                rightCurlies++;
            break;
        case '"': {
            if (buf.getByte(i - 1) != '\\')
                inS = !inS;
            break;
        }
        default:
            break;
        }

        if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) {
            ByteBuf slice = buf.readSlice(1 + i - buf.readerIndex());
            JsonParser jp = jacksonJsonFactory.createParser(new ByteBufInputStream(slice));
            JsonNode root = jp.readValueAsTree();
            out.add(root);
            leftCurlies = rightCurlies = lastRecordBytes = 0;
            recordsRead++;
            break;
        }

        if (i - buf.readerIndex() >= maxFrameLength) {
            fail(ctx, i - buf.readerIndex());
        }
    }

    // end of stream, save the incomplete record index to avoid reexamining the whole on next run
    if (i >= buf.writerIndex()) {
        lastRecordBytes = buf.readableBytes();
        return;
    }
}

From source file:org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder.java

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {

    logger.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(),
            recordsRead, lastRecordBytes);

    if (lastRecordBytes == 0) {
        if (buf.readableBytes() < 4) {
            return; //wait for more data
        }/*from  www .j a v  a 2 s. co  m*/

        skipSpaces(buf);

        byte[] buff = new byte[4];
        buf.getBytes(buf.readerIndex(), buff);
        ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(jacksonIOContext, buff, 0, 4);
        JsonEncoding jsonEncoding = strapper.detectEncoding();
        if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
            throw new InvalidEncodingException(jsonEncoding.getJavaName(), "currently only UTF-8 is supported");
        }
    }

    int index = lastRecordBytes + buf.readerIndex();

    for (; index < buf.writerIndex(); index++) {
        switch (buf.getByte(index)) {
        case '{':
            if (!inS) {
                leftCurlies++;
            }
            break;
        case '}':
            if (!inS) {
                rightCurlies++;
            }
            break;
        case '"':
            if (buf.getByte(index - 1) != '\\') {
                inS = !inS;
            }
            break;
        default:
            break;
        }

        if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) {
            ByteBuf slice = buf.readSlice(1 + index - buf.readerIndex());
            JsonParser jp = jacksonJsonFactory.createParser(new ByteBufInputStream(slice));
            JsonNode root = jp.readValueAsTree();
            out.add(root);
            leftCurlies = rightCurlies = lastRecordBytes = 0;
            recordsRead++;
            break;
        }

        if (index - buf.readerIndex() >= maxFrameLength) {
            fail(ctx, index - buf.readerIndex());
        }
    }

    // end of stream, save the incomplete record index to avoid reexamining the whole on next run
    if (index >= buf.writerIndex()) {
        lastRecordBytes = buf.readableBytes();
        return;
    }
}