Exemplo n.º 1
0
        private static byte[] GetBytes(Stream stream, int initialLength)
        {
            if (stream.Position > 0)
            {
                stream.Position = 0;
            }

            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1)
            {
                initialLength = 32768;
            }

            byte[] buffer = SafeMemoryAllocator.CreateArray <byte>(initialLength);
            int    read   = 0;

            int chunk;

            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;

                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();

                    // End of stream? If so, we're done
                    if (nextByte == -1)
                    {
                        return(buffer);
                    }

                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Buffer.BlockCopy(buffer, 0, newBuffer, 0, Buffer.ByteLength(buffer));
                    newBuffer[read] = (byte)nextByte;
                    buffer          = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = SafeMemoryAllocator.CreateArray <byte>(read);
            Buffer.BlockCopy(buffer, 0, ret, 0, Buffer.ByteLength(ret));
            return(ret);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts the int array to a byte array.
        /// </summary>
        /// <param name="integers">The int array to convert.</param>
        /// <returns>the byte array representation of the specified int array</returns>
        public static byte[] ConvertIntArrayToByteArray(int[] integers)
        {
            if (integers == null || integers.Length == 0)
            {
                return(new byte[0]);
            }

            int sizeInBytes = integers.Length * 4;

            byte[] bytes = SafeMemoryAllocator.CreateArray <byte>(sizeInBytes);

            Buffer.BlockCopy(integers, 0, bytes, 0, sizeInBytes);

            return(bytes);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts the byte array to an int array.
        /// </summary>
        /// <param name="bytes">The byte array to convert.</param>
        /// <returns>the int array representation of the specified byte array</returns>
        /// <remarks>This method expects the specified byte array's length to be divisible by 4 (the number of bytes for a single int)</remarks>
        /// <exception cref="ArgumentOutOfRangeException">thrown if the specified byte array is not divisible by 4</exception>
        public static int[] ConvertByteArrayToIntArray(byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0)
            {
                return(new int[0]);
            }

            if (bytes.Length % 4 != 0)
            {
                throw new ArgumentOutOfRangeException("bytes", "This method expects the specified byte array's length to be divisible by 4 (the number of bytes for a single int)");
            }

            int sizeInIntegers = bytes.Length / 4;

            int[] integers = SafeMemoryAllocator.CreateArray <int>(sizeInIntegers);

            Buffer.BlockCopy(bytes, 0, integers, 0, bytes.Length);

            return(integers);
        }
Exemplo n.º 4
0
        private static byte[] ReadBytes(IPrimitiveReader reader, int count)
        {
            if (count < 0)
            {
                throw new InvalidDataException();
            }

            var result    = SafeMemoryAllocator.CreateArray <byte>(count);
            int remaining = count;

            while (remaining > 0)
            {
                int read = reader.BaseStream.Read(result, count - remaining, remaining);
                if (read <= 0)
                {
                    throw new InvalidDataException("Unexpected end of stream");
                }
                remaining -= read;
            }
            return(result);
        }
Exemplo n.º 5
0
        public static BitArray ReadBitArray(this IPrimitiveReader reader)
        {
            var count = reader.ReadVarInt32();

            if (count == -1)
            {
                return(null);
            }
            if (count < 0)
            {
                reader.RaiseInvalidDataException();
            }
            var buffer = SafeMemoryAllocator.CreateArray <byte>(RoundToNearest8(count));

            reader.BaseStream.Read(buffer, 0, buffer.Length);
            var result = new BitArray(buffer)
            {
                Length = count
            };

            return(result);
        }