Esempio n. 1
0
        /// <summary>
        /// Checks whether an {@link InputStream} is in the Horrible
        /// Property Set Format.
        /// </summary>
        /// <param name="stream">The {@link InputStream} To check. In order To
        /// perform the check, the method Reads the first bytes from the
        /// stream. After Reading, the stream is Reset To the position it
        /// had before Reading. The {@link InputStream} must support the
        /// {@link InputStream#mark} method.</param>
        /// <returns>
        ///     <c>true</c> if the stream is a property Set
        /// stream; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsPropertySetStream(Stream stream)
        {
            ByteArrayInputStream dis = stream as ByteArrayInputStream;

            /*
             * Read at most this many bytes.
             */
            int BUFFER_SIZE = 50;

            /*
             * Mark the current position in the stream so that we can
             * Reset To this position if the stream does not contain a
             * property Set.
             */
            if (dis == null || !dis.MarkSupported())
            {
                throw new MarkUnsupportedException(stream.GetType().Name);
            }
            dis.Mark(BUFFER_SIZE);

            /*
             * Read a couple of bytes from the stream.
             */
            byte[] buffer = new byte[BUFFER_SIZE];
            int    bytes  =
                stream.Read(buffer, 0,
                            (int)Math.Min(buffer.Length, dis.Available()));
            bool isPropertySetStream =
                IsPropertySetStream(buffer, 0, bytes);

            stream.Seek(0, SeekOrigin.Begin);
            dis.Reset();
            return(isPropertySetStream);
        }
Esempio n. 2
0
        public virtual void TestSkipFully()
        {
            byte[] inArray           = new byte[] { 0, 1, 2, 3, 4 };
            ByteArrayInputStream @in = new ByteArrayInputStream(inArray);

            try
            {
                @in.Mark(inArray.Length);
                IOUtils.SkipFully(@in, 2);
                IOUtils.SkipFully(@in, 2);
                try
                {
                    IOUtils.SkipFully(@in, 2);
                    NUnit.Framework.Assert.Fail("expected to get a PrematureEOFException");
                }
                catch (EOFException e)
                {
                    Assert.Equal("Premature EOF from inputStream " + "after skipping 1 byte(s)."
                                 , e.Message);
                }
                @in.Reset();
                try
                {
                    IOUtils.SkipFully(@in, 20);
                    NUnit.Framework.Assert.Fail("expected to get a PrematureEOFException");
                }
                catch (EOFException e)
                {
                    Assert.Equal("Premature EOF from inputStream " + "after skipping 5 byte(s)."
                                 , e.Message);
                }
                @in.Reset();
                IOUtils.SkipFully(@in, 5);
                try
                {
                    IOUtils.SkipFully(@in, 10);
                    NUnit.Framework.Assert.Fail("expected to get a PrematureEOFException");
                }
                catch (EOFException e)
                {
                    Assert.Equal("Premature EOF from inputStream " + "after skipping 0 byte(s)."
                                 , e.Message);
                }
            }
            finally
            {
                @in.Close();
            }
        }