本文整理匯總了Java中org.apache.thrift.TBase類的典型用法代碼示例。如果您正苦於以下問題:Java TBase類的具體用法?Java TBase怎麽用?Java TBase使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TBase類屬於org.apache.thrift包,在下文中一共展示了TBase類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: printTbase
import org.apache.thrift.TBase; //導入依賴的package包/類
/**
* Prints a TBase.
*
* @param t The object to print.
* @param depth The print nesting level.
* @return The pretty-printed version of the TBase.
*/
private static String printTbase(TBase t, int depth) {
List<String> fields = Lists.newArrayList();
for (Map.Entry<? extends TFieldIdEnum, FieldMetaData> entry :
FieldMetaData.getStructMetaDataMap(t.getClass()).entrySet()) {
@SuppressWarnings("unchecked")
boolean fieldSet = t.isSet(entry.getKey());
String strValue;
if (fieldSet) {
@SuppressWarnings("unchecked")
Object value = t.getFieldValue(entry.getKey());
strValue = printValue(value, depth);
} else {
strValue = "not set";
}
fields.add(tabs(depth) + entry.getValue().fieldName + ": " + strValue);
}
return Joiner.on("\n").join(fields);
}
示例2: printValue
import org.apache.thrift.TBase; //導入依賴的package包/類
/**
* Prints an object contained in a thrift message.
*
* @param o The object to print.
* @param depth The print nesting level.
* @return The pretty-printed version of the thrift field.
*/
private static String printValue(Object o, int depth) {
if (o == null) {
return "null";
} else if (TBase.class.isAssignableFrom(o.getClass())) {
return "\n" + printTbase((TBase) o, depth + 1);
} else if (Map.class.isAssignableFrom(o.getClass())) {
return printMap((Map) o, depth + 1);
} else if (List.class.isAssignableFrom(o.getClass())) {
return printList((List) o, depth + 1);
} else if (Set.class.isAssignableFrom(o.getClass())) {
return printSet((Set) o, depth + 1);
} else if (String.class == o.getClass()) {
return '"' + o.toString() + '"';
} else {
return o.toString();
}
}
示例3: getStructClass
import org.apache.thrift.TBase; //導入依賴的package包/類
public static <P extends TBase<P, ?>, C extends TBase<C, ?>, G extends TBase<G, ?>>
FieldGetter<P, G> compose(final FieldGetter<P, C> parent, final FieldGetter<C, G> child) {
return new FieldGetter<P, G>() {
@Override
public Class<P> getStructClass() {
return parent.getStructClass();
}
@Override
public Class<G> getValueClass() {
return child.getValueClass();
}
@Override
public Optional<G> apply(P input) {
Optional<C> parentValue = parent.apply(input);
if (parentValue.isPresent()) {
return child.apply(parentValue.get());
} else {
return Optional.absent();
}
}
};
}
示例4: deflateNonNull
import org.apache.thrift.TBase; //導入依賴的package包/類
/**
* Encodes a thrift object into a DEFLATE-compressed binary array.
*
* @param tBase Object to encode.
* @return Deflated, encoded object.
* @throws CodingException If the object could not be encoded.
*/
public static byte[] deflateNonNull(TBase<?, ?> tBase) throws CodingException {
requireNonNull(tBase);
// NOTE: Buffering is needed here for performance.
// There are actually 2 buffers in play here - the BufferedOutputStream prevents thrift from
// causing a call to deflate() on every encoded primitive. The DeflaterOutputStream buffer
// allows the underlying Deflater to operate on a larger chunk at a time without stopping to
// copy the intermediate compressed output to outBytes.
// See http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4986239
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
TTransport transport = new TIOStreamTransport(
new BufferedOutputStream(
new DeflaterOutputStream(outBytes, new Deflater(DEFLATE_LEVEL), DEFLATER_BUFFER_SIZE),
DEFLATER_BUFFER_SIZE));
try {
TProtocol protocol = PROTOCOL_FACTORY.getProtocol(transport);
tBase.write(protocol);
transport.close(); // calls finish() on the underlying stream, completing the compression
return outBytes.toByteArray();
} catch (TException e) {
throw new CodingException("Failed to serialize: " + tBase, e);
} finally {
transport.close();
}
}
示例5: inflateNonNull
import org.apache.thrift.TBase; //導入依賴的package包/類
/**
* Decodes a thrift object from a DEFLATE-compressed byte array into a target type.
*
* @param clazz Class to instantiate and deserialize to.
* @param buffer Compressed buffer to decode.
* @return A populated message.
* @throws CodingException If the message could not be decoded.
*/
public static <T extends TBase<T, ?>> T inflateNonNull(Class<T> clazz, byte[] buffer)
throws CodingException {
requireNonNull(clazz);
requireNonNull(buffer);
T tBase = newInstance(clazz);
TTransport transport = new TIOStreamTransport(
new InflaterInputStream(new ByteArrayInputStream(buffer)));
try {
TProtocol protocol = PROTOCOL_FACTORY.getProtocol(transport);
tBase.read(protocol);
return tBase;
} catch (TException e) {
throw new CodingException("Failed to deserialize: " + e, e);
} finally {
transport.close();
}
}
示例6: beforeWrite
import org.apache.thrift.TBase; //導入依賴的package包/類
@SuppressWarnings({ "rawtypes" })
@Override
public void beforeWrite(TMessage msg, TBase args, TBase result) {
// reuse message's buffer when write? yes, we use the pool.
ByteBuf readedBuf = message.getContent();
int refCount = readedBuf.refCnt();
if (refCount > 0) {
readedBuf.release(refCount);
}
// voidMethod's return message is very short
int initialCapacity = serverDef.trafficForecast.getInitBytesForWrite(msg.name);
// logger.debug("initialCapacity = {} , msg = {}",initialCapacity, msg);
ByteBuf buf = ctx.alloc().buffer(initialCapacity, serverDef.maxFrameSize);
message.setContent(buf).beforeWrite(ctx);
transport.setOutputBuffer(buf);
}
示例7: writeResult
import org.apache.thrift.TBase; //導入依賴的package包/類
@SuppressWarnings("rawtypes")
private void writeResult(final TProtocol out, final TMessage msg, final WriterHandler onComplete, TBase args,
final TBase result) {
try {
onComplete.beforeWrite(msg, args, result);
// if (!isOneway()) {
out.writeMessageBegin(new TMessage(msg.name, TMessageType.REPLY, msg.seqid));
if (result != null) {
result.write(out);
} else {
out.writeStructBegin(null);
out.writeFieldStop();
out.writeStructEnd();
}
out.writeMessageEnd();
out.getTransport().flush();
// }
onComplete.afterWrite(msg, null, TMessageType.REPLY, args, result);
} catch (Throwable e) {
onComplete.afterWrite(msg, e, TMessageType.EXCEPTION, args, result);
}
}
示例8: openThriftReader
import org.apache.thrift.TBase; //導入依賴的package包/類
/**
* This method will open the dictionary file stream for reading
*
* @throws IOException thrift reader open method throws IOException
*/
private void openThriftReader() throws IOException {
// initialise dictionary file reader which will return dictionary thrift object
// dictionary thrift object contains a list of byte buffer
if (null == dictionaryMetadataFileReader) {
dictionaryMetadataFileReader =
new ThriftReader(this.columnDictionaryMetadataFilePath, new ThriftReader.TBaseCreator() {
@Override public TBase create() {
return new ColumnDictionaryChunkMeta();
}
});
// Open it
dictionaryMetadataFileReader.open();
}
}
示例9: openThriftReader
import org.apache.thrift.TBase; //導入依賴的package包/類
/**
* This method will open the dictionary file stream for reading
*
* @throws IOException thrift reader open method throws IOException
*/
private void openThriftReader() throws IOException {
if (null == dictionaryFileReader) {
// initialise dictionary file reader which will return dictionary thrift object
// dictionary thrift object contains a list of byte buffer
dictionaryFileReader =
new ThriftReader(this.columnDictionaryFilePath, new ThriftReader.TBaseCreator() {
@Override public TBase create() {
return new ColumnDictionaryChunk();
}
});
// Open dictionary file reader
dictionaryFileReader.open();
}
}
示例10: cypherToken
import org.apache.thrift.TBase; //導入依賴的package包/類
public String cypherToken(TBase<?, ?> token, KeyStore keyStore) throws TException {
byte[] tokenAesKey = keyStore.getKey(KeyStore.AES_TOKEN);
byte[] tokenSipHashkey = keyStore.getKey(KeyStore.SIPHASH_TOKEN);
// Serialize the thrift token into byte array
byte[] serialized = serializer.serialize(token);
// Calculate the SIP
long sip = SipHashInline.hash24_palindromic(tokenSipHashkey, serialized);
//Create the token byte buffer
ByteBuffer buffer = ByteBuffer.allocate(8 + serialized.length);
// adds the sip
buffer.putLong(sip);
// adds the thrift token
buffer.put(serialized);
// Wrap the TOKEN
byte[] wrappedData = CryptoUtils.wrap(tokenAesKey, buffer.array());
String accessToken = new String(OrderPreservingBase64.encode(wrappedData));
return accessToken;
}
示例11: buildTransportRequest
import org.apache.thrift.TBase; //導入依賴的package包/類
private TransportRequest buildTransportRequest(
DefaultCall<?> call, Context ctx, String procedure, TBase reqBody)
throws ThriftEncodingException {
TSerializer serializer = new TSerializer(protocolFactory);
try {
return DefaultTransportRequest.builder()
.caller(clientConfig.getCaller())
.service(clientConfig.getService())
.procedure(procedure)
.encoding(ThriftEncoding.ENCODING)
.headers(call.getHeaders())
.deadline(ctx.getDeadline())
.span(ctx.getSpan())
.body(TransportBody.fromByteArray(serializer.serialize(reqBody)))
.build();
} catch (TException e) {
throw new ThriftEncodingException(e);
}
}
示例12: deserialize
import org.apache.thrift.TBase; //導入依賴的package包/類
/**
* Deserialize the Thrift object from a byte array.
*
* @param bytes The array to read from
*/
public TBase<?, ?> deserialize(byte[] bytes) throws TException {
try {
trans.reset(bytes);
Header header = readHeader();
final int validate = validate(header);
if (validate == HeaderUtils.OK) {
TBase<?, ?> base = locator.tBaseLookup(header.getType());
base.read(protocol);
return base;
}
if (validate == HeaderUtils.PASS_L4) {
return new L4Packet(header);
}
throw new IllegalStateException("invalid validate " + validate);
} finally {
trans.clear();
protocol.reset();
}
}
示例13: serialize
import org.apache.thrift.TBase; //導入依賴的package包/類
@Deprecated
public static void serialize(TSerializer serializer, TBase struct, DataOutput out)
throws IOException
{
assert serializer != null;
assert struct != null;
assert out != null;
byte[] bytes;
try
{
bytes = serializer.serialize(struct);
}
catch (TException e)
{
throw new RuntimeException(e);
}
out.writeInt(bytes.length);
out.write(bytes);
}
示例14: deserialize
import org.apache.thrift.TBase; //導入依賴的package包/類
@Deprecated
public static void deserialize(TDeserializer deserializer, TBase struct, DataInput in)
throws IOException
{
assert deserializer != null;
assert struct != null;
assert in != null;
byte[] bytes = new byte[in.readInt()];
in.readFully(bytes);
try
{
deserializer.deserialize(struct, bytes);
}
catch (TException ex)
{
throw new IOException(ex);
}
}
示例15: decodeBody
import org.apache.thrift.TBase; //導入依賴的package包/類
@Override
public @Nullable <T> T decodeBody(@NotNull ByteBuf arg3, @NotNull Class<T> bodyType) {
try {
// Create a new instance of type 'T'
T base = bodyType.newInstance();
// Get byte[] from ByteBuf
byte[] payloadBytes = new byte[arg3.readableBytes()];
arg3.readBytes(payloadBytes);
// Actually deserialize the payload
TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
deserializer.deserialize((TBase<?, ?>) base, payloadBytes);
return base;
} catch (InstantiationException | IllegalAccessException | TException e) {
logger.error("Failed to decode body to {}", bodyType.getName(), e);
}
return null;
}