summaryrefslogtreecommitdiff
path: root/python/google/protobuf/internal/output_stream.py
blob: ccb91225eb696cbd315cd00e7e3e1b8e8ee04630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Protocol Buffers - Google's data interchange format
# Copyright 2008 Google Inc.
# http://code.google.com/p/protobuf/
#
# 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.

"""OutputStream is the primitive interface for sticking bits on the wire.

All protocol buffer serialization can be expressed in terms of
the OutputStream primitives provided here.
"""

__author__ = 'robinson@google.com (Will Robinson)'

import array
import struct
from google.protobuf import message
from google.protobuf.internal import wire_format



# Note that much of this code is ported from //net/proto/ProtocolBuffer, and
# that the interface is strongly inspired by CodedOutputStream from the C++
# proto2 implementation.


class OutputStream(object):

  """Contains all logic for writing bits, and ToString() to get the result."""

  def __init__(self):
    self._buffer = array.array('B')

  def AppendRawBytes(self, raw_bytes):
    """Appends raw_bytes to our internal buffer."""
    self._buffer.fromstring(raw_bytes)

  def AppendLittleEndian32(self, unsigned_value):
    """Appends an unsigned 32-bit integer to the internal buffer,
    in little-endian byte order.
    """
    if not 0 <= unsigned_value <= wire_format.UINT32_MAX:
      raise message.EncodeError(
          'Unsigned 32-bit out of range: %d' % unsigned_value)
    self._buffer.fromstring(struct.pack(
        wire_format.FORMAT_UINT32_LITTLE_ENDIAN, unsigned_value))

  def AppendLittleEndian64(self, unsigned_value):
    """Appends an unsigned 64-bit integer to the internal buffer,
    in little-endian byte order.
    """
    if not 0 <= unsigned_value <= wire_format.UINT64_MAX:
      raise message.EncodeError(
          'Unsigned 64-bit out of range: %d' % unsigned_value)
    self._buffer.fromstring(struct.pack(
        wire_format.FORMAT_UINT64_LITTLE_ENDIAN, unsigned_value))

  def AppendVarint32(self, value):
    """Appends a signed 32-bit integer to the internal buffer,
    encoded as a varint.  (Note that a negative varint32 will
    always require 10 bytes of space.)
    """
    if not wire_format.INT32_MIN <= value <= wire_format.INT32_MAX:
      raise message.EncodeError('Value out of range: %d' % value)
    self.AppendVarint64(value)

  def AppendVarUInt32(self, value):
    """Appends an unsigned 32-bit integer to the internal buffer,
    encoded as a varint.
    """
    if not 0 <= value <= wire_format.UINT32_MAX:
      raise message.EncodeError('Value out of range: %d' % value)
    self.AppendVarUInt64(value)

  def AppendVarint64(self, value):
    """Appends a signed 64-bit integer to the internal buffer,
    encoded as a varint.
    """
    if not wire_format.INT64_MIN <= value <= wire_format.INT64_MAX:
      raise message.EncodeError('Value out of range: %d' % value)
    if value < 0:
      value += (1 << 64)
    self.AppendVarUInt64(value)

  def AppendVarUInt64(self, unsigned_value):
    """Appends an unsigned 64-bit integer to the internal buffer,
    encoded as a varint.
    """
    if not 0 <= unsigned_value <= wire_format.UINT64_MAX:
      raise message.EncodeError('Value out of range: %d' % unsigned_value)
    while True:
      bits = unsigned_value & 0x7f
      unsigned_value >>= 7
      if not unsigned_value:
        self._buffer.append(bits)
        break
      self._buffer.append(0x80|bits)

  def ToString(self):
    """Returns a string containing the bytes in our internal buffer."""
    return self._buffer.tostring()