Created
August 29, 2011 17:16
-
-
Save sheki/1178864 to your computer and use it in GitHub Desktop.
TBase TSerializer Issue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [INFO] required: org.apache.thrift.TBase[_ <: org.apache.thrift.TBase, _ <: org.apache.thrift.TFieldIdEnum] | |
| [INFO] val itemBytes = new TSerializer().serialize(item) | |
| [INFO] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val item :Item = new Item | |
| val bytes = new TSerializer().serialize(item) //THROWS ABOVE GIVEN ERROR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Autogenerated by Thrift | |
| * | |
| * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING | |
| */ | |
| package flipkart.platform.queueinterface; | |
| import java.util.List; | |
| import java.util.ArrayList; | |
| import java.util.Map; | |
| import java.util.HashMap; | |
| import java.util.EnumMap; | |
| import java.util.Set; | |
| import java.util.HashSet; | |
| import java.util.EnumSet; | |
| import java.util.Collections; | |
| import java.util.BitSet; | |
| import java.nio.ByteBuffer; | |
| import java.util.Arrays; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| public class Item implements org.apache.thrift.TBase<Item, Item._Fields>, java.io.Serializable, Cloneable { | |
| private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("Item"); | |
| private static final org.apache.thrift.protocol.TField ITEM_ID_FIELD_DESC = new org.apache.thrift.protocol.TField("itemId", org.apache.thrift.protocol.TType.STRING, (short)1); | |
| private static final org.apache.thrift.protocol.TField DATA_FIELD_DESC = new org.apache.thrift.protocol.TField("data", org.apache.thrift.protocol.TType.STRING, (short)2); | |
| private static final org.apache.thrift.protocol.TField CREATION_TIME_FIELD_DESC = new org.apache.thrift.protocol.TField("creationTime", org.apache.thrift.protocol.TType.I64, (short)3); | |
| private static final org.apache.thrift.protocol.TField METADATA_FIELD_DESC = new org.apache.thrift.protocol.TField("metadata", org.apache.thrift.protocol.TType.STRING, (short)4); | |
| private static final org.apache.thrift.protocol.TField QUEUE_NAME_FIELD_DESC = new org.apache.thrift.protocol.TField("queueName", org.apache.thrift.protocol.TType.STRING, (short)5); | |
| public String itemId; | |
| public String data; | |
| public long creationTime; | |
| public String metadata; | |
| public String queueName; | |
| /** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */ | |
| public enum _Fields implements org.apache.thrift.TFieldIdEnum { | |
| ITEM_ID((short)1, "itemId"), | |
| DATA((short)2, "data"), | |
| CREATION_TIME((short)3, "creationTime"), | |
| METADATA((short)4, "metadata"), | |
| QUEUE_NAME((short)5, "queueName"); | |
| private static final Map<String, _Fields> byName = new HashMap<String, _Fields>(); | |
| static { | |
| for (_Fields field : EnumSet.allOf(_Fields.class)) { | |
| byName.put(field.getFieldName(), field); | |
| } | |
| } | |
| /** | |
| * Find the _Fields constant that matches fieldId, or null if its not found. | |
| */ | |
| public static _Fields findByThriftId(int fieldId) { | |
| switch(fieldId) { | |
| case 1: // ITEM_ID | |
| return ITEM_ID; | |
| case 2: // DATA | |
| return DATA; | |
| case 3: // CREATION_TIME | |
| return CREATION_TIME; | |
| case 4: // METADATA | |
| return METADATA; | |
| case 5: // QUEUE_NAME | |
| return QUEUE_NAME; | |
| default: | |
| return null; | |
| } | |
| } | |
| /** | |
| * Find the _Fields constant that matches fieldId, throwing an exception | |
| * if it is not found. | |
| */ | |
| public static _Fields findByThriftIdOrThrow(int fieldId) { | |
| _Fields fields = findByThriftId(fieldId); | |
| if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!"); | |
| return fields; | |
| } | |
| /** | |
| * Find the _Fields constant that matches name, or null if its not found. | |
| */ | |
| public static _Fields findByName(String name) { | |
| return byName.get(name); | |
| } | |
| private final short _thriftId; | |
| private final String _fieldName; | |
| _Fields(short thriftId, String fieldName) { | |
| _thriftId = thriftId; | |
| _fieldName = fieldName; | |
| } | |
| public short getThriftFieldId() { | |
| return _thriftId; | |
| } | |
| public String getFieldName() { | |
| return _fieldName; | |
| } | |
| } | |
| // isset id assignments | |
| private static final int __CREATIONTIME_ISSET_ID = 0; | |
| private BitSet __isset_bit_vector = new BitSet(1); | |
| public static final Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap; | |
| static { | |
| Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class); | |
| tmpMap.put(_Fields.ITEM_ID, new org.apache.thrift.meta_data.FieldMetaData("itemId", org.apache.thrift.TFieldRequirementType.DEFAULT, | |
| new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING))); | |
| tmpMap.put(_Fields.DATA, new org.apache.thrift.meta_data.FieldMetaData("data", org.apache.thrift.TFieldRequirementType.DEFAULT, | |
| new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING))); | |
| tmpMap.put(_Fields.CREATION_TIME, new org.apache.thrift.meta_data.FieldMetaData("creationTime", org.apache.thrift.TFieldRequirementType.DEFAULT, | |
| new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.I64))); | |
| tmpMap.put(_Fields.METADATA, new org.apache.thrift.meta_data.FieldMetaData("metadata", org.apache.thrift.TFieldRequirementType.DEFAULT, | |
| new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING))); | |
| tmpMap.put(_Fields.QUEUE_NAME, new org.apache.thrift.meta_data.FieldMetaData("queueName", org.apache.thrift.TFieldRequirementType.DEFAULT, | |
| new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING))); | |
| metaDataMap = Collections.unmodifiableMap(tmpMap); | |
| org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(Item.class, metaDataMap); | |
| } | |
| public Item() { | |
| } | |
| public Item( | |
| String itemId, | |
| String data, | |
| long creationTime, | |
| String metadata, | |
| String queueName) | |
| { | |
| this(); | |
| this.itemId = itemId; | |
| this.data = data; | |
| this.creationTime = creationTime; | |
| setCreationTimeIsSet(true); | |
| this.metadata = metadata; | |
| this.queueName = queueName; | |
| } | |
| /** | |
| * Performs a deep copy on <i>other</i>. | |
| */ | |
| public Item(Item other) { | |
| __isset_bit_vector.clear(); | |
| __isset_bit_vector.or(other.__isset_bit_vector); | |
| if (other.isSetItemId()) { | |
| this.itemId = other.itemId; | |
| } | |
| if (other.isSetData()) { | |
| this.data = other.data; | |
| } | |
| this.creationTime = other.creationTime; | |
| if (other.isSetMetadata()) { | |
| this.metadata = other.metadata; | |
| } | |
| if (other.isSetQueueName()) { | |
| this.queueName = other.queueName; | |
| } | |
| } | |
| public Item deepCopy() { | |
| return new Item(this); | |
| } | |
| @Override | |
| public void clear() { | |
| this.itemId = null; | |
| this.data = null; | |
| setCreationTimeIsSet(false); | |
| this.creationTime = 0; | |
| this.metadata = null; | |
| this.queueName = null; | |
| } | |
| public String getItemId() { | |
| return this.itemId; | |
| } | |
| public Item setItemId(String itemId) { | |
| this.itemId = itemId; | |
| return this; | |
| } | |
| public void unsetItemId() { | |
| this.itemId = null; | |
| } | |
| /** Returns true if field itemId is set (has been assigned a value) and false otherwise */ | |
| public boolean isSetItemId() { | |
| return this.itemId != null; | |
| } | |
| public void setItemIdIsSet(boolean value) { | |
| if (!value) { | |
| this.itemId = null; | |
| } | |
| } | |
| public String getData() { | |
| return this.data; | |
| } | |
| public Item setData(String data) { | |
| this.data = data; | |
| return this; | |
| } | |
| public void unsetData() { | |
| this.data = null; | |
| } | |
| /** Returns true if field data is set (has been assigned a value) and false otherwise */ | |
| public boolean isSetData() { | |
| return this.data != null; | |
| } | |
| public void setDataIsSet(boolean value) { | |
| if (!value) { | |
| this.data = null; | |
| } | |
| } | |
| public long getCreationTime() { | |
| return this.creationTime; | |
| } | |
| public Item setCreationTime(long creationTime) { | |
| this.creationTime = creationTime; | |
| setCreationTimeIsSet(true); | |
| return this; | |
| } | |
| public void unsetCreationTime() { | |
| __isset_bit_vector.clear(__CREATIONTIME_ISSET_ID); | |
| } | |
| /** Returns true if field creationTime is set (has been assigned a value) and false otherwise */ | |
| public boolean isSetCreationTime() { | |
| return __isset_bit_vector.get(__CREATIONTIME_ISSET_ID); | |
| } | |
| public void setCreationTimeIsSet(boolean value) { | |
| __isset_bit_vector.set(__CREATIONTIME_ISSET_ID, value); | |
| } | |
| public String getMetadata() { | |
| return this.metadata; | |
| } | |
| public Item setMetadata(String metadata) { | |
| this.metadata = metadata; | |
| return this; | |
| } | |
| public void unsetMetadata() { | |
| this.metadata = null; | |
| } | |
| /** Returns true if field metadata is set (has been assigned a value) and false otherwise */ | |
| public boolean isSetMetadata() { | |
| return this.metadata != null; | |
| } | |
| public void setMetadataIsSet(boolean value) { | |
| if (!value) { | |
| this.metadata = null; | |
| } | |
| } | |
| public String getQueueName() { | |
| return this.queueName; | |
| } | |
| public Item setQueueName(String queueName) { | |
| this.queueName = queueName; | |
| return this; | |
| } | |
| public void unsetQueueName() { | |
| this.queueName = null; | |
| } | |
| /** Returns true if field queueName is set (has been assigned a value) and false otherwise */ | |
| public boolean isSetQueueName() { | |
| return this.queueName != null; | |
| } | |
| public void setQueueNameIsSet(boolean value) { | |
| if (!value) { | |
| this.queueName = null; | |
| } | |
| } | |
| public void setFieldValue(_Fields field, Object value) { | |
| switch (field) { | |
| case ITEM_ID: | |
| if (value == null) { | |
| unsetItemId(); | |
| } else { | |
| setItemId((String)value); | |
| } | |
| break; | |
| case DATA: | |
| if (value == null) { | |
| unsetData(); | |
| } else { | |
| setData((String)value); | |
| } | |
| break; | |
| case CREATION_TIME: | |
| if (value == null) { | |
| unsetCreationTime(); | |
| } else { | |
| setCreationTime((Long)value); | |
| } | |
| break; | |
| case METADATA: | |
| if (value == null) { | |
| unsetMetadata(); | |
| } else { | |
| setMetadata((String)value); | |
| } | |
| break; | |
| case QUEUE_NAME: | |
| if (value == null) { | |
| unsetQueueName(); | |
| } else { | |
| setQueueName((String)value); | |
| } | |
| break; | |
| } | |
| } | |
| public Object getFieldValue(_Fields field) { | |
| switch (field) { | |
| case ITEM_ID: | |
| return getItemId(); | |
| case DATA: | |
| return getData(); | |
| case CREATION_TIME: | |
| return new Long(getCreationTime()); | |
| case METADATA: | |
| return getMetadata(); | |
| case QUEUE_NAME: | |
| return getQueueName(); | |
| } | |
| throw new IllegalStateException(); | |
| } | |
| /** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */ | |
| public boolean isSet(_Fields field) { | |
| if (field == null) { | |
| throw new IllegalArgumentException(); | |
| } | |
| switch (field) { | |
| case ITEM_ID: | |
| return isSetItemId(); | |
| case DATA: | |
| return isSetData(); | |
| case CREATION_TIME: | |
| return isSetCreationTime(); | |
| case METADATA: | |
| return isSetMetadata(); | |
| case QUEUE_NAME: | |
| return isSetQueueName(); | |
| } | |
| throw new IllegalStateException(); | |
| } | |
| @Override | |
| public boolean equals(Object that) { | |
| if (that == null) | |
| return false; | |
| if (that instanceof Item) | |
| return this.equals((Item)that); | |
| return false; | |
| } | |
| public boolean equals(Item that) { | |
| if (that == null) | |
| return false; | |
| boolean this_present_itemId = true && this.isSetItemId(); | |
| boolean that_present_itemId = true && that.isSetItemId(); | |
| if (this_present_itemId || that_present_itemId) { | |
| if (!(this_present_itemId && that_present_itemId)) | |
| return false; | |
| if (!this.itemId.equals(that.itemId)) | |
| return false; | |
| } | |
| boolean this_present_data = true && this.isSetData(); | |
| boolean that_present_data = true && that.isSetData(); | |
| if (this_present_data || that_present_data) { | |
| if (!(this_present_data && that_present_data)) | |
| return false; | |
| if (!this.data.equals(that.data)) | |
| return false; | |
| } | |
| boolean this_present_creationTime = true; | |
| boolean that_present_creationTime = true; | |
| if (this_present_creationTime || that_present_creationTime) { | |
| if (!(this_present_creationTime && that_present_creationTime)) | |
| return false; | |
| if (this.creationTime != that.creationTime) | |
| return false; | |
| } | |
| boolean this_present_metadata = true && this.isSetMetadata(); | |
| boolean that_present_metadata = true && that.isSetMetadata(); | |
| if (this_present_metadata || that_present_metadata) { | |
| if (!(this_present_metadata && that_present_metadata)) | |
| return false; | |
| if (!this.metadata.equals(that.metadata)) | |
| return false; | |
| } | |
| boolean this_present_queueName = true && this.isSetQueueName(); | |
| boolean that_present_queueName = true && that.isSetQueueName(); | |
| if (this_present_queueName || that_present_queueName) { | |
| if (!(this_present_queueName && that_present_queueName)) | |
| return false; | |
| if (!this.queueName.equals(that.queueName)) | |
| return false; | |
| } | |
| return true; | |
| } | |
| @Override | |
| public int hashCode() { | |
| return 0; | |
| } | |
| public int compareTo(Item other) { | |
| if (!getClass().equals(other.getClass())) { | |
| return getClass().getName().compareTo(other.getClass().getName()); | |
| } | |
| int lastComparison = 0; | |
| Item typedOther = (Item)other; | |
| lastComparison = Boolean.valueOf(isSetItemId()).compareTo(typedOther.isSetItemId()); | |
| if (lastComparison != 0) { | |
| return lastComparison; | |
| } | |
| if (isSetItemId()) { | |
| lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.itemId, typedOther.itemId); | |
| if (lastComparison != 0) { | |
| return lastComparison; | |
| } | |
| } | |
| lastComparison = Boolean.valueOf(isSetData()).compareTo(typedOther.isSetData()); | |
| if (lastComparison != 0) { | |
| return lastComparison; | |
| } | |
| if (isSetData()) { | |
| lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.data, typedOther.data); | |
| if (lastComparison != 0) { | |
| return lastComparison; | |
| } | |
| } | |
| lastComparison = Boolean.valueOf(isSetCreationTime()).compareTo(typedOther.isSetCreationTime()); | |
| if (lastComparison != 0) { | |
| return lastComparison; | |
| } | |
| if (isSetCreationTime()) { | |
| lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.creationTime, typedOther.creationTime); | |
| if (lastComparison != 0) { | |
| return lastComparison; | |
| } | |
| } | |
| lastComparison = Boolean.valueOf(isSetMetadata()).compareTo(typedOther.isSetMetadata()); | |
| if (lastComparison != 0) { | |
| return lastComparison; | |
| } | |
| if (isSetMetadata()) { | |
| lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.metadata, typedOther.metadata); | |
| if (lastComparison != 0) { | |
| return lastComparison; | |
| } | |
| } | |
| lastComparison = Boolean.valueOf(isSetQueueName()).compareTo(typedOther.isSetQueueName()); | |
| if (lastComparison != 0) { | |
| return lastComparison; | |
| } | |
| if (isSetQueueName()) { | |
| lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.queueName, typedOther.queueName); | |
| if (lastComparison != 0) { | |
| return lastComparison; | |
| } | |
| } | |
| return 0; | |
| } | |
| public _Fields fieldForId(int fieldId) { | |
| return _Fields.findByThriftId(fieldId); | |
| } | |
| public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException { | |
| org.apache.thrift.protocol.TField field; | |
| iprot.readStructBegin(); | |
| while (true) | |
| { | |
| field = iprot.readFieldBegin(); | |
| if (field.type == org.apache.thrift.protocol.TType.STOP) { | |
| break; | |
| } | |
| switch (field.id) { | |
| case 1: // ITEM_ID | |
| if (field.type == org.apache.thrift.protocol.TType.STRING) { | |
| this.itemId = iprot.readString(); | |
| } else { | |
| org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); | |
| } | |
| break; | |
| case 2: // DATA | |
| if (field.type == org.apache.thrift.protocol.TType.STRING) { | |
| this.data = iprot.readString(); | |
| } else { | |
| org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); | |
| } | |
| break; | |
| case 3: // CREATION_TIME | |
| if (field.type == org.apache.thrift.protocol.TType.I64) { | |
| this.creationTime = iprot.readI64(); | |
| setCreationTimeIsSet(true); | |
| } else { | |
| org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); | |
| } | |
| break; | |
| case 4: // METADATA | |
| if (field.type == org.apache.thrift.protocol.TType.STRING) { | |
| this.metadata = iprot.readString(); | |
| } else { | |
| org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); | |
| } | |
| break; | |
| case 5: // QUEUE_NAME | |
| if (field.type == org.apache.thrift.protocol.TType.STRING) { | |
| this.queueName = iprot.readString(); | |
| } else { | |
| org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); | |
| } | |
| break; | |
| default: | |
| org.apache.thrift.protocol.TProtocolUtil.skip(iprot, field.type); | |
| } | |
| iprot.readFieldEnd(); | |
| } | |
| iprot.readStructEnd(); | |
| // check for required fields of primitive type, which can't be checked in the validate method | |
| validate(); | |
| } | |
| public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException { | |
| validate(); | |
| oprot.writeStructBegin(STRUCT_DESC); | |
| if (this.itemId != null) { | |
| oprot.writeFieldBegin(ITEM_ID_FIELD_DESC); | |
| oprot.writeString(this.itemId); | |
| oprot.writeFieldEnd(); | |
| } | |
| if (this.data != null) { | |
| oprot.writeFieldBegin(DATA_FIELD_DESC); | |
| oprot.writeString(this.data); | |
| oprot.writeFieldEnd(); | |
| } | |
| oprot.writeFieldBegin(CREATION_TIME_FIELD_DESC); | |
| oprot.writeI64(this.creationTime); | |
| oprot.writeFieldEnd(); | |
| if (this.metadata != null) { | |
| oprot.writeFieldBegin(METADATA_FIELD_DESC); | |
| oprot.writeString(this.metadata); | |
| oprot.writeFieldEnd(); | |
| } | |
| if (this.queueName != null) { | |
| oprot.writeFieldBegin(QUEUE_NAME_FIELD_DESC); | |
| oprot.writeString(this.queueName); | |
| oprot.writeFieldEnd(); | |
| } | |
| oprot.writeFieldStop(); | |
| oprot.writeStructEnd(); | |
| } | |
| @Override | |
| public String toString() { | |
| StringBuilder sb = new StringBuilder("Item("); | |
| boolean first = true; | |
| sb.append("itemId:"); | |
| if (this.itemId == null) { | |
| sb.append("null"); | |
| } else { | |
| sb.append(this.itemId); | |
| } | |
| first = false; | |
| if (!first) sb.append(", "); | |
| sb.append("data:"); | |
| if (this.data == null) { | |
| sb.append("null"); | |
| } else { | |
| sb.append(this.data); | |
| } | |
| first = false; | |
| if (!first) sb.append(", "); | |
| sb.append("creationTime:"); | |
| sb.append(this.creationTime); | |
| first = false; | |
| if (!first) sb.append(", "); | |
| sb.append("metadata:"); | |
| if (this.metadata == null) { | |
| sb.append("null"); | |
| } else { | |
| sb.append(this.metadata); | |
| } | |
| first = false; | |
| if (!first) sb.append(", "); | |
| sb.append("queueName:"); | |
| if (this.queueName == null) { | |
| sb.append("null"); | |
| } else { | |
| sb.append(this.queueName); | |
| } | |
| first = false; | |
| sb.append(")"); | |
| return sb.toString(); | |
| } | |
| public void validate() throws org.apache.thrift.TException { | |
| // check for required fields | |
| } | |
| private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { | |
| try { | |
| write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out))); | |
| } catch (org.apache.thrift.TException te) { | |
| throw new java.io.IOException(te); | |
| } | |
| } | |
| private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException { | |
| try { | |
| // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor. | |
| __isset_bit_vector = new BitSet(1); | |
| read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in))); | |
| } catch (org.apache.thrift.TException te) { | |
| throw new java.io.IOException(te); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| package org.apache.thrift; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.UnsupportedEncodingException; | |
| import org.apache.thrift.protocol.TBinaryProtocol; | |
| import org.apache.thrift.protocol.TProtocol; | |
| import org.apache.thrift.protocol.TProtocolFactory; | |
| import org.apache.thrift.transport.TIOStreamTransport; | |
| /** | |
| * Generic utility for easily serializing objects into a byte array or Java | |
| * String. | |
| * | |
| */ | |
| public class TSerializer { | |
| /** | |
| * This is the byte array that data is actually serialized into | |
| */ | |
| private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream(); | |
| /** | |
| * This transport wraps that byte array | |
| */ | |
| private final TIOStreamTransport transport_ = new TIOStreamTransport(baos_); | |
| /** | |
| * Internal protocol used for serializing objects. | |
| */ | |
| private TProtocol protocol_; | |
| /** | |
| * Create a new TSerializer that uses the TBinaryProtocol by default. | |
| */ | |
| public TSerializer() { | |
| this(new TBinaryProtocol.Factory()); | |
| } | |
| /** | |
| * Create a new TSerializer. It will use the TProtocol specified by the | |
| * factory that is passed in. | |
| * | |
| * @param protocolFactory Factory to create a protocol | |
| */ | |
| public TSerializer(TProtocolFactory protocolFactory) { | |
| protocol_ = protocolFactory.getProtocol(transport_); | |
| } | |
| /** | |
| * Serialize the Thrift object into a byte array. The process is simple, | |
| * just clear the byte array output, write the object into it, and grab the | |
| * raw bytes. | |
| * | |
| * @param base The object to serialize | |
| * @return Serialized object in byte[] format | |
| */ | |
| public byte[] serialize(TBase base) throws TException { | |
| baos_.reset(); | |
| base.write(protocol_); | |
| return baos_.toByteArray(); | |
| } | |
| /** | |
| * Serialize the Thrift object into a Java string, using a specified | |
| * character set for encoding. | |
| * | |
| * @param base The object to serialize | |
| * @param charset Valid JVM charset | |
| * @return Serialized object as a String | |
| */ | |
| public String toString(TBase base, String charset) throws TException { | |
| try { | |
| return new String(serialize(base), charset); | |
| } catch (UnsupportedEncodingException uex) { | |
| throw new TException("JVM DOES NOT SUPPORT ENCODING: " + charset); | |
| } | |
| } | |
| /** | |
| * Serialize the Thrift object into a Java string, using the default JVM | |
| * charset encoding. | |
| * | |
| * @param base The object to serialize | |
| * @return Serialized object as a String | |
| */ | |
| public String toString(TBase base) throws TException { | |
| return new String(serialize(base)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment