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

ASF GitHub Bot commented on THRIFT-3432:
----------------------------------------

Github user nsuke commented on a diff in the pull request:

    https://github.com/apache/thrift/pull/705#discussion_r46765197
  
    --- Diff: lib/java/src/org/apache/thrift/transport/TByteBuffer.java ---
    @@ -0,0 +1,88 @@
    +package org.apache.thrift.transport;
    +
    +import java.nio.BufferOverflowException;
    +import java.nio.BufferUnderflowException;
    +import java.nio.ByteBuffer;
    +
    +/**
    + * ByteBuffer-backed implementation of TTransport.
    + */
    +public class TByteBuffer extends TTransport {
    +  private ByteBuffer byteBuffer;
    +
    +  /**
    +   * Creates a new TByteBuffer wrapping a given NIO ByteBuffer.
    +   */
    +  public TByteBuffer(ByteBuffer byteBuffer) {
    +    this.byteBuffer = byteBuffer;
    +  }
    +
    +  /**
    +   * Resets the underlying NIO ByteBuffer.
    +   */
    +  public void reset(ByteBuffer byteBuffer) {
    +    this.byteBuffer = byteBuffer;
    +  }
    +
    +  @Override
    +  public boolean isOpen() {
    +    return byteBuffer != null;
    +  }
    +
    +  @Override
    +  public void open() {
    +  }
    +
    +  @Override
    +  public void close() {
    +  }
    +
    +  @Override
    +  public int read(byte[] buf, int off, int len) throws TTransportException 
{
    +    final int n = Math.min(byteBuffer.remaining(), len);
    +    if (n > 0) {
    +      try {
    +        byteBuffer.get(buf, off, len);
    +      }
    +      catch (BufferUnderflowException e) {
    --- End diff --
    
    Would you fix style of `catch`s like this ?
    
        } catch {


> Add a TByteBuffer transport to the Java library
> -----------------------------------------------
>
>                 Key: THRIFT-3432
>                 URL: https://issues.apache.org/jira/browse/THRIFT-3432
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Java - Library
>    Affects Versions: 0.9.3
>            Reporter: Tom Lee
>            Priority: Minor
>
> A bounded alternative to TMemoryBuffer that uses java.nio.ByteBuffer under 
> the hood.
> This is useful both to set sane constraints on maximum message size in a 
> transport-agnostic manner but also because direct buffers can be used to 
> serialize Thrift off-heap, avoiding allocations (thus reducing GC frequency 
> and potentially live set size)



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to