in thrift/lib/javadeprecated/src/main/java/com/facebook/thrift/transport/THeaderTransport.java [557:693]
public void flushImpl(boolean oneway) throws TTransportException {
try {
// Check if this is a TApplicationException
TApplicationException tae = null;
byte[] buf = writeBuffer_.get();
int len = writeBuffer_.len();
if (len >= 2
&& buf[0] == TCompactProtocol.PROTOCOL_ID
&& ((buf[1] >> TCompactProtocol.TYPE_SHIFT_AMOUNT) & 0x03) == TMessageType.EXCEPTION) {
// Compact
TCompactProtocol proto = new TCompactProtocol(new TMemoryInputTransport(buf));
@SuppressWarnings("unused")
TMessage msg = proto.readMessageBegin();
tae = TApplicationException.read(proto);
} else if (len >= 4
&& ((buf[0] << 24) | (buf[1] << 16)) == TBinaryProtocol.VERSION_1
&& buf[3] == TMessageType.EXCEPTION) {
// Binary
TBinaryProtocol proto = new TBinaryProtocol(new TMemoryInputTransport(buf));
@SuppressWarnings("unused")
TMessage msg = proto.readMessageBegin();
tae = TApplicationException.read(proto);
}
if (tae != null) {
if (!writeHeaders.containsKey("uex")) {
writeHeaders.put("uex", "TApplicationException");
}
if (!writeHeaders.containsKey("uexw")) {
writeHeaders.put("uexw", tae.getMessage() == null ? "[null]" : tae.getMessage());
}
}
} catch (TException e) {
// Failed parsing a TApplicationException, so don't write headers
}
ByteBuffer frame = ByteBuffer.wrap(writeBuffer_.get());
frame.limit(writeBuffer_.len());
writeBuffer_.reset();
if (clientType == ClientTypes.HEADERS) {
frame = transform(frame);
}
if (frame.remaining() > MAX_FRAME_SIZE) {
throw new TTransportException(
"Attempting to send frame that is "
+ "too large: "
+ Integer.toString(frame.remaining()));
}
if (protoId == T_JSON_PROTOCOL && clientType != ClientTypes.HTTP) {
throw new TTransportException("Trying to send JSON encoding" + " over binary");
}
if (clientType == ClientTypes.HEADERS) {
// Each varint could be up to 5 in size.
ByteBuffer transformData = ByteBuffer.allocate(writeTransforms.size() * 5);
// For now, no transforms require data.
int numTransforms = writeTransforms.size();
for (Transforms trans : writeTransforms) {
writeVarint(transformData, trans.getValue());
}
transformData.limit(transformData.position());
transformData.position(0);
if (firstRequest) {
firstRequest = false;
writeHeaders.put(CLIENT_METADATA_HEADER, CLIENT_METADATA);
}
if (identity != null && identity.length() > 0) {
writeHeaders.put(ID_VERSION_HEADER, ID_VERSION);
writeHeaders.put(IDENTITY_HEADER, identity);
}
ByteBuffer infoData1 = flushInfoHeaders(Infos.INFO_PKEYVALUE, writePersistentHeaders);
ByteBuffer infoData2 = flushInfoHeaders(Infos.INFO_KEYVALUE, writeHeaders);
ByteBuffer headerData = ByteBuffer.allocate(10);
writeVarint(headerData, protoId);
writeVarint(headerData, numTransforms);
headerData.limit(headerData.position());
headerData.position(0);
int headerSize =
transformData.remaining()
+ infoData1.remaining()
+ infoData2.remaining()
+ headerData.remaining();
int paddingSize = 4 - headerSize % 4;
headerSize += paddingSize;
// Allocate buffer for the headers.
// 14 bytes for sz, magic , flags , seqId , headerSize
ByteBuffer out = ByteBuffer.allocate(headerSize + 14);
// See thrift/doc/HeaderFormat.txt for more info on wire format
encodeInt(out, 10 + headerSize + frame.remaining());
encodeShort(out, HEADER_MAGIC >> 16);
encodeShort(out, flags);
encodeInt(out, seqId);
encodeShort(out, headerSize / 4);
out.put(headerData);
out.put(transformData);
out.put(infoData1);
out.put(infoData2);
// There are no info headers for this version
// Pad out the header with 0x00
for (int i = 0; i < paddingSize; i++) {
out.put((byte) 0x00);
}
out.position(0);
transport_.write(out.array(), out.position(), out.remaining());
transport_.write(frame.array(), frame.position(), frame.remaining());
} else if (clientType == ClientTypes.FRAMED_DEPRECATED) {
ByteBuffer out = ByteBuffer.allocate(4);
encodeInt(out, frame.remaining());
out.position(0);
transport_.write(out.array(), out.position(), out.remaining());
transport_.write(frame.array(), frame.position(), frame.remaining());
} else if (clientType == ClientTypes.HTTP) {
throw new TTransportException("HTTP is unimplemented in this language");
} else {
throw new TTransportException("Unknown client type on send");
}
if (oneway) {
transport_.onewayFlush();
} else {
transport_.flush();
}
}