]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/transport/TMemoryBuffer.java
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / src / org / apache / thrift / transport / TMemoryBuffer.java
CommitLineData
f67539c2
TL
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package org.apache.thrift.transport;
21
22import org.apache.thrift.TByteArrayOutputStream;
23import java.nio.charset.Charset;
24
25/**
26 * Memory buffer-based implementation of the TTransport interface.
27 */
28public class TMemoryBuffer extends TTransport {
29 /**
30 * Create a TMemoryBuffer with an initial buffer size of <i>size</i>. The
31 * internal buffer will grow as necessary to accommodate the size of the data
32 * being written to it.
33 *
34 * @param size the initial size of the buffer
35 */
36 public TMemoryBuffer(int size) {
37 arr_ = new TByteArrayOutputStream(size);
38 }
39
40 @Override
41 public boolean isOpen() {
42 return true;
43 }
44
45 @Override
46 public void open() {
47 /* Do nothing */
48 }
49
50 @Override
51 public void close() {
52 /* Do nothing */
53 }
54
55 @Override
56 public int read(byte[] buf, int off, int len) {
57 byte[] src = arr_.get();
58 int amtToRead = (len > arr_.len() - pos_ ? arr_.len() - pos_ : len);
59 if (amtToRead > 0) {
60 System.arraycopy(src, pos_, buf, off, amtToRead);
61 pos_ += amtToRead;
62 }
63 return amtToRead;
64 }
65
66 @Override
67 public void write(byte[] buf, int off, int len) {
68 arr_.write(buf, off, len);
69 }
70
71 /**
72 * Output the contents of the memory buffer as a String, using the supplied
73 * encoding
74 * @param charset the encoding to use
75 * @return the contents of the memory buffer as a String
76 */
77 public String toString(Charset charset) {
78 return arr_.toString(charset);
79 }
80
81 public String inspect() {
82 StringBuilder buf = new StringBuilder();
83 byte[] bytes = arr_.toByteArray();
84 for (int i = 0; i < bytes.length; i++) {
85 buf.append(pos_ == i ? "==>" : "" ).append(Integer.toHexString(bytes[i] & 0xff)).append(" ");
86 }
87 return buf.toString();
88 }
89
90 // The contents of the buffer
91 private TByteArrayOutputStream arr_;
92
93 // Position to read next byte from
94 private int pos_;
95
96 public int length() {
97 return arr_.size();
98 }
99
100 public byte[] getArray() {
101 return arr_.get();
102 }
103}
104