Skip to content

Commit 8285332

Browse files
authored
RUBY-1977 Document and repair edge cases in ByteBuffer (#138)
* Revise ByteBuffer documentation * Reorder the methods * Document rewind in tutorial * Include C extension in documentation * C extension documentation * docs * Split byte buffer tests * Add empty buffer length test * Rewind test for write position * Move rewind test to general byte buffer file * put_bytes with null bytes test * put_bytes documentation * Move put_bytes tests next to put_byte * Document put_byte and prohibit strings of length != 1 * Rename BSON_TYPE_OBJECT to BSON_TYPE_DOCUMENT * Order types as they are listed in the bson spec * Putting partial strings is private helper * Bson string writing is also a private helper * put_string documentation * Move put_string tests * Add put_string with empty string test * Use pvt_put_byte * Number writing documentation * Do not duplicate docstrings * put_cstring docstring * Documentation for positions * Document to_s * put_decimal128 * Documentation and range checks in replace_int32 * More replace_int32 tests * put_symbol and null bytes * Fix signedness warning * Fix the length comparison again * put_hash and put_array docs * JRuby compatibility * JRuby implementation changes for replace_int32 * JRuby implementation changes for put_byte * JRuby 9.1 compatibility
1 parent e2ff5af commit 8285332

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

docs/tutorials/bson-v4.txt

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,22 @@ you wish to instantiate and passing it a ``BSON::ByteBuffer`` instance.
6868
String.from_bson(byte_buffer)
6969
BSON::Int32.from_bson(byte_buffer)
7070

71+
7172
Byte Buffers
7273
------------
7374

74-
In 4.0, BSON introduced the use of native byte buffers in both MRI and JRuby
75-
instead of using ``StringIO``. This was done for performance improvements.
75+
BSON library 4.0 introduces the use of native byte buffers in MRI and JRuby
76+
instead of using ``StringIO``, for improved performance.
7677

77-
API
78-
```
78+
Writing
79+
```````
7980

80-
A ``BSON::ByteBuffer`` can be instantiated with nothing (for write mode) or
81-
with a string of raw bytes (for read mode).
81+
To create a ``ByteBuffer`` for writing (i.e. serializing to BSON),
82+
instantiate ``BSON::ByteBuffer`` with no arguments:
8283

8384
.. code-block:: ruby
8485

8586
buffer = BSON::ByteBuffer.new # a write mode buffer.
86-
buffer = BSON::ByteBuffer.new(string) # a read mode buffer.
8787

8888
Writing to the buffer is done via the following API:
8989

@@ -99,6 +99,32 @@ Writing to the buffer is done via the following API:
9999
# writes the string to the buffer.
100100
buffer.put_cstring(value)
101101

102+
To obtain the serialized data as a byte string (for example, to send the data
103+
over a socket), call ``to_s`` on the buffer:
104+
105+
.. code-block:: ruby
106+
107+
buffer = BSON::ByteBuffer.new
108+
buffer.put_string('testing')
109+
socket.write(buffer.to_s)
110+
111+
.. note::
112+
113+
``ByteBuffer`` keeps track of read and write positions separately.
114+
There is no way to rewind the buffer for writing - ``rewind`` only affects
115+
the read position.
116+
117+
118+
Reading
119+
```````
120+
121+
To create a ``ByteBuffer`` for reading (i.e. deserializing from BSON),
122+
instantiate ``BSON::ByteBuffer`` with a byte string as the argument:
123+
124+
.. code-block:: ruby
125+
126+
buffer = BSON::ByteBuffer.new(string) # a read mode buffer.
127+
102128
Reading from the buffer is done via the following API:
103129

104130
.. code-block:: ruby
@@ -111,19 +137,22 @@ Reading from the buffer is done via the following API:
111137
buffer.get_int64 # Pulls a 64-bit integer (8 bytes) from the buffer.
112138
buffer.get_string # Pulls a UTF-8 string from the buffer.
113139

114-
Converting a buffer to its raw bytes (for example, for sending over a socket)
115-
is done by simply calling ``to_s`` on the buffer.
140+
To restart reading from the beginning of a buffer, use ``rewind``:
116141

117142
.. code-block:: ruby
118143

119-
buffer = BSON::ByteBuffer.new
120-
buffer.put_string('testing')
121-
socket.write(buffer.to_s)
144+
buffer.rewind
145+
146+
.. note::
147+
148+
``ByteBuffer`` keeps track of read and write positions separately.
149+
``rewind`` only affects the read position.
150+
122151

123-
Supported Objects
152+
Supported Classes
124153
-----------------
125154

126-
Core Ruby objects that have representations in the BSON specification and
155+
Core Ruby classes that have representations in the BSON specification and
127156
will have a ``to_bson`` method defined for them are: ``Object``, ``Array``,
128157
``FalseClass``, ``Float``, ``Hash``, ``Integer``, ``NilClass``, ``Regexp``,
129158
``String``, ``Symbol`` (deprecated), ``Time``, ``TrueClass``.

0 commit comments

Comments
 (0)