Example #1
0
        /// <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);
        }
Example #2
0
        /// <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
            {
                // Compare to 128 to preserve original behavior
                const int maxLengthToUseCStringUtf8EncodingWith = 128;

                if (maxLength <= maxLengthToUseCStringUtf8EncodingWith)
                {
                    using var rentedBuffer = ThreadStaticBuffer.RentBuffer(maxLengthToUseCStringUtf8EncodingWith);
                    actualLength           = CStringUtf8Encoding.GetBytes(value, rentedBuffer.Bytes, 0, Utf8Encodings.Strict);

                    SetBytes(rentedBuffer.Bytes, actualLength);
                }
                else
                {
                    using var rentedSegmentEncoded = Utf8Encodings.Strict.GetBytesUsingThreadStaticBuffer(value);
                    var segmentEncoded = rentedSegmentEncoded.Segment;
                    actualLength = segmentEncoded.Count;

                    if (Array.IndexOf <byte>(segmentEncoded.Array, 0, 0, actualLength) != -1)
                    {
                        throw new ArgumentException("A CString cannot contain null bytes.", "value");
                    }

                    SetBytes(segmentEncoded.Array, actualLength);
                }

                void SetBytes(byte[] bytes, int lenght)
                {
                    _buffer.SetBytes(_position, bytes, 0, actualLength);
                    _buffer.SetByte(_position + actualLength, 0);
                }
            }

            SetPositionAfterWrite(_position + actualLength + 1);
        }
Example #3
0
        /// <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 WriteCString(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            ThrowIfDisposed();


            // Compare to 128 to preserve original behavior
            const int maxLengthToUseCStringUtf8EncodingWith = 128;

            if (CStringUtf8Encoding.GetMaxByteCount(value.Length) <= maxLengthToUseCStringUtf8EncodingWith)
            {
                using var rentedBuffer = ThreadStaticBuffer.RentBuffer(maxLengthToUseCStringUtf8EncodingWith);

                var length = CStringUtf8Encoding.GetBytes(value, rentedBuffer.Bytes, 0, Utf8Encodings.Strict);
                WriteBytes(rentedBuffer.Bytes, length);
            }
            else
            {
                using var rentedSegment = Utf8Encodings.Strict.GetBytesUsingThreadStaticBuffer(value);
                var segment = rentedSegment.Segment;
                if (Array.IndexOf <byte>(segment.Array, 0, 0, segment.Count) != -1)
                {
                    throw new ArgumentException("A CString cannot contain null bytes.", "value");
                }

                WriteBytes(segment.Array, segment.Count);
            }

            void WriteBytes(byte[] bytes, int length)
            {
                _stream.Write(bytes, 0, length);
                _stream.WriteByte(0);
            }
        }