]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/transport/TByteBuffer.java
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / src / org / apache / thrift / transport / TByteBuffer.java
1 package org.apache.thrift.transport;
2
3 import java.nio.BufferOverflowException;
4 import java.nio.BufferUnderflowException;
5 import java.nio.ByteBuffer;
6
7 /**
8 * ByteBuffer-backed implementation of TTransport.
9 */
10 public final class TByteBuffer extends TTransport {
11 private final ByteBuffer byteBuffer;
12
13 /**
14 * Creates a new TByteBuffer wrapping a given NIO ByteBuffer.
15 */
16 public TByteBuffer(ByteBuffer byteBuffer) {
17 this.byteBuffer = byteBuffer;
18 }
19
20 @Override
21 public boolean isOpen() {
22 return true;
23 }
24
25 @Override
26 public void open() {
27 }
28
29 @Override
30 public void close() {
31 }
32
33 @Override
34 public int read(byte[] buf, int off, int len) throws TTransportException {
35 final int n = Math.min(byteBuffer.remaining(), len);
36 if (n > 0) {
37 try {
38 byteBuffer.get(buf, off, n);
39 } catch (BufferUnderflowException e) {
40 throw new TTransportException("Unexpected end of input buffer", e);
41 }
42 }
43 return n;
44 }
45
46 @Override
47 public void write(byte[] buf, int off, int len) throws TTransportException {
48 try {
49 byteBuffer.put(buf, off, len);
50 } catch (BufferOverflowException e) {
51 throw new TTransportException("Not enough room in output buffer", e);
52 }
53 }
54
55 /**
56 * Get the underlying NIO ByteBuffer.
57 */
58 public ByteBuffer getByteBuffer() {
59 return byteBuffer;
60 }
61
62 /**
63 * Convenience method to call clear() on the underlying NIO ByteBuffer.
64 */
65 public TByteBuffer clear() {
66 byteBuffer.clear();
67 return this;
68 }
69
70 /**
71 * Convenience method to call flip() on the underlying NIO ByteBuffer.
72 */
73 public TByteBuffer flip() {
74 byteBuffer.flip();
75 return this;
76 }
77
78 /**
79 * Convenience method to convert the underlying NIO ByteBuffer to a
80 * plain old byte array.
81 */
82 public byte[] toByteArray() {
83 final byte[] data = new byte[byteBuffer.remaining()];
84 byteBuffer.slice().get(data);
85 return data;
86 }
87 }