TBinarySerializer.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. namespace Thrift\Serializer;
  24. use Thrift\Transport\TMemoryBuffer;
  25. use Thrift\Protocol\TBinaryProtocolAccelerated;
  26. use Thrift\Type\TMessageType;
  27. /**
  28. * Utility class for serializing and deserializing
  29. * a thrift object using TBinaryProtocolAccelerated.
  30. */
  31. class TBinarySerializer
  32. {
  33. // NOTE(rmarin): Because thrift_protocol_write_binary
  34. // adds a begin message prefix, you cannot specify
  35. // a transport in which to serialize an object. It has to
  36. // be a string. Otherwise we will break the compatibility with
  37. // normal deserialization.
  38. public static function serialize($object)
  39. {
  40. $transport = new TMemoryBuffer();
  41. $protocol = new TBinaryProtocolAccelerated($transport);
  42. if (function_exists('thrift_protocol_write_binary')) {
  43. thrift_protocol_write_binary($protocol, $object->getName(),
  44. TMessageType::REPLY, $object,
  45. 0, $protocol->isStrictWrite());
  46. $protocol->readMessageBegin($unused_name, $unused_type,
  47. $unused_seqid);
  48. } else {
  49. $object->write($protocol);
  50. }
  51. $protocol->getTransport()->flush();
  52. return $transport->getBuffer();
  53. }
  54. public static function deserialize($string_object, $class_name, $buffer_size = 8192)
  55. {
  56. $transport = new TMemoryBuffer();
  57. $protocol = new TBinaryProtocolAccelerated($transport);
  58. if (function_exists('thrift_protocol_read_binary')) {
  59. // NOTE (t.heintz) TBinaryProtocolAccelerated internally wraps our TMemoryBuffer in a
  60. // TBufferedTransport, so we have to retrieve it again or risk losing data when writing
  61. // less than 512 bytes to the transport (see the comment there as well).
  62. // @see THRIFT-1579
  63. $protocol->writeMessageBegin('', TMessageType::REPLY, 0);
  64. $protocolTransport = $protocol->getTransport();
  65. $protocolTransport->write($string_object);
  66. $protocolTransport->flush();
  67. return thrift_protocol_read_binary($protocol, $class_name,
  68. $protocol->isStrictRead(),
  69. $buffer_size);
  70. } else {
  71. $transport->write($string_object);
  72. $object = new $class_name();
  73. $object->read($protocol);
  74. return $object;
  75. }
  76. }
  77. }