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

James E. King III commented on THRIFT-1039:
-------------------------------------------

I can confirm as of 0.12.0:

A structure with a list<binary> field generates:
```
  @org.apache.thrift.annotation.Nullable
  public java.util.List<java.nio.ByteBuffer> getBinaries() {
    return this.binaries;
  }
```

A structure with a binary field generates:
```
  public byte[] getBytes() {
    setBytes(org.apache.thrift.TBaseHelper.rightSize(bytes));
    return bytes == null ? null : bytes.array();
  }
```

I agree that it is strange to have two different types for the same thing.
If a change were made in this area I would want it to have a java compiler
option that defaulted to the new mode, had the option of generating the
old mode, and was documented as a breaking change.

I'll leave this open in the backlog because it is a valid issue.

> Refactor methods accessing binary data
> --------------------------------------
>
>                 Key: THRIFT-1039
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1039
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Java - Compiler
>         Environment: All
>            Reporter: Mathias Herberts
>            Priority: Minor
>         Attachments: THRIFT-1039.patch
>
>
> Since THRIFT-830, binary fields are implemented using ByteBuffer.
> struct A {
>   1: binary bin_field,
> }
> will generate a method 'byte[] getBin_field()' which will optionally resize 
> the underlying ByteBuffer so it has a capacity of 'remaining()' and return 
> the backing array of this new ByteBuffer.
> This has several implications.
> First the ByteBuffer in 'bin_field' might be modified, thus misleading users 
> into thinking that the call to 'getBin_field()' had no effect on the 
> underlying structure.
> Thus the following will fail:
> byte[] b = new byte[2];
> b[0] = 0x01;
> A a = new A();
> a.setBin_field(ByteBuffer.wrap(b, 0, 1));
> byte[] bb = a.getBin_field();
> b[0] = 0x02;
> Assert.assertEquals(0x02, bb[0]);
> Second it creates a singularity in the getters as all other getters involving 
> containers of binary data will return collections of ByteBuffer.
> I suggest we refactor to something more in the line of what HADOOP-6298 has 
> done, i.e. provide a copyBytes() method which copies the bytes in a 
> ByteBuffer and returns a correctly sized array.
> Such a method could be generated for each structure, with the following 
> signature:
> public byte[] copyBytes(ByteBuffer b);
> This method would basically do what the current 'getBin_field' type methods 
> do (allocate a byte array and fill it with the ByteBuffer's data), except it 
> would not modify the structure's internal ByteBuffers.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to