Exemple #1
0
        /// <summary>
        /// Reads a DirtyImageFragment from the stream, assuming it is already in compressed format.
        /// </summary>
        /// <param name="s"></param>
        public DirtyImageFragment(IDataStream s)
        {
            bounds.X      = s.ReadInt16();
            bounds.Y      = s.ReadInt16();
            bounds.Width  = s.ReadUInt16();
            bounds.Height = s.ReadUInt16();
            int imgLength = s.ReadInt32();

            screenshot = new Screenshot(bounds.Width, bounds.Height, 32, new byte[imgLength], bufferIsCompressed: true);
            s.Read(screenshot.Buffer, 0, imgLength);
        }
Exemple #2
0
        /// <summary>
        /// Reads a specific number of bytes from the stream, returning a byte array.  Ordinary stream.Read operations are not guaranteed to read all the requested bytes.
        /// </summary>
        /// <param name="s">The stream to read from.</param>
        /// <param name="n">The number of bytes to read.</param>
        /// <returns></returns>
        public static byte[] ReadNBytes(IDataStream s, int n)
        {
            byte[] buffer = new byte[n];
            if (n == 0)
            {
                return(buffer);                // Just to be explicit and sure about this behavior.
            }
            int totalRead = 0;
            int justRead;

            try
            {
                do
                {
                    totalRead += (justRead = s.Read(buffer, totalRead, n - totalRead));
                }while (justRead > 0 && totalRead < n);
            }
            catch (IOException ex)
            {
                if (ex.InnerException is SocketException)
                {
                    throw new EndOfStreamException("Stream was closed", ex);
                }
                else
                {
                    throw;
                }
            }
            catch (SocketException ex) { throw new EndOfStreamException("Stream was closed", ex); }
            if (totalRead < n)
            {
                throw new EndOfStreamException("Stream was closed");
            }
            else if (totalRead > n)
            {
                throw new Exception("Somehow read too much from stream");
            }
            return(buffer);
        }