| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- /* 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.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MongoDB.Bson.IO
- {
- /// <summary>
- /// A Stream that wraps another Stream while implementing the BsonStream abstract methods.
- /// </summary>
- public sealed class BsonStreamAdapter : BsonStream
- {
- // fields
- private bool _disposed;
- private bool _ownsStream;
- private readonly Stream _stream;
- private readonly byte[] _temp = new byte[12];
- private readonly byte[] _tempUtf8 = new byte[128];
- // constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="BsonStreamAdapter"/> class.
- /// </summary>
- /// <param name="stream">The stream.</param>
- /// <param name="ownsStream">if set to <c>true</c> [owns stream].</param>
- /// <exception cref="System.ArgumentNullException">stream</exception>
- public BsonStreamAdapter(Stream stream, bool ownsStream = false)
- {
- if (stream == null)
- {
- throw new ArgumentNullException("stream");
- }
- _stream = stream;
- _ownsStream = ownsStream;
- }
- // properties
- /// <summary>
- /// Gets the base stream.
- /// </summary>
- /// <value>
- /// The base stream.
- /// </value>
- public Stream BaseStream
- {
- get
- {
- ThrowIfDisposed();
- return _stream;
- }
- }
- /// <inheritdoc/>
- public override bool CanRead
- {
- get
- {
- ThrowIfDisposed();
- return _stream.CanRead;
- }
- }
- /// <inheritdoc/>
- public override bool CanSeek
- {
- get
- {
- ThrowIfDisposed();
- return _stream.CanSeek;
- }
- }
- /// <inheritdoc/>
- public override bool CanTimeout
- {
- get
- {
- ThrowIfDisposed();
- return _stream.CanTimeout;
- }
- }
- /// <inheritdoc/>
- public override bool CanWrite
- {
- get
- {
- ThrowIfDisposed();
- return _stream.CanWrite;
- }
- }
- /// <inheritdoc/>
- public override long Length
- {
- get
- {
- ThrowIfDisposed();
- return _stream.Length;
- }
- }
- /// <inheritdoc/>
- public override long Position
- {
- get
- {
- ThrowIfDisposed();
- return _stream.Position;
- }
- set
- {
- ThrowIfDisposed();
- _stream.Position = value;
- }
- }
- /// <inheritdoc/>
- public override int ReadTimeout
- {
- get
- {
- ThrowIfDisposed();
- return _stream.ReadTimeout;
- }
- set
- {
- ThrowIfDisposed();
- _stream.ReadTimeout = value;
- }
- }
- /// <inheritdoc/>
- public override int WriteTimeout
- {
- get
- {
- ThrowIfDisposed();
- return _stream.WriteTimeout;
- }
- set
- {
- ThrowIfDisposed();
- _stream.WriteTimeout = value;
- }
- }
- // methods
- #if NET45
- /// <inheritdoc/>
- public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
- {
- ThrowIfDisposed();
- return _stream.BeginRead(buffer, offset, count, callback, state);
- }
- #endif
- #if NET45
- /// <inheritdoc/>
- public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
- {
- ThrowIfDisposed();
- return _stream.BeginWrite(buffer, offset, count, callback, state);
- }
- #endif
- #if NET45
- /// <inheritdoc/>
- public override void Close()
- {
- base.Close(); // base class will call Dispose
- }
- #endif
- /// <inheritdoc/>
- public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
- {
- ThrowIfDisposed();
- return _stream.CopyToAsync(destination, bufferSize, cancellationToken);
- }
- /// <inheritdoc/>
- protected override void Dispose(bool disposing)
- {
- if (!_disposed)
- {
- if (disposing)
- {
- if (_ownsStream)
- {
- _stream.Dispose();
- }
- }
- _disposed = true;
- }
- base.Dispose(disposing);
- }
- #if NET45
- /// <inheritdoc/>
- public override int EndRead(IAsyncResult asyncResult)
- {
- ThrowIfDisposed();
- return _stream.EndRead(asyncResult);
- }
- #endif
- #if NET45
- /// <inheritdoc/>
- public override void EndWrite(IAsyncResult asyncResult)
- {
- ThrowIfDisposed();
- _stream.EndWrite(asyncResult);
- }
- #endif
- /// <inheritdoc/>
- public override void Flush()
- {
- ThrowIfDisposed();
- _stream.Flush();
- }
- /// <inheritdoc/>
- public override Task FlushAsync(CancellationToken cancellationToken)
- {
- ThrowIfDisposed();
- return _stream.FlushAsync(cancellationToken);
- }
- /// <inheritdoc/>
- public override int Read(byte[] buffer, int offset, int count)
- {
- ThrowIfDisposed();
- return _stream.Read(buffer, offset, count);
- }
- /// <inheritdoc/>
- public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
- {
- ThrowIfDisposed();
- return _stream.ReadAsync(buffer, offset, count, cancellationToken);
- }
- /// <inheritdoc/>
- public override int ReadByte()
- {
- ThrowIfDisposed();
- return _stream.ReadByte();
- }
- /// <inheritdoc/>
- public override string ReadCString(UTF8Encoding encoding)
- {
- if (encoding == null)
- {
- throw new ArgumentNullException("encoding");
- }
- ThrowIfDisposed();
- var bytes = ReadCStringBytes();
- return Utf8Helper.DecodeUtf8String(bytes.Array, 0, bytes.Count, encoding);
- }
- /// <inheritdoc/>
- public override ArraySegment<byte> ReadCStringBytes()
- {
- ThrowIfDisposed();
- var memoryStream = new MemoryStream(32);
- while (true)
- {
- var b = _stream.ReadByte();
- if (b == -1)
- {
- throw new EndOfStreamException();
- }
- if (b == 0)
- {
- byte[] memoryStreamBuffer;
- #if NETSTANDARD1_5 || NETSTANDARD1_6
- memoryStreamBuffer = memoryStream.ToArray();
- #else
- memoryStreamBuffer = memoryStream.GetBuffer();
- #endif
- return new ArraySegment<byte>(memoryStreamBuffer, 0, (int)memoryStream.Length);
- }
- memoryStream.WriteByte((byte)b);
- }
- }
- /// <inheritdoc/>
- public override Decimal128 ReadDecimal128()
- {
- ThrowIfDisposed();
- var lowBits = (ulong)ReadInt64();
- var highBits = (ulong)ReadInt64();
- return Decimal128.FromIEEEBits(highBits, lowBits);
- }
- /// <inheritdoc/>
- public override double ReadDouble()
- {
- ThrowIfDisposed();
- this.ReadBytes(_temp, 0, 8);
- return BitConverter.ToDouble(_temp, 0);
- }
- /// <inheritdoc/>
- public override int ReadInt32()
- {
- ThrowIfDisposed();
- this.ReadBytes(_temp, 0, 4);
- return _temp[0] | (_temp[1] << 8) | (_temp[2] << 16) | (_temp[3] << 24);
- }
- /// <inheritdoc/>
- public override long ReadInt64()
- {
- ThrowIfDisposed();
- this.ReadBytes(_temp, 0, 8);
- return BitConverter.ToInt64(_temp, 0);
- }
- /// <inheritdoc/>
- public override ObjectId ReadObjectId()
- {
- ThrowIfDisposed();
- this.ReadBytes(_temp, 0, 12);
- return new ObjectId(_temp, 0);
- }
- /// <inheritdoc/>
- public override IByteBuffer ReadSlice()
- {
- ThrowIfDisposed();
- var position = _stream.Position;
- var length = ReadInt32();
- var bytes = new byte[length];
- _stream.Position = position;
- this.ReadBytes(bytes, 0, length);
- return new ByteArrayBuffer(bytes, isReadOnly: true);
- }
- /// <inheritdoc/>
- public override string ReadString(UTF8Encoding encoding)
- {
- if (encoding == null)
- {
- throw new ArgumentNullException("encoding");
- }
- ThrowIfDisposed();
- var length = ReadInt32();
- 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 encoding.GetString(bytes, 0, length - 1);
- }
- /// <inheritdoc/>
- public override long Seek(long offset, SeekOrigin origin)
- {
- ThrowIfDisposed();
- return _stream.Seek(offset, origin);
- }
- /// <inheritdoc/>
- public override void SetLength(long value)
- {
- ThrowIfDisposed();
- _stream.SetLength(value);
- }
- /// <inheritdoc/>
- public override void SkipCString()
- {
- ThrowIfDisposed();
- while (true)
- {
- var b = _stream.ReadByte();
- if (b == -1)
- {
- throw new EndOfStreamException();
- }
- if (b == 0)
- {
- return;
- }
- }
- }
- /// <inheritdoc/>
- private void ThrowIfDisposed()
- {
- if (_disposed)
- {
- throw new ObjectDisposedException(GetType().Name);
- }
- }
- /// <inheritdoc/>
- public override void Write(byte[] buffer, int offset, int count)
- {
- ThrowIfDisposed();
- _stream.Write(buffer, offset, count);
- }
- /// <inheritdoc/>
- public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
- {
- ThrowIfDisposed();
- return _stream.WriteAsync(buffer, offset, count, cancellationToken);
- }
- /// <inheritdoc/>
- public override void WriteByte(byte value)
- {
- ThrowIfDisposed();
- _stream.WriteByte(value);
- }
- /// <inheritdoc/>
- public override void WriteCString(string value)
- {
- if (value == null)
- {
- throw new ArgumentNullException("value");
- }
- ThrowIfDisposed();
- byte[] bytes;
- int length;
- if (CStringUtf8Encoding.GetMaxByteCount(value.Length) <= _tempUtf8.Length)
- {
- bytes = _tempUtf8;
- length = CStringUtf8Encoding.GetBytes(value, _tempUtf8, 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");
- }
- length = bytes.Length;
- }
- _stream.Write(bytes, 0, length);
- _stream.WriteByte(0);
- }
- /// <inheritdoc/>
- public override void WriteCStringBytes(byte[] value)
- {
- if (value == null)
- {
- throw new ArgumentNullException("value");
- }
- ThrowIfDisposed();
- this.WriteBytes(value, 0, value.Length);
- WriteByte(0);
- }
- /// <inheritdoc/>
- public override void WriteDecimal128(Decimal128 value)
- {
- ThrowIfDisposed();
- WriteInt64((long)value.GetIEEELowBits());
- WriteInt64((long)value.GetIEEEHighBits());
- }
- /// <inheritdoc/>
- public override void WriteDouble(double value)
- {
- ThrowIfDisposed();
- var bytes = BitConverter.GetBytes(value);
- _stream.Write(bytes, 0, 8);
- }
- /// <inheritdoc/>
- public override void WriteInt32(int value)
- {
- ThrowIfDisposed();
- _temp[0] = (byte)(value);
- _temp[1] = (byte)(value >> 8);
- _temp[2] = (byte)(value >> 16);
- _temp[3] = (byte)(value >> 24);
- _stream.Write(_temp, 0, 4);
- }
- /// <inheritdoc/>
- public override void WriteInt64(long value)
- {
- ThrowIfDisposed();
- var bytes = BitConverter.GetBytes(value);
- _stream.Write(bytes, 0, 8);
- }
- /// <inheritdoc/>
- public override void WriteObjectId(ObjectId value)
- {
- ThrowIfDisposed();
- value.ToByteArray(_temp, 0);
- _stream.Write(_temp, 0, 12);
- }
- /// <inheritdoc/>
- public override void WriteString(string value, UTF8Encoding encoding)
- {
- if (value == null)
- {
- throw new ArgumentNullException("value");
- }
- if (encoding == null)
- {
- throw new ArgumentNullException("encoding");
- }
- ThrowIfDisposed();
- byte[] bytes;
- int length;
- if (encoding.GetMaxByteCount(value.Length) <= _tempUtf8.Length)
- {
- bytes = _tempUtf8;
- length = encoding.GetBytes(value, 0, value.Length, _tempUtf8, 0);
- }
- else
- {
- bytes = encoding.GetBytes(value);
- length = bytes.Length;
- }
- WriteInt32(length + 1);
- _stream.Write(bytes, 0, length);
- _stream.WriteByte(0);
- }
- }
- }
|