blob: f62cd1c45eb560533b5e4ae016d7dc3814fa412b [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001# Protocol Buffers - Google's data interchange format
2# Copyright 2008 Google Inc. All rights reserved.
3# http://code.google.com/p/protobuf/
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""OutputStream is the primitive interface for sticking bits on the wire.
32
33All protocol buffer serialization can be expressed in terms of
34the OutputStream primitives provided here.
35"""
36
37__author__ = 'robinson@google.com (Will Robinson)'
38
39import array
40import struct
41from froofle.protobuf import message
42from froofle.protobuf.internal import wire_format
43
44
45
46# Note that much of this code is ported from //net/proto/ProtocolBuffer, and
47# that the interface is strongly inspired by CodedOutputStream from the C++
48# proto2 implementation.
49
50
51class OutputStream(object):
52
53 """Contains all logic for writing bits, and ToString() to get the result."""
54
55 def __init__(self):
56 self._buffer = array.array('B')
57
58 def AppendRawBytes(self, raw_bytes):
59 """Appends raw_bytes to our internal buffer."""
60 self._buffer.fromstring(raw_bytes)
61
62 def AppendLittleEndian32(self, unsigned_value):
63 """Appends an unsigned 32-bit integer to the internal buffer,
64 in little-endian byte order.
65 """
66 if not 0 <= unsigned_value <= wire_format.UINT32_MAX:
67 raise message.EncodeError(
68 'Unsigned 32-bit out of range: %d' % unsigned_value)
69 self._buffer.fromstring(struct.pack(
70 wire_format.FORMAT_UINT32_LITTLE_ENDIAN, unsigned_value))
71
72 def AppendLittleEndian64(self, unsigned_value):
73 """Appends an unsigned 64-bit integer to the internal buffer,
74 in little-endian byte order.
75 """
76 if not 0 <= unsigned_value <= wire_format.UINT64_MAX:
77 raise message.EncodeError(
78 'Unsigned 64-bit out of range: %d' % unsigned_value)
79 self._buffer.fromstring(struct.pack(
80 wire_format.FORMAT_UINT64_LITTLE_ENDIAN, unsigned_value))
81
82 def AppendVarint32(self, value):
83 """Appends a signed 32-bit integer to the internal buffer,
84 encoded as a varint. (Note that a negative varint32 will
85 always require 10 bytes of space.)
86 """
87 if not wire_format.INT32_MIN <= value <= wire_format.INT32_MAX:
88 raise message.EncodeError('Value out of range: %d' % value)
89 self.AppendVarint64(value)
90
91 def AppendVarUInt32(self, value):
92 """Appends an unsigned 32-bit integer to the internal buffer,
93 encoded as a varint.
94 """
95 if not 0 <= value <= wire_format.UINT32_MAX:
96 raise message.EncodeError('Value out of range: %d' % value)
97 self.AppendVarUInt64(value)
98
99 def AppendVarint64(self, value):
100 """Appends a signed 64-bit integer to the internal buffer,
101 encoded as a varint.
102 """
103 if not wire_format.INT64_MIN <= value <= wire_format.INT64_MAX:
104 raise message.EncodeError('Value out of range: %d' % value)
105 if value < 0:
106 value += (1 << 64)
107 self.AppendVarUInt64(value)
108
109 def AppendVarUInt64(self, unsigned_value):
110 """Appends an unsigned 64-bit integer to the internal buffer,
111 encoded as a varint.
112 """
113 if not 0 <= unsigned_value <= wire_format.UINT64_MAX:
114 raise message.EncodeError('Value out of range: %d' % unsigned_value)
115 while True:
116 bits = unsigned_value & 0x7f
117 unsigned_value >>= 7
118 if not unsigned_value:
119 self._buffer.append(bits)
120 break
121 self._buffer.append(0x80|bits)
122
123 def ToString(self):
124 """Returns a string containing the bytes in our internal buffer."""
125 return self._buffer.tostring()