com.facebook.swift.codec.internal.TProtocolWriter Maven / Gradle / Ivy
/*
* Copyright (C) 2012 Facebook, Inc.
*
* Licensed 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 com.facebook.swift.codec.internal;
import com.facebook.swift.codec.ThriftCodec;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TField;
import org.apache.thrift.protocol.TList;
import org.apache.thrift.protocol.TMap;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TSet;
import org.apache.thrift.protocol.TStruct;
import org.apache.thrift.protocol.TType;
import javax.annotation.concurrent.NotThreadSafe;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Set;
@NotThreadSafe
public class TProtocolWriter
{
private final TProtocol protocol;
public TProtocolWriter(TProtocol protocol)
{
this.protocol = protocol;
}
public void writeStructBegin(String name)
throws TException
{
protocol.writeStructBegin(new TStruct(name));
}
public void writeStructEnd()
throws TException
{
protocol.writeFieldStop();
protocol.writeStructEnd();
}
public void writeField(String name, short id, ThriftCodec codec, T value)
throws Exception
{
if (value == null) {
return;
}
protocol.writeFieldBegin(new TField(name, codec.getType().getProtocolType().getType(), id));
codec.write(value, protocol);
protocol.writeFieldEnd();
}
public void writeBinaryField(String name, short id, ByteBuffer buf)
throws TException
{
if (buf == null) {
return;
}
protocol.writeFieldBegin(new TField(name, TType.STRING, id));
protocol.writeBinary(buf);
protocol.writeFieldEnd();
}
public void writeBoolField(String name, short id, boolean b)
throws TException
{
protocol.writeFieldBegin(new TField(name, TType.BOOL, id));
protocol.writeBool(b);
protocol.writeFieldEnd();
}
public void writeByteField(String name, short id, byte b)
throws TException
{
protocol.writeFieldBegin(new TField(name, TType.BYTE, id));
protocol.writeByte(b);
protocol.writeFieldEnd();
}
public void writeDoubleField(String name, short id, double dub)
throws TException
{
protocol.writeFieldBegin(new TField(name, TType.DOUBLE, id));
protocol.writeDouble(dub);
protocol.writeFieldEnd();
}
public void writeI16Field(String name, short id, short i16)
throws TException
{
protocol.writeFieldBegin(new TField(name, TType.I16, id));
protocol.writeI16(i16);
protocol.writeFieldEnd();
}
public void writeI32Field(String name, short id, int i32)
throws TException
{
protocol.writeFieldBegin(new TField(name, TType.I32, id));
protocol.writeI32(i32);
protocol.writeFieldEnd();
}
public void writeI64Field(String name, short id, long i64)
throws TException
{
protocol.writeFieldBegin(new TField(name, TType.I64, id));
protocol.writeI64(i64);
protocol.writeFieldEnd();
}
public void writeStringField(String name, short id, String string)
throws TException
{
if (string == null) {
return;
}
protocol.writeFieldBegin(new TField(name, TType.STRING, id));
protocol.writeString(string);
protocol.writeFieldEnd();
}
public void writeStructField(String name, short id, ThriftCodec codec, T struct)
throws Exception
{
if (struct == null) {
return;
}
protocol.writeFieldBegin(new TField(name, TType.STRUCT, id));
codec.write(struct, protocol);
protocol.writeFieldEnd();
}
public void writeBoolArrayField(String name, short id, boolean[] array)
throws Exception
{
if (array == null) {
return;
}
protocol.writeFieldBegin(new TField(name, TType.LIST, id));
writeBoolArray(array);
protocol.writeFieldEnd();
}
public void writeI16ArrayField(String name, short id, short[] array)
throws Exception
{
if (array == null) {
return;
}
protocol.writeFieldBegin(new TField(name, TType.LIST, id));
writeI16Array(array);
protocol.writeFieldEnd();
}
public void writeI32ArrayField(String name, short id, int[] array)
throws Exception
{
if (array == null) {
return;
}
protocol.writeFieldBegin(new TField(name, TType.LIST, id));
writeI32Array(array);
protocol.writeFieldEnd();
}
public void writeI64ArrayField(String name, short id, long[] array)
throws Exception
{
if (array == null) {
return;
}
protocol.writeFieldBegin(new TField(name, TType.LIST, id));
writeI64Array(array);
protocol.writeFieldEnd();
}
public void writeDoubleArrayField(String name, short id, double[] array)
throws Exception
{
if (array == null) {
return;
}
protocol.writeFieldBegin(new TField(name, TType.LIST, id));
writeDoubleArray(array);
protocol.writeFieldEnd();
}
public void writeSetField(String name, short id, ThriftCodec> codec, Set set)
throws Exception
{
if (set == null) {
return;
}
protocol.writeFieldBegin(new TField(name, TType.SET, id));
codec.write(set, protocol);
protocol.writeFieldEnd();
}
public void writeListField(String name, short id, ThriftCodec> codec, List list)
throws Exception
{
if (list == null) {
return;
}
protocol.writeFieldBegin(new TField(name, TType.LIST, id));
codec.write(list, protocol);
protocol.writeFieldEnd();
}
public void writeMapField(String name, short id, ThriftCodec
© 2015 - 2025 Weber Informatics LLC | Privacy Policy