[ 
https://issues.apache.org/jira/browse/THRIFT-2104?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13933179#comment-13933179
 ] 

Carl Yeksigian commented on THRIFT-2104:
----------------------------------------

Because a ByteBuffer is just a view on the underlying byte array, we have to 
respect the variables as they are presented. When using the ByteBuffers, the 
position needs to be reset to where it should have started before being passed 
in.

One way of doing this:
{code}
@Override
public TObject get() throws TException {
        return new TObject(ByteBuffer.allocate(8).putLong(0).rewind());
}
{code}

Another might involve using the ByteBuffer's slice() method.

> Structs lose binary data when transferred from server to client in Java
> -----------------------------------------------------------------------
>
>                 Key: THRIFT-2104
>                 URL: https://issues.apache.org/jira/browse/THRIFT-2104
>             Project: Thrift
>          Issue Type: Bug
>          Components: Java - Compiler
>    Affects Versions: 0.9
>         Environment: Java 1.7
>            Reporter: Jeff Nelson
>            Priority: Critical
>
> There seems to be an issue where structs lose their binary data when passed 
> from server to client in Java. This can be illustrated using the repro steps 
> below.
> 1. Create a simple struct that contains a binary field
> {code}
> struct TObject {
>         1: required binary data
> }
> {code}
> 2. Create a simple service that has a method that returns a TObject
> {code}
> service TService {
>         TObject get();
> }
> {code}
> 3. Generate java code for the struct and service
> 4. Create a server that implements TService.Iface
> {code}
> @Override
>       public TObject get() throws TException {
>               return new TObject(ByteBuffer.allocate(8).putLong(0));
>       }
> {code} 
> 5. Create a client that calls to get get() function on the server and prints 
> out the capacity of the return TObject's bytebuffer
> {code}
> TTransport transport = new TSocket("localhost", 1111);
> transport.open();
> TProtocol protocol = new TBinaryProtocol(transport);
> TService.Client client = new TService.Client(protocol);
> TObject obj = client.get();
> System.out.println(obj.bufferForData().capacity());
> {code}
> Expected: the capacity to be 8
> Actual: the capacity is 0, which indicates that the data was somehow lost 
> during transmission



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to