public void write(final TBase<?, ?> base) throws TException { final TBaseStreamNode node = new TBaseStreamNode(transport); node.setClassName(base.getClass().getName()); node.setBeginPosition(transport.getBufferPosition()); final TProtocol protocol = protocolFactory.getProtocol(transport); base.write(protocol); node.setEndPosition(transport.getBufferPosition()); nodes.add(node); }
/** Write the object to disk. */ public void read(TBase<?, ?> t) throws IOException { try { t.read(binaryProtocol); } catch (TException e) { throw new IOException(e); } }
public static void testSerialization(TProtocolFactory factory, TBase object) throws Exception { TTransport trans = new TTransport() { public void write(byte[] bin, int x, int y) throws TTransportException {} public int read(byte[] bin, int x, int y) throws TTransportException { return 0; } public void close() {} public void open() {} public boolean isOpen() { return true; } }; TProtocol proto = factory.getProtocol(trans); long startTime = System.currentTimeMillis(); for (int i = 0; i < HOW_MANY; i++) { object.write(proto); } long endTime = System.currentTimeMillis(); System.out.println("Serialization test time: " + (endTime - startTime) + " ms"); }
boolean setException(TBase<?, TFieldIdEnum> result, Throwable cause) { Class<?> causeType = cause.getClass(); for (Entry<Class<Throwable>, TFieldIdEnum> e : exceptionFields.entrySet()) { if (e.getKey().isAssignableFrom(causeType)) { result.setFieldValue(e.getValue(), cause); return true; } } return false; }
@SuppressWarnings("unchecked") private <T extends TBase<?, ?>> T fromBytes(byte[] data, Class<T> clazz) { try { if (data == null) { return null; } TBase<?, ?> base = clazz.newInstance(); TMemoryInputTransport trans = new TMemoryInputTransport(data); TJSONProtocol protocol = new TJSONProtocol(trans); base.read(protocol); trans.close(); return (T) base; } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (TException e) { throw new RuntimeException(e); } }
@Override public void serialize(TBase value, JsonGenerator gen, SerializerProvider provider) throws IOException { gen.writeRawValue( writeThriftObjectAsTText( protocol -> { try { value.write(protocol); } catch (TException ex) { throw new IllegalArgumentException(ex); } })); }
/** * Builds Thrift object from the raw bytes returned by RCFile reader. * * @throws TException */ @SuppressWarnings({"unchecked", "rawtypes"}) public TBase<?, ?> getCurrentThriftValue() throws IOException, InterruptedException, TException { BytesRefArrayWritable byteRefs = getCurrentBytesRefArrayWritable(); if (byteRefs == null) { return null; } TBase tObj = tDesc.newThriftObject(); for (int i = 0; i < knownRequiredFields.size(); i++) { BytesRefWritable buf = byteRefs.get(columnsBeingRead.get(i)); if (buf.getLength() > 0) { memTransport.reset(buf.getData(), buf.getStart(), buf.getLength()); Field field = knownRequiredFields.get(i); tObj.setFieldValue(field.getFieldIdEnum(), ThriftUtils.readFieldNoTag(tProto, field)); } // else no need to set default value since any default value // would have been serialized when this record was written. } // parse unknowns column if required if (readUnknownsColumn) { int last = columnsBeingRead.get(columnsBeingRead.size() - 1); BytesRefWritable buf = byteRefs.get(last); if (buf.getLength() > 0) { memTransport.reset(buf.getData(), buf.getStart(), buf.getLength()); tObj.read(tProto); } } return tObj; }
void setSuccess(TBase<?, TFieldIdEnum> result, Object value) { if (successField != null) { result.setFieldValue(successField, value); } }
private String getMethodArgs(TBase<?, ?> args) { return StringUtils.drop(args.toString(), 256); }