Package com.couchbase.client.deps.io.netty.buffer

Examples of com.couchbase.client.deps.io.netty.buffer.ByteBuf


    protected Tuple2<ByteBuf, Integer> doEncode(LegacyDocument document)
        throws Exception {

        int flags = 0;
        Object content = document.content();
        ByteBuf encoded = Unpooled.buffer();

        if (content instanceof String) {
            encoded.writeBytes(((String) content).getBytes(CharsetUtil.UTF_8));
        } else if (content instanceof Long) {
            flags |= SPECIAL_LONG;
            encoded.writeBytes(encodeNum((Long) content, 8));
        } else if (content instanceof Integer) {
            flags |= SPECIAL_INT;
            encoded.writeBytes(encodeNum((Integer) content, 4));
        } else if (content instanceof Boolean) {
            flags |= SPECIAL_BOOLEAN;
            boolean b = (Boolean) content;
            encoded = Unpooled.buffer().writeByte(b ? '1' : '0');
        } else if (content instanceof Date) {
            flags |= SPECIAL_DATE;
            encoded.writeBytes(encodeNum(((Date) content).getTime(), 8));
        } else if (content instanceof Byte) {
            flags |= SPECIAL_BYTE;
            encoded.writeByte((Byte) content);
        } else if (content instanceof Float) {
            flags |= SPECIAL_FLOAT;
            encoded.writeBytes(encodeNum(Float.floatToRawIntBits((Float) content), 4));
        } else if (content instanceof Double) {
            flags |= SPECIAL_DOUBLE;
            encoded.writeBytes(encodeNum(Double.doubleToRawLongBits((Double) content), 8));
        } else if (content instanceof byte[]) {
            flags |= SPECIAL_BYTEARRAY;
            encoded.writeBytes((byte[]) content);
        } else {
            flags |= SERIALIZED;
            encoded.writeBytes(serialize(content));
        }

        if (encoded.readableBytes() >= compressionThreshold) {
            byte[] compressed = compress(encoded.copy().array());
            if (compressed.length < encoded.array().length) {
                encoded.clear().writeBytes(compressed);
                flags |= COMPRESSED;
            }
        }

        return Tuple.create(encoded, flags);
View Full Code Here


        return newDocument(id, expiry, decoded, cas);
    }

    @Override
    protected Tuple2<ByteBuf, Integer> doEncode(final JsonBooleanDocument document) throws Exception {
        ByteBuf encoded = Unpooled.copiedBuffer(document.content() ? "true" : "false", CharsetUtil.UTF_8);
        return Tuple.create(encoded, TranscoderUtils.BOOLEAN_COMPAT_FLAGS);
    }
View Full Code Here

        assertEquals(TranscoderUtils.STRING_COMPAT_FLAGS, (long) encoded.value2());
    }

    @Test
    public void shouldDecodeCommonString() {
        ByteBuf content = Unpooled.copiedBuffer("value", CharsetUtil.UTF_8);
        StringDocument decoded = converter.decode("id", content, 0, 0, TranscoderUtils.STRING_COMMON_FLAGS,
            ResponseStatus.SUCCESS);

        assertEquals("value", decoded.content());
    }
View Full Code Here

        assertEquals("value", decoded.content());
    }

    @Test
    public void shouldDecodeLegacyString() {
        ByteBuf content = Unpooled.copiedBuffer("value", CharsetUtil.UTF_8);
        StringDocument decoded = converter.decode("id", content, 0, 0, 0,
            ResponseStatus.SUCCESS);

        assertEquals("value", decoded.content());
    }
View Full Code Here

        assertEquals("value", decoded.content());
    }

    @Test
    public void shouldReleaseBufferWhenDecoded() {
        ByteBuf content = Unpooled.copiedBuffer("value", CharsetUtil.UTF_8);
        StringDocument decoded = converter.decode("id", content, 0, 0, TranscoderUtils.STRING_COMMON_FLAGS,
            ResponseStatus.SUCCESS);
        assertEquals(0, content.refCnt());
    }
View Full Code Here

        assertEquals(0, content.refCnt());
    }

    @Test(expected = TranscodingException.class)
    public void shouldReleaseBufferWhenError() {
        ByteBuf content = Unpooled.copiedBuffer("value", CharsetUtil.UTF_8);
        int wrongFlags = 1234;
        try {
            converter.decode("id", content, 0, 0, wrongFlags, ResponseStatus.SUCCESS);
        } finally {
            assertEquals(0, content.refCnt());
        }
    }
View Full Code Here

    }

    @Test
    public void shouldDecodeLegacyLong() {
        byte[] bytes = LegacyTranscoder.encodeNum(Long.MAX_VALUE, 8);
        ByteBuf content = Unpooled.buffer().writeBytes(bytes);
        JsonLongDocument decoded = converter.decode("id", content, 0, 0, 3 << 8,
            ResponseStatus.SUCCESS);

        assertEquals(Long.MAX_VALUE, (long) decoded.content());
    }
View Full Code Here

    }

    @Test
    public void shouldDecodeLegacyInt() {
        byte[] bytes = LegacyTranscoder.encodeNum(Integer.MAX_VALUE, 4);
        ByteBuf content = Unpooled.buffer().writeBytes(bytes);
        JsonLongDocument decoded = converter.decode("id", content, 0, 0, 2 << 8,
            ResponseStatus.SUCCESS);

        assertEquals(Integer.MAX_VALUE, (long) decoded.content());
    }
View Full Code Here

        assertEquals(Integer.MAX_VALUE, (long) decoded.content());
    }

    @Test
    public void shouldDecodeCommonFlagsLong() {
        ByteBuf content = Unpooled.copiedBuffer("9223372036854775807", CharsetUtil.UTF_8);
        JsonLongDocument decoded = converter.decode("id", content, 0, 0, TranscoderUtils.JSON_COMPAT_FLAGS,
            ResponseStatus.SUCCESS);

        assertEquals(Long.MAX_VALUE, (long) decoded.content());
    }
View Full Code Here

        assertEquals(Long.MAX_VALUE, (long) decoded.content());
    }

    @Test
    public void shouldDecodeCommonFlagsInt() {
        ByteBuf content = Unpooled.copiedBuffer("2147483647", CharsetUtil.UTF_8);
        JsonLongDocument decoded = converter.decode("id", content, 0, 0, TranscoderUtils.JSON_COMPAT_FLAGS,
            ResponseStatus.SUCCESS);

        assertEquals(Integer.MAX_VALUE, (long) decoded.content());
    }
View Full Code Here

TOP

Related Classes of com.couchbase.client.deps.io.netty.buffer.ByteBuf

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.