com.alibaba.dubbo.common.io.UnsafeByteArrayInputStream Java Examples

The following examples show how to use com.alibaba.dubbo.common.io.UnsafeByteArrayInputStream. 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: Builder.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Override
public Serializable parseFrom(GenericObjectInput in) throws IOException {
    byte b = in.read0();
    if (b == OBJECT_NULL)
        return null;
    if (b != OBJECT_STREAM)
        throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_STREAM, get " + b + ".");

    UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(in.read0(in.readUInt()));
    CompactedObjectInputStream ois = new CompactedObjectInputStream(bis);
    try {
        return (Serializable) ois.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException(StringUtils.toString(e));
    }
}
 
Example #2
Source File: CodecAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    byte[] bytes = new byte[buffer.readableBytes()];
    int savedReaderIndex = buffer.readerIndex();
    buffer.readBytes(bytes);
    UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(bytes);
    Object result = codec.decode(channel, is);
    buffer.readerIndex(savedReaderIndex + is.position());
    return result == Codec.NEED_MORE_INPUT ? DecodeResult.NEED_MORE_INPUT : result;
}
 
Example #3
Source File: Builder.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public Serializable parseFrom(GenericObjectInput in) throws IOException
{
	byte b = in.read0();
	if( b == OBJECT_NULL )
		return null;
	if( b != OBJECT_STREAM )
		throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_STREAM, get " + b + ".");

	UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(in.read0(in.readUInt()));
	CompactedObjectInputStream ois = new CompactedObjectInputStream(bis);
	try{ return (Serializable)ois.readObject(); }
	catch(ClassNotFoundException e){ throw new IOException(StringUtils.toString(e)); }
}
 
Example #4
Source File: CodecAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    byte[] bytes = new byte[buffer.readableBytes()];
    int savedReaderIndex = buffer.readerIndex();
    buffer.readBytes(bytes);
    UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(bytes);
    Object result = codec.decode(channel, is);
    buffer.readerIndex(savedReaderIndex + is.position());
    return result == Codec.NEED_MORE_INPUT ? DecodeResult.NEED_MORE_INPUT : result;
}
 
Example #5
Source File: DataInputOutputTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public void testMain() throws Exception
{
	// write.
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	DataOutput cos = new GenericDataOutput(os);
	writeTest(cos);

	// read.
	byte[] b = os.toByteArray();
	DataInput cis = new GenericDataInput(new UnsafeByteArrayInputStream(b));
	readTest(cis);
}
 
Example #6
Source File: DataInputOutputTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void testMain() throws Exception
{
	// write.
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	DataOutput cos = new GenericDataOutput(os);
	writeTest(cos);

	// read.
	byte[] b = os.toByteArray();
	DataInput cis = new GenericDataInput(new UnsafeByteArrayInputStream(b));
	readTest(cis);
}
 
Example #7
Source File: CodecAdapter.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    byte[] bytes = new byte[buffer.readableBytes()];
    int savedReaderIndex = buffer.readerIndex();
    buffer.readBytes(bytes);
    UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(bytes);
    Object result = codec.decode(channel, is);
    buffer.readerIndex(savedReaderIndex + is.position());
    return result == Codec.NEED_MORE_INPUT ? DecodeResult.NEED_MORE_INPUT : result;
}
 
Example #8
Source File: CodecAdapter.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    byte[] bytes = new byte[buffer.readableBytes()];
    int savedReaderIndex = buffer.readerIndex();
    buffer.readBytes(bytes);
    UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(bytes);
    Object result = codec.decode(channel, is);
    buffer.readerIndex(savedReaderIndex + is.position());
    return result == Codec.NEED_MORE_INPUT ? DecodeResult.NEED_MORE_INPUT : result;
}
 
Example #9
Source File: DataInputOutputTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public void testMain() throws Exception
{
	// write.
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	DataOutput cos = new GenericDataOutput(os);
	writeTest(cos);

	// read.
	byte[] b = os.toByteArray();
	DataInput cis = new GenericDataInput(new UnsafeByteArrayInputStream(b));
	readTest(cis);
}
 
Example #10
Source File: Builder.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Override
public Serializable parseFrom(GenericObjectInput in) throws IOException
{
	byte b = in.read0();
	if( b == OBJECT_NULL )
		return null;
	if( b != OBJECT_STREAM )
		throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_STREAM, get " + b + ".");

	UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(in.read0(in.readUInt()));
	CompactedObjectInputStream ois = new CompactedObjectInputStream(bis);
	try{ return (Serializable)ois.readObject(); }
	catch(ClassNotFoundException e){ throw new IOException(StringUtils.toString(e)); }
}
 
Example #11
Source File: CodecAdapter.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    byte[] bytes = new byte[buffer.readableBytes()];
    int savedReaderIndex = buffer.readerIndex();
    buffer.readBytes(bytes);
    UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(bytes);
    Object result = codec.decode(channel, is);
    buffer.readerIndex(savedReaderIndex + is.position());
    return result == Codec.NEED_MORE_INPUT ? DecodeResult.NEED_MORE_INPUT : result;
}
 
Example #12
Source File: DataInputOutputTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void testMain() throws Exception
{
	// write.
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	DataOutput cos = new GenericDataOutput(os);
	writeTest(cos);

	// read.
	byte[] b = os.toByteArray();
	DataInput cis = new GenericDataInput(new UnsafeByteArrayInputStream(b));
	readTest(cis);
}
 
Example #13
Source File: Builder.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public Serializable parseFrom(GenericObjectInput in) throws IOException
{
	byte b = in.read0();
	if( b == OBJECT_NULL )
		return null;
	if( b != OBJECT_STREAM )
		throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_STREAM, get " + b + ".");

	UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(in.read0(in.readUInt()));
	CompactedObjectInputStream ois = new CompactedObjectInputStream(bis);
	try{ return (Serializable)ois.readObject(); }
	catch(ClassNotFoundException e){ throw new IOException(StringUtils.toString(e)); }
}
 
Example #14
Source File: Builder.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public Serializable parseFrom(GenericObjectInput in) throws IOException
{
	byte b = in.read0();
	if( b == OBJECT_NULL )
		return null;
	if( b != OBJECT_STREAM )
		throw new IOException("Input format error, expect OBJECT_NULL|OBJECT_STREAM, get " + b + ".");

	UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(in.read0(in.readUInt()));
	CompactedObjectInputStream ois = new CompactedObjectInputStream(bis);
	try{ return (Serializable)ois.readObject(); }
	catch(ClassNotFoundException e){ throw new IOException(StringUtils.toString(e)); }
}
 
Example #15
Source File: CodecAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    byte[] bytes = new byte[buffer.readableBytes()];
    int savedReaderIndex = buffer.readerIndex();
    buffer.readBytes(bytes);
    UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(bytes);
    Object result = codec.decode(channel, is);
    buffer.readerIndex(savedReaderIndex + is.position());
    return result == Codec.NEED_MORE_INPUT ? DecodeResult.NEED_MORE_INPUT : result;
}
 
Example #16
Source File: DataInputOutputTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void testMain() throws Exception
{
	// write.
	UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
	DataOutput cos = new GenericDataOutput(os);
	writeTest(cos);

	// read.
	byte[] b = os.toByteArray();
	DataInput cis = new GenericDataInput(new UnsafeByteArrayInputStream(b));
	readTest(cis);
}
 
Example #17
Source File: DeprecatedExchangeCodec.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
protected Object decode(Channel channel, InputStream is, int readable, byte[] header) throws IOException {
    // check magic number.
    if (readable > 0 && header[0] != MAGIC_HIGH
        || readable > 1 && header[1] != MAGIC_LOW) {
        int length = header.length;
        if (header.length < readable) {
            header = Bytes.copyOf(header, readable);
            is.read(header, length, readable - length);
        }
        for (int i = 1; i < header.length - 1; i++) {
            if (header[i] == MAGIC_HIGH && header[i + 1] == MAGIC_LOW) {
                UnsafeByteArrayInputStream bis = ((UnsafeByteArrayInputStream) is);
                bis.position(bis.position() - header.length + i);
                header = Bytes.copyOf(header, i);
                break;
            }
        }
        return super.decode(channel, is, readable, header);
    }
    // check length.
    if (readable < HEADER_LENGTH) {
        return NEED_MORE_INPUT;
    }

    // get data length.
    int len = Bytes.bytes2int(header, 12);
    checkPayload(channel, len);

    int tt = len + HEADER_LENGTH;
    if (readable < tt) {
        return NEED_MORE_INPUT;
    }

    // limit input stream.
    if (readable != tt)
        is = StreamUtils.limitedInputStream(is, len);

    try {
        return decodeBody(channel, is, header);
    } finally {
        if (is.available() > 0) {
            try {
                if (logger.isWarnEnabled()) {
                    logger.warn("Skip input stream " + is.available());
                }
                StreamUtils.skipUnusedStream(is);
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }
}
 
Example #18
Source File: DeprecatedExchangeCodec.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected Object decode(Channel channel, InputStream is, int readable, byte[] header) throws IOException {
    // check magic number.
    if (readable > 0 && header[0] != MAGIC_HIGH
        || readable > 1 && header[1] != MAGIC_LOW) {
        int length = header.length;
        if (header.length < readable) {
            header = Bytes.copyOf(header, readable);
            is.read(header, length, readable - length);
        }
        for (int i = 1; i < header.length - 1; i++) {
            if (header[i] == MAGIC_HIGH && header[i + 1] == MAGIC_LOW) {
                UnsafeByteArrayInputStream bis = ((UnsafeByteArrayInputStream) is);
                bis.position(bis.position() - header.length + i);
                header = Bytes.copyOf(header, i);
                break;
            }
        }
        return super.decode(channel, is, readable, header);
    }
    // check length.
    if (readable < HEADER_LENGTH) {
        return NEED_MORE_INPUT;
    }

    // get data length.
    int len = Bytes.bytes2int(header, 12);
    checkPayload(channel, len);

    int tt = len + HEADER_LENGTH;
    if (readable < tt) {
        return NEED_MORE_INPUT;
    }

    // limit input stream.
    if (readable != tt)
        is = StreamUtils.limitedInputStream(is, len);

    try {
        return decodeBody(channel, is, header);
    } finally {
        if (is.available() > 0) {
            try {
                if (logger.isWarnEnabled()) {
                    logger.warn("Skip input stream " + is.available());
                }
                StreamUtils.skipUnusedStream(is);
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }
}
 
Example #19
Source File: Builder.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public T parseFrom(byte[] b) throws IOException
{
	return parseFrom(new UnsafeByteArrayInputStream(b));
}
 
Example #20
Source File: Builder.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public T parseFrom(byte[] b) throws IOException
{
	return parseFrom(new UnsafeByteArrayInputStream(b));
}
 
Example #21
Source File: DeprecatedExchangeCodec.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected Object decode(Channel channel, InputStream is, int readable, byte[] header) throws IOException {
    // check magic number.
    if (readable > 0 && header[0] != MAGIC_HIGH
        || readable > 1 && header[1] != MAGIC_LOW) {
        int length = header.length;
        if (header.length < readable) {
            header = Bytes.copyOf(header, readable);
            is.read(header, length, readable - length);
        }
        for (int i = 1; i < header.length - 1; i++) {
            if (header[i] == MAGIC_HIGH && header[i + 1] == MAGIC_LOW) {
                UnsafeByteArrayInputStream bis = ((UnsafeByteArrayInputStream) is);
                bis.position(bis.position() - header.length + i);
                header = Bytes.copyOf(header, i);
                break;
            }
        }
        return super.decode(channel, is, readable, header);
    }
    // check length.
    if (readable < HEADER_LENGTH) {
        return NEED_MORE_INPUT;
    }

    // get data length.
    int len = Bytes.bytes2int(header, 12);
    checkPayload(channel, len);

    int tt = len + HEADER_LENGTH;
    if (readable < tt) {
        return NEED_MORE_INPUT;
    }

    // limit input stream.
    if (readable != tt)
        is = StreamUtils.limitedInputStream(is, len);

    try {
        return decodeBody(channel, is, header);
    } finally {
        if (is.available() > 0) {
            try {
                if (logger.isWarnEnabled()) {
                    logger.warn("Skip input stream " + is.available());
                }
                StreamUtils.skipUnusedStream(is);
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }
}
 
Example #22
Source File: Builder.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
public T parseFrom(byte[] b) throws IOException {
    return parseFrom(new UnsafeByteArrayInputStream(b));
}
 
Example #23
Source File: Builder.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public T parseFrom(byte[] b) throws IOException
{
	return parseFrom(new UnsafeByteArrayInputStream(b));
}
 
Example #24
Source File: DeprecatedExchangeCodec.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
protected Object decode(Channel channel, InputStream is, int readable, byte[] header) throws IOException {
    // check magic number.
    if (readable > 0 && header[0] != MAGIC_HIGH
        || readable > 1 && header[1] != MAGIC_LOW) {
        int length = header.length;
        if (header.length < readable) {
            header = Bytes.copyOf(header, readable);
            is.read(header, length, readable - length);
        }
        for (int i = 1; i < header.length - 1; i++) {
            if (header[i] == MAGIC_HIGH && header[i + 1] == MAGIC_LOW) {
                UnsafeByteArrayInputStream bis = ((UnsafeByteArrayInputStream) is);
                bis.position(bis.position() - header.length + i);
                header = Bytes.copyOf(header, i);
                break;
            }
        }
        return super.decode(channel, is, readable, header);
    }
    // check length.
    if (readable < HEADER_LENGTH) {
        return NEED_MORE_INPUT;
    }

    // get data length.
    int len = Bytes.bytes2int(header, 12);
    checkPayload(channel, len);

    int tt = len + HEADER_LENGTH;
    if (readable < tt) {
        return NEED_MORE_INPUT;
    }

    // limit input stream.
    if (readable != tt)
        is = StreamUtils.limitedInputStream(is, len);

    try {
        return decodeBody(channel, is, header);
    } finally {
        if (is.available() > 0) {
            try {
                if (logger.isWarnEnabled()) {
                    logger.warn("Skip input stream " + is.available());
                }
                StreamUtils.skipUnusedStream(is);
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }
}
 
Example #25
Source File: Builder.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public T parseFrom(byte[] b) throws IOException
{
	return parseFrom(new UnsafeByteArrayInputStream(b));
}
 
Example #26
Source File: DeprecatedExchangeCodec.java    From dubbox with Apache License 2.0 4 votes vote down vote up
protected Object decode(Channel channel, InputStream is, int readable, byte[] header) throws IOException {
    // check magic number.
    if (readable > 0 && header[0] != MAGIC_HIGH
        || readable > 1 && header[1] != MAGIC_LOW) {
        int length = header.length;
        if (header.length < readable) {
            header = Bytes.copyOf(header, readable);
            is.read(header, length, readable - length);
        }
        for (int i = 1; i < header.length - 1; i++) {
            if (header[i] == MAGIC_HIGH && header[i + 1] == MAGIC_LOW) {
                UnsafeByteArrayInputStream bis = ((UnsafeByteArrayInputStream) is);
                bis.position(bis.position() - header.length + i);
                header = Bytes.copyOf(header, i);
                break;
            }
        }
        return super.decode(channel, is, readable, header);
    }
    // check length.
    if (readable < HEADER_LENGTH) {
        return NEED_MORE_INPUT;
    }

    // get data length.
    int len = Bytes.bytes2int(header, 12);
    checkPayload(channel, len);

    int tt = len + HEADER_LENGTH;
    if (readable < tt) {
        return NEED_MORE_INPUT;
    }

    // limit input stream.
    if (readable != tt)
        is = StreamUtils.limitedInputStream(is, len);

    try {
        return decodeBody(channel, is, header);
    } finally {
        if (is.available() > 0) {
            try {
                if (logger.isWarnEnabled()) {
                    logger.warn("Skip input stream " + is.available());
                }
                StreamUtils.skipUnusedStream(is);
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }
}
 
Example #27
Source File: DeprecatedExchangeCodec.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
protected Object decode(Channel channel, InputStream is, int readable, byte[] header) throws IOException {
    // check magic number.
    if (readable > 0 && header[0] != MAGIC_HIGH
            || readable > 1 && header[1] != MAGIC_LOW) {
        int length = header.length;
        if (header.length < readable) {
            header = Bytes.copyOf(header, readable);
            is.read(header, length, readable - length);
        }
        for (int i = 1; i < header.length - 1; i++) {
            if (header[i] == MAGIC_HIGH && header[i + 1] == MAGIC_LOW) {
                UnsafeByteArrayInputStream bis = ((UnsafeByteArrayInputStream) is);
                bis.position(bis.position() - header.length + i);
                header = Bytes.copyOf(header, i);
                break;
            }
        }
        return super.decode(channel, is, readable, header);
    }
    // check length.
    if (readable < HEADER_LENGTH) {
        return NEED_MORE_INPUT;
    }

    // get data length.
    int len = Bytes.bytes2int(header, 12);
    checkPayload(channel, len);

    int tt = len + HEADER_LENGTH;
    if (readable < tt) {
        return NEED_MORE_INPUT;
    }

    // limit input stream.
    if (readable != tt)
        is = StreamUtils.limitedInputStream(is, len);

    try {
        return decodeBody(channel, is, header);
    } finally {
        if (is.available() > 0) {
            try {
                if (logger.isWarnEnabled()) {
                    logger.warn("Skip input stream " + is.available());
                }
                StreamUtils.skipUnusedStream(is);
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }
}