]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/php/lib/Serializer/TBinarySerializer.php
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / php / lib / Serializer / TBinarySerializer.php
1 <?php
2 /*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 * @package thrift.protocol
21 * @author: rmarin (marin.radu@facebook.com)
22 */
23
24 namespace Thrift\Serializer;
25
26 use Thrift\Transport\TMemoryBuffer;
27 use Thrift\Protocol\TBinaryProtocolAccelerated;
28 use Thrift\Type\TMessageType;
29
30 /**
31 * Utility class for serializing and deserializing
32 * a thrift object using TBinaryProtocolAccelerated.
33 */
34 class TBinarySerializer
35 {
36 // NOTE(rmarin): Because thrift_protocol_write_binary
37 // adds a begin message prefix, you cannot specify
38 // a transport in which to serialize an object. It has to
39 // be a string. Otherwise we will break the compatibility with
40 // normal deserialization.
41 public static function serialize($object)
42 {
43 $transport = new TMemoryBuffer();
44 $protocol = new TBinaryProtocolAccelerated($transport);
45 if (function_exists('thrift_protocol_write_binary')) {
46 thrift_protocol_write_binary(
47 $protocol,
48 $object->getName(),
49 TMessageType::REPLY,
50 $object,
51 0,
52 $protocol->isStrictWrite()
53 );
54
55 $protocol->readMessageBegin($unused_name, $unused_type, $unused_seqid);
56 } else {
57 $object->write($protocol);
58 }
59 $protocol->getTransport()->flush();
60
61 return $transport->getBuffer();
62 }
63
64 public static function deserialize($string_object, $class_name, $buffer_size = 8192)
65 {
66 $transport = new TMemoryBuffer();
67 $protocol = new TBinaryProtocolAccelerated($transport);
68 if (function_exists('thrift_protocol_read_binary')) {
69 // NOTE (t.heintz) TBinaryProtocolAccelerated internally wraps our TMemoryBuffer in a
70 // TBufferedTransport, so we have to retrieve it again or risk losing data when writing
71 // less than 512 bytes to the transport (see the comment there as well).
72 // @see THRIFT-1579
73 $protocol->writeMessageBegin('', TMessageType::REPLY, 0);
74 $protocolTransport = $protocol->getTransport();
75 $protocolTransport->write($string_object);
76 $protocolTransport->flush();
77
78 return thrift_protocol_read_binary($protocol, $class_name, $protocol->isStrictRead(), $buffer_size);
79 } else {
80 $transport->write($string_object);
81 $object = new $class_name();
82 $object->read($protocol);
83
84 return $object;
85 }
86 }
87 }