public byte[] GetBytes()
        {
            Contract.Requires(this.Count >= 0);
            var tmp = new byte[this.Count];

            if (this.Count > 0)
            {
                Contract.Assert(this.Data != null);
                fixed(byte *ptr = tmp)
                {
                    UnmanagedHelpers.CopyUnsafe(ptr, this.Data, this.Count);
                }
            }
            return(tmp);
        }
Exemple #2
0
        public void Append(USlice source)
        {
            if (source.Count == 0)
            {
                return;
            }
            if (source.Data == null)
            {
                ThrowInvalidSource();
            }

            byte *ptr = AllocateInternal(source.Count, zeroed: false);

            Contract.Assert(ptr != null);
            UnmanagedHelpers.CopyUnsafe(ptr, source);
        }
Exemple #3
0
        public void Append(byte *source, uint size)
        {
            if (size == 0)
            {
                return;
            }
            if (source == null)
            {
                ThrowInvalidSource();
            }

            byte *ptr = AllocateInternal(size, zeroed: false);

            Contract.Assert(ptr != null, "AllocateInternal() => null");
            UnmanagedHelpers.CopyUnsafe(ptr, source, size);
        }
Exemple #4
0
 public UnmanagedSliceBuilder(byte *data, uint size)
 {
     if (data == null && size != 0)
     {
         throw new ArgumentNullException("data");
     }
     if (size == 0)
     {
         m_buffer = s_empty;
     }
     else
     {
         GrowBuffer(size);
         UnmanagedHelpers.CopyUnsafe(m_data, data, size);
         m_count = size;
     }
 }
        public byte[] ToArray()
        {
            if (m_begin == null)
            {
                ThrowDisposed();
            }
            var tmp = new byte[m_size];

            if (tmp.Length > 0)
            {
                fixed(byte *ptr = tmp)
                {
                    UnmanagedHelpers.CopyUnsafe(ptr, m_begin, (uint)m_size);
                }
            }
            return(tmp);
        }
Exemple #6
0
        public byte[] GetBytes()
        {
            if (m_buffer == null)
            {
                ThrowAlreadyDisposed();
            }

            var tmp = new byte[m_count];

            if (m_count >= 0)
            {
                fixed(byte *ptr = tmp)
                {
                    UnmanagedHelpers.CopyUnsafe(ptr, m_data, m_count);
                }
            }
            return(tmp);
        }
Exemple #7
0
        /// <summary>Copy a segment of the buffer to an unmanaged pointer, and return the corresponding slice</summary>
        /// <param name="count">Number of bytes to copy</param>
        /// <param name="dest">Destination pointer where the buffer will be copied. Caution: the destination buffer must be large enough!</param>
        /// <returns>Slice that points to the copied segment in the destination buffer</returns>
        internal USlice CopyTo(byte *dest, uint count)
        {
            if (m_buffer == null)
            {
                ThrowAlreadyDisposed();
            }
            if (count == 0)
            {
                return(default(USlice));
            }
            if (count > m_count)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            UnmanagedHelpers.CopyUnsafe(dest, m_data, count);
            return(new USlice(dest, count));
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (m_begin == null)
            {
                ThrowDisposed();
            }

            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");
            }

            uint pos = m_pos;

            if (pos >= m_size)
            {
                return(0);                           // EOF
            }
            uint chunk;

            checked { chunk = (uint)Math.Max(m_size - pos, count); }

            if (chunk > 0)
            {
                fixed(byte *ptr = buffer)
                {
                    UnmanagedHelpers.CopyUnsafe(ptr + offset, m_begin + pos, chunk);
                }

                m_pos = pos + chunk;
            }
            return((int)chunk);
        }