1 package org
.apache
.thrift
.transport
;
3 import java
.nio
.BufferOverflowException
;
4 import java
.nio
.BufferUnderflowException
;
5 import java
.nio
.ByteBuffer
;
8 * ByteBuffer-backed implementation of TTransport.
10 public final class TByteBuffer
extends TTransport
{
11 private final ByteBuffer byteBuffer
;
14 * Creates a new TByteBuffer wrapping a given NIO ByteBuffer.
16 public TByteBuffer(ByteBuffer byteBuffer
) {
17 this.byteBuffer
= byteBuffer
;
21 public boolean isOpen() {
34 public int read(byte[] buf
, int off
, int len
) throws TTransportException
{
35 final int n
= Math
.min(byteBuffer
.remaining(), len
);
38 byteBuffer
.get(buf
, off
, n
);
39 } catch (BufferUnderflowException e
) {
40 throw new TTransportException("Unexpected end of input buffer", e
);
47 public void write(byte[] buf
, int off
, int len
) throws TTransportException
{
49 byteBuffer
.put(buf
, off
, len
);
50 } catch (BufferOverflowException e
) {
51 throw new TTransportException("Not enough room in output buffer", e
);
56 * Get the underlying NIO ByteBuffer.
58 public ByteBuffer
getByteBuffer() {
63 * Convenience method to call clear() on the underlying NIO ByteBuffer.
65 public TByteBuffer
clear() {
71 * Convenience method to call flip() on the underlying NIO ByteBuffer.
73 public TByteBuffer
flip() {
79 * Convenience method to convert the underlying NIO ByteBuffer to a
80 * plain old byte array.
82 public byte[] toByteArray() {
83 final byte[] data
= new byte[byteBuffer
.remaining()];
84 byteBuffer
.slice().get(data
);