Пример #1
0
        public void testFileCorruptionOPOIFS()
        {
            // create test InputStream
            byte[]      testData  = { (byte)1, (byte)2, (byte)3 };
            InputStream testInput = new ByteArrayInputStream(testData);

            // detect header
            InputStream in1 = new PushbackInputStream(testInput, 10);

            Assert.IsFalse(OPOIFSFileSystem.HasPOIFSHeader(in1));
            // check if InputStream is still intact
            byte[] test = new byte[3];
            in1.Read(test);
            Assert.IsTrue(Arrays.Equals(testData, test));
            Assert.AreEqual(-1, in1.Read());
        }
Пример #2
0
        public void setup()
        {
            _rawSample = new PushbackInputStream(HttpSample.ChunkedResponse());
            var buf = new byte[74];

            _rawSample.Read(buf, 0, 74);             // skip HTTP headers
            _subject = new HttpChunkedStreamWrapper(_rawSample, TimeSpan.FromSeconds(1));
        }
Пример #3
0
        public void TestFileCorruption()
        {
            // create test InputStream
            byte[] testData = { (byte)1, (byte)2, (byte)3 };
            ByteArrayInputStream testInput = new ByteArrayInputStream(testData);

            // detect header
            InputStream in1 = new PushbackInputStream(testInput, 10);

            Assert.IsFalse(DocumentFactoryHelper.HasOOXMLHeader(in1));
            //noinspection deprecation
            Assert.IsFalse(POIXMLDocument.HasOOXMLHeader(in1));

            // check if InputStream is still intact
            byte[] test = new byte[3];
            Assert.AreEqual(3, in1.Read(test));
            Assert.IsTrue(Arrays.Equals(testData, test));
            Assert.AreEqual(-1, in1.Read());
        }
Пример #4
0
        public void Read_ReadsFromBufferFirst()
        {
            const int Position    = 128;
            var       callsToRead = 0;
            var       mock        = new FakeStream
            {
                OnCanReadGet              = () => true,
                OnCanSeekGet              = () => false,
                OnPositionGet             = () => Position,
                OnReadByteArrayInt32Int32 = (_, __, ___) =>
                {
                    callsToRead++;
                    return(0);
                }
            };

            using (var pushbackStream = new PushbackInputStream(mock))
            {
                var pushbackBytes = new byte[] { 1, 2, 4, 8 };
                pushbackStream.Write(pushbackBytes, 0, pushbackBytes.Length);

                // Read as many bytes as we wrote to the pusback buffer
                // The stream wrapper should not touch the underlying stream
                var result    = new byte[pushbackBytes.Length];
                var bytesRead = pushbackStream.Read(result, 0, result.Length);

                Assert.Equal(0, callsToRead);
                Assert.Equal(pushbackBytes.Length, bytesRead);
                Assert.Equal(pushbackBytes, result);

                // The pushback buffer should be empty at this point;
                // a subsequent read should hit the underlying stream
                pushbackStream.Read(result, 0, result.Length);
                Assert.Equal(1, callsToRead);
            }
        }
        /// <exception cref="System.IO.IOException"></exception>
        public DeflateInputStream(InputStream wrapped)
        {
            byte[] peeked = new byte[6];
            PushbackInputStream pushback = new PushbackInputStream(wrapped, peeked.Length);
            int headerLength             = pushback.Read(peeked);

            if (headerLength == -1)
            {
                throw new IOException("Unable to read the response");
            }
            byte[]   dummy = new byte[1];
            Inflater inf   = new Inflater();

            try
            {
                int n;
                while ((n = inf.Inflate(dummy)) == 0)
                {
                    if (inf.IsFinished)
                    {
                        throw new IOException("Unable to read the response");
                    }
                    if (inf.NeedsDictionary())
                    {
                        break;
                    }
                    if (inf.IsNeedingInput)
                    {
                        inf.SetInput(peeked);
                    }
                }
                if (n == -1)
                {
                    throw new IOException("Unable to read the response");
                }
                pushback.Unread(peeked, 0, headerLength);
                sourceStream = new DeflateInputStream.DeflateStream(pushback, new Inflater());
            }
            catch (SharpZipBaseException)
            {
                pushback.Unread(peeked, 0, headerLength);
                sourceStream = new DeflateInputStream.DeflateStream(pushback, new Inflater(true));
            }
            finally
            {
                inf.Finish();
            }
        }
Пример #6
0
        public void Read_ReadsFromBufferFirst()
        {
            const int Position = 128;
            var callsToRead = 0;
            var mock = new FakeStream
                           {
                               OnCanReadGet = () => true,
                               OnCanSeekGet = () => false,
                               OnPositionGet = () => Position,
                               OnReadByteArrayInt32Int32 = (_, __, ___) =>
                                   {
                                       callsToRead++;
                                       return 0;
                                   }
                           };
            using (var pushbackStream = new PushbackInputStream(mock))
            {
                var pushbackBytes = new byte[] { 1, 2, 4, 8 };
                pushbackStream.Write(pushbackBytes, 0, pushbackBytes.Length);

                // Read as many bytes as we wrote to the pusback buffer
                // The stream wrapper should not touch the underlying stream
                var result = new byte[pushbackBytes.Length];
                var bytesRead = pushbackStream.Read(result, 0, result.Length);

                Assert.Equal(0, callsToRead);
                Assert.Equal(pushbackBytes.Length, bytesRead);
                Assert.Equal(pushbackBytes, result);

                // The pushback buffer should be empty at this point;
                // a subsequent read should hit the underlying stream
                pushbackStream.Read(result, 0, result.Length);
                Assert.Equal(1, callsToRead);
            }
        }
Пример #7
0
 public void reading_is_passed_to_underlying_stream_and_correct_length_returned()
 {
     _subject.Read(_buffer, 0, 100);
     _underlying.Received().Read(_buffer, 0, 100);
     Assert.That(_subject.Position, Is.EqualTo(99));
 }