com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper Java Examples

The following examples show how to use com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonRpcReaderUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether the encoding is valid.
 * @param in input of bytes
 * @throws IOException this is an IO exception
 * @throws UnsupportedException this is an unsupported exception
 */
private static void checkEncoding(ByteBuf in) throws IOException {
    int inputStart = 0;
    int inputLength = 4;
    fliterCharaters(in);
    byte[] buff = new byte[4];
    in.getBytes(in.readerIndex(), buff);
    ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(new IOContext(new BufferRecycler(),
                                                                                       null,
                                                                                       false),
                                                                         buff, inputStart,
                                                                         inputLength);
    JsonEncoding jsonEncoding = strapper.detectEncoding();
    if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
        throw new UnsupportedException("Only UTF-8 encoding is supported.");
    }
}
 
Example #2
Source File: JSONIO.java    From pom-manipulation-ext with Apache License 2.0 6 votes vote down vote up
public static JsonEncoding detectEncoding(File jsonFile) throws ManipulationException
{
    try ( FileInputStream in = new FileInputStream( jsonFile ) )
    {
        byte[] inputBuffer = new byte[4];
        in.read( inputBuffer );
        IOContext ctxt = new IOContext( new BufferRecycler(), null, false );
        ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper( ctxt, inputBuffer, 0,
                4 );
        JsonEncoding encoding = strapper.detectEncoding();
        logger.debug( "Detected JSON encoding {} for file {}", encoding, jsonFile );
        return encoding;
    }
    catch ( IOException e )
    {
        logger.error( "Unable to detect charset for file: {}", jsonFile, e );
        throw new ManipulationException( "Unable to detect charset for file {}", jsonFile, e );
    }
}
 
Example #3
Source File: JsonRpcDecoder.java    From ovsdb with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf, final List<Object> out)
        throws IOException {
    LOG.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(),
        recordsRead, lastRecordBytes);

    if (lastRecordBytes == 0) {
        if (buf.readableBytes() < 4) {
            return; //wait for more data
        }

        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 = JSON_FACTORY.createParser((InputStream) new ByteBufInputStream(slice));
            JsonNode root = jp.readValueAsTree();
            out.add(root);
            leftCurlies = 0;
            rightCurlies = 0;
            lastRecordBytes = 0;
            recordsRead++;
            break;
        }

        /*
         * Changing this limit to being a warning, we do not wish to "break" in scale environment
         * and currently this limits the ovs of having only around 50 ports defined...
         * I do acknowledge the fast that this might be risky in case of huge amount of strings
         * in which the controller can crash with an OOM, however seems that we need a really huge
         * ovs to reach that limit.
         */

        //We do not want to issue a log message on every extent of the buffer
        //hence logging only once
        if (index - buf.readerIndex() >= maxFrameLength && !maxFrameLimitWasReached) {
            maxFrameLimitWasReached = true;
            LOG.warn("***** OVSDB Frame limit of {} bytes has been reached! *****", this.maxFrameLength);
        }
    }

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