| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727 |
- /* Copyright 2010-2016 MongoDB Inc.
- *
- * 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.
- */
- using System;
- using System.IO;
- using System.Text;
- namespace MongoDB.Bson.IO
- {
- /// <summary>
- /// Represents a Stream backed by an IByteBuffer. Similar to MemoryStream but backed by an IByteBuffer
- /// instead of a byte array and also implements the BsonStream interface for higher performance BSON I/O.
- /// </summary>
- public class ByteBufferStream : BsonStream
- {
- // private fields
- private IByteBuffer _buffer;
- private bool _disposed;
- private int _length;
- private readonly bool _ownsBuffer;
- private int _position;
- private readonly byte[] _temp = new byte[12];
- private readonly byte[] _tempUtf8 = new byte[128];
- // constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="ByteBufferStream"/> class.
- /// </summary>
- /// <param name="buffer">The buffer.</param>
- /// <param name="ownsBuffer">Whether the stream owns the buffer and should Dispose it when done.</param>
- public ByteBufferStream(IByteBuffer buffer, bool ownsBuffer = false)
- {
- if (buffer == null)
- {
- throw new ArgumentNullException("buffer");
- }
- _buffer = buffer;
- _ownsBuffer = ownsBuffer;
- _length = buffer.Length;
- }
- // public properties
- /// <summary>
- /// Gets the buffer.
- /// </summary>
- /// <value>
- /// The buffer.
- /// </value>
- public IByteBuffer Buffer
- {
- get
- {
- ThrowIfDisposed();
- return _buffer;
- }
- }
- /// <inheritdoc/>
- public override bool CanRead
- {
- get { return !_disposed; }
- }
- /// <inheritdoc/>
- public override bool CanSeek
- {
- get { return !_disposed; }
- }
- /// <inheritdoc/>
- public override bool CanTimeout
- {
- get { return false; }
- }
- /// <inheritdoc/>
- public override bool CanWrite
- {
- get { return !_disposed && !_buffer.IsReadOnly; }
- }
- /// <inheritdoc/>
- public override long Length
- {
- get
- {
- ThrowIfDisposed();
- return _length;
- }
- }
- /// <inheritdoc/>
- public override long Position
- {
- get
- {
- ThrowIfDisposed();
- return _position;
- }
- set
- {
- if (value < 0 || value > int.MaxValue)
- {
- throw new ArgumentOutOfRangeException("value");
- }
- ThrowIfDisposed();
- _position = (int)value;
- }
- }
- // public methods
- /// <inheritdoc/>
- public override void Flush()
- {
- ThrowIfDisposed();
- // do nothing
- }
- /// <inheritdoc/>
- public override int Read(byte[] buffer, int offset, int count)
- {
- if (buffer == null)
- {
- throw new ArgumentNullException("buffer");
- }
- if (offset < 0 || offset > buffer.Length)
- {
- throw new ArgumentOutOfRangeException("offset");
- }
- if (count < 0 || offset + count > buffer.Length)
- {
- throw new ArgumentOutOfRangeException("count");
- }
- ThrowIfDisposed();
- if (_position >= _length)
- {
- return 0;
- }
- var available = _length - _position;
- if (count > available)
- {
- count = available;
- }
- _buffer.GetBytes(_position, buffer, offset, count);
- _position += count;
- return count;
- }
- /// <inheritdoc/>
- public override int ReadByte()
- {
- ThrowIfDisposed();
- if (_position >= _length)
- {
- return -1;
- }
- return _buffer.GetByte(_position++);
- }
- /// <inheritdoc/>
- public override long Seek(long offset, SeekOrigin origin)
- {
- ThrowIfDisposed();
- long position;
- switch (origin)
- {
- case SeekOrigin.Begin: position = offset; break;
- case SeekOrigin.Current: position = _position + offset; break;
- case SeekOrigin.End: position = _length + offset; break;
- default: throw new ArgumentException("Invalid origin.", "origin");
- }
- if (position < 0)
- {
- throw new IOException("Attempted to seek before the beginning of the stream.");
- }
- if (position > int.MaxValue)
- {
- throw new IOException("Attempted to seek beyond the maximum value that can be represented using 32 bits.");
- }
- _position = (int)position;
- return position;
- }
- /// <inheritdoc/>
- public override void SetLength(long value)
- {
- if (value < 0 || value > int.MaxValue)
- {
- throw new ArgumentOutOfRangeException("value");
- }
- ThrowIfDisposed();
- EnsureWriteable();
- _buffer.EnsureCapacity((int)value);
- _length = (int)value;
- if (_position > _length)
- {
- _position = _length;
- }
- }
- /// <inheritdoc/>
- public override void Write(byte[] buffer, int offset, int count)
- {
- if (buffer == null)
- {
- throw new ArgumentNullException("buffer");
- }
- if (offset < 0 || offset > buffer.Length)
- {
- throw new ArgumentOutOfRangeException("offset");
- }
- if (count < 0 || offset + count > buffer.Length)
- {
- throw new ArgumentOutOfRangeException("count");
- }
- ThrowIfDisposed();
- EnsureWriteable();
- PrepareToWrite(count);
- _buffer.SetBytes(_position, buffer, offset, count);
- SetPositionAfterWrite(_position + count);
- }
- /// <inheritdoc/>
- public override void WriteByte(byte value)
- {
- ThrowIfDisposed();
- PrepareToWrite(1);
- _buffer.SetByte(_position, value);
- SetPositionAfterWrite(_position + 1);
- }
- // protected methods
- /// <inheritdoc/>
- protected override void Dispose(bool disposing)
- {
- if (!_disposed)
- {
- if (_ownsBuffer)
- {
- _buffer.Dispose();
- }
- _disposed = true;
- }
- base.Dispose(disposing);
- }
- // private methods
- private void EnsureWriteable()
- {
- if (!CanWrite)
- {
- throw new NotSupportedException("Stream is not writeable.");
- }
- }
- private int FindNullByte()
- {
- var position = _position;
- while (position < _length)
- {
- var segment = _buffer.AccessBackingBytes(position);
- var endOfSegmentIndex = segment.Offset + segment.Count;
- for (var index = segment.Offset; index < endOfSegmentIndex; index++)
- {
- if (segment.Array[index] == 0)
- {
- return position + (index - segment.Offset);
- }
- }
- position += segment.Count;
- }
- throw new EndOfStreamException();
- }
- private void PrepareToWrite(int count)
- {
- var minimumCapacity = (long)_position + (long)count;
- if (minimumCapacity > int.MaxValue)
- {
- throw new IOException("Stream was too long.");
- }
- _buffer.EnsureCapacity((int)minimumCapacity);
- _buffer.Length = _buffer.Capacity;
- if (_length < _position)
- {
- _buffer.Clear(_length, _position - _length);
- }
- }
- private byte[] ReadBytes(int count)
- {
- ThrowIfEndOfStream(count);
- var bytes = new byte[count];
- _buffer.GetBytes(_position, bytes, 0, count);
- _position += count;
- return bytes;
- }
- private void SetPositionAfterWrite(int position)
- {
- _position = position;
- if (_length < position)
- {
- _length = position;
- }
- }
- private void ThrowIfDisposed()
- {
- if (_disposed)
- {
- throw new ObjectDisposedException("ByteBufferStream");
- }
- }
- private void ThrowIfEndOfStream(int count)
- {
- var minimumLength = (long)_position + (long)count;
- if (_length < minimumLength)
- {
- if (_position < _length)
- {
- _position = _length;
- }
- throw new EndOfStreamException();
- }
- }
- /// <inheritdoc/>
- public override string ReadCString(UTF8Encoding encoding)
- {
- if (encoding == null)
- {
- throw new ArgumentNullException("encoding");
- }
- ThrowIfDisposed();
- var bytes = ReadCStringBytes();
- return Utf8Helper.DecodeUtf8String(bytes.Array, bytes.Offset, bytes.Count, encoding);
- }
- /// <inheritdoc/>
- public override ArraySegment<byte> ReadCStringBytes()
- {
- ThrowIfDisposed();
- ThrowIfEndOfStream(1);
- var segment = _buffer.AccessBackingBytes(_position);
- var index = Array.IndexOf<byte>(segment.Array, 0, segment.Offset, segment.Count);
- if (index != -1)
- {
- var length = index - segment.Offset;
- _position += length + 1; // advance over the null byte
- return new ArraySegment<byte>(segment.Array, segment.Offset, length); // without the null byte
- }
- else
- {
- var nullPosition = FindNullByte();
- var length = nullPosition - _position;
- var cstring = ReadBytes(length + 1); // advance over the null byte
- return new ArraySegment<byte>(cstring, 0, length); // without the null byte
- }
- }
- /// <inheritdoc/>
- public override Decimal128 ReadDecimal128()
- {
- ThrowIfDisposed();
- ThrowIfEndOfStream(16);
- var lowBits = (ulong)ReadInt64();
- var highBits = (ulong)ReadInt64();
- return Decimal128.FromIEEEBits(highBits, lowBits);
- }
- /// <inheritdoc/>
- public override double ReadDouble()
- {
- ThrowIfDisposed();
- ThrowIfEndOfStream(8);
- var segment = _buffer.AccessBackingBytes(_position);
- if (segment.Count >= 8)
- {
- _position += 8;
- return BitConverter.ToDouble(segment.Array, segment.Offset);
- }
- else
- {
- this.ReadBytes(_temp, 0, 8);
- return BitConverter.ToDouble(_temp, 0);
- }
- }
- /// <inheritdoc/>
- public override int ReadInt32()
- {
- ThrowIfDisposed();
- ThrowIfEndOfStream(4);
- var segment = _buffer.AccessBackingBytes(_position);
- if (segment.Count >= 4)
- {
- _position += 4;
- var bytes = segment.Array;
- var offset = segment.Offset;
- return bytes[offset] | (bytes[offset + 1] << 8) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 24);
- }
- else
- {
- this.ReadBytes(_temp, 0, 4);
- return _temp[0] | (_temp[1] << 8) | (_temp[2] << 16) | (_temp[3] << 24);
- }
- }
- /// <inheritdoc/>
- public override long ReadInt64()
- {
- ThrowIfDisposed();
- ThrowIfEndOfStream(8);
- var segment = _buffer.AccessBackingBytes(_position);
- if (segment.Count >= 8)
- {
- _position += 8;
- return BitConverter.ToInt64(segment.Array, segment.Offset);
- }
- else
- {
- this.ReadBytes(_temp, 0, 8);
- return BitConverter.ToInt64(_temp, 0);
- }
- }
- /// <inheritdoc/>
- public override ObjectId ReadObjectId()
- {
- ThrowIfDisposed();
- ThrowIfEndOfStream(12);
- var segment = _buffer.AccessBackingBytes(_position);
- if (segment.Count >= 12)
- {
- _position += 12;
- return new ObjectId(segment.Array, segment.Offset);
- }
- else
- {
- this.ReadBytes(_temp, 0, 12);
- return new ObjectId(_temp, 0);
- }
- }
- /// <inheritdoc/>
- public override IByteBuffer ReadSlice()
- {
- ThrowIfDisposed();
- var position = _position;
- var length = ReadInt32();
- ThrowIfEndOfStream(length - 4);
- Position = position + length;
- return _buffer.GetSlice(position, length);
- }
- /// <inheritdoc/>
- public override string ReadString(UTF8Encoding encoding)
- {
- if (encoding == null)
- {
- throw new ArgumentNullException("encoding");
- }
- ThrowIfDisposed();
- var length = ReadInt32();
- if (length <= 0)
- {
- var message = string.Format("Invalid string length: {0}.", length);
- throw new FormatException(message);
- }
- var segment = _buffer.AccessBackingBytes(_position);
- if (segment.Count >= length)
- {
- ThrowIfEndOfStream(length);
- if (segment.Array[segment.Offset + length - 1] != 0)
- {
- throw new FormatException("String is missing terminating null byte.");
- }
- _position += length;
- return Utf8Helper.DecodeUtf8String(segment.Array, segment.Offset, length - 1, encoding);
- }
- else
- {
- var bytes = length <= _tempUtf8.Length ? _tempUtf8 : new byte[length];
- this.ReadBytes(bytes, 0, length);
- if (bytes[length - 1] != 0)
- {
- throw new FormatException("String is missing terminating null byte.");
- }
- return Utf8Helper.DecodeUtf8String(bytes, 0, length - 1, encoding);
- }
- }
- /// <inheritdoc/>
- public override void SkipCString()
- {
- ThrowIfDisposed();
- var nullPosition = FindNullByte();
- _position = nullPosition + 1;
- }
- /// <inheritdoc/>
- public override void WriteCString(string value)
- {
- if (value == null)
- {
- throw new ArgumentNullException("value");
- }
- ThrowIfDisposed();
- var maxLength = CStringUtf8Encoding.GetMaxByteCount(value.Length) + 1;
- PrepareToWrite(maxLength);
- int actualLength;
- var segment = _buffer.AccessBackingBytes(_position);
- if (segment.Count >= maxLength)
- {
- actualLength = CStringUtf8Encoding.GetBytes(value, segment.Array, segment.Offset, Utf8Encodings.Strict);
- segment.Array[segment.Offset + actualLength] = 0;
- }
- else
- {
- byte[] bytes;
- if (maxLength <= _tempUtf8.Length)
- {
- bytes = _tempUtf8;
- actualLength = CStringUtf8Encoding.GetBytes(value, bytes, 0, Utf8Encodings.Strict);
- }
- else
- {
- bytes = Utf8Encodings.Strict.GetBytes(value);
- if (Array.IndexOf<byte>(bytes, 0) != -1)
- {
- throw new ArgumentException("A CString cannot contain null bytes.", "value");
- }
- actualLength = bytes.Length;
- }
- _buffer.SetBytes(_position, bytes, 0, actualLength);
- _buffer.SetByte(_position + actualLength, 0);
- }
- SetPositionAfterWrite(_position + actualLength + 1);
- }
- /// <inheritdoc/>
- public override void WriteCStringBytes(byte[] value)
- {
- if (value == null)
- {
- throw new ArgumentNullException("value");
- }
- ThrowIfDisposed();
- var length = value.Length;
- PrepareToWrite(length + 1);
- _buffer.SetBytes(_position, value, 0, length);
- _buffer.SetByte(_position + length, 0);
- SetPositionAfterWrite(_position + length + 1);
- }
- /// <inheritdoc/>
- public override void WriteDecimal128(Decimal128 value)
- {
- ThrowIfDisposed();
- WriteInt64((long)value.GetIEEELowBits());
- WriteInt64((long)value.GetIEEEHighBits());
- }
- /// <inheritdoc/>
- public override void WriteDouble(double value)
- {
- ThrowIfDisposed();
- PrepareToWrite(8);
- var bytes = BitConverter.GetBytes(value);
- _buffer.SetBytes(_position, bytes, 0, 8);
- SetPositionAfterWrite(_position + 8);
- }
- /// <inheritdoc/>
- public override void WriteInt32(int value)
- {
- ThrowIfDisposed();
- PrepareToWrite(4);
- var segment = _buffer.AccessBackingBytes(_position);
- if (segment.Count >= 4)
- {
- segment.Array[segment.Offset] = (byte)value;
- segment.Array[segment.Offset + 1] = (byte)(value >> 8);
- segment.Array[segment.Offset + 2] = (byte)(value >> 16);
- segment.Array[segment.Offset + 3] = (byte)(value >> 24);
- }
- else
- {
- _temp[0] = (byte)(value);
- _temp[1] = (byte)(value >> 8);
- _temp[2] = (byte)(value >> 16);
- _temp[3] = (byte)(value >> 24);
- _buffer.SetBytes(_position, _temp, 0, 4);
- }
- SetPositionAfterWrite(_position + 4);
- }
- /// <inheritdoc/>
- public override void WriteInt64(long value)
- {
- ThrowIfDisposed();
- PrepareToWrite(8);
- var bytes = BitConverter.GetBytes(value);
- _buffer.SetBytes(_position, bytes, 0, 8);
- SetPositionAfterWrite(_position + 8);
- }
- /// <inheritdoc/>
- public override void WriteObjectId(ObjectId value)
- {
- ThrowIfDisposed();
- PrepareToWrite(12);
- var segment = _buffer.AccessBackingBytes(_position);
- if (segment.Count >= 12)
- {
- value.ToByteArray(segment.Array, segment.Offset);
- }
- else
- {
- var bytes = value.ToByteArray();
- _buffer.SetBytes(_position, bytes, 0, 12);
- }
- SetPositionAfterWrite(_position + 12);
- }
- /// <inheritdoc/>
- public override void WriteString(string value, UTF8Encoding encoding)
- {
- ThrowIfDisposed();
- var maxLength = encoding.GetMaxByteCount(value.Length) + 5;
- PrepareToWrite(maxLength);
- int actualLength;
- var segment = _buffer.AccessBackingBytes(_position);
- if (segment.Count >= maxLength)
- {
- actualLength = encoding.GetBytes(value, 0, value.Length, segment.Array, segment.Offset + 4);
- var lengthPlusOne = actualLength + 1;
- segment.Array[segment.Offset] = (byte)lengthPlusOne;
- segment.Array[segment.Offset + 1] = (byte)(lengthPlusOne >> 8);
- segment.Array[segment.Offset + 2] = (byte)(lengthPlusOne >> 16);
- segment.Array[segment.Offset + 3] = (byte)(lengthPlusOne >> 24);
- segment.Array[segment.Offset + 4 + actualLength] = 0;
- }
- else
- {
- byte[] bytes;
- if (maxLength <= _tempUtf8.Length)
- {
- bytes = _tempUtf8;
- actualLength = encoding.GetBytes(value, 0, value.Length, bytes, 0);
- }
- else
- {
- bytes = encoding.GetBytes(value);
- actualLength = bytes.Length;
- }
- var lengthPlusOneBytes = BitConverter.GetBytes(actualLength + 1);
- _buffer.SetBytes(_position, lengthPlusOneBytes, 0, 4);
- _buffer.SetBytes(_position + 4, bytes, 0, actualLength);
- _buffer.SetByte(_position + 4 + actualLength, 0);
- }
- SetPositionAfterWrite(_position + actualLength + 5);
- }
- }
- }
|