示例#1
0
        public void Basics()
        {
            var trie = new ByteTrie <string>();

            var strings = new[] { "HELLO", "HELLO WORLD", "HERBERT" };

            foreach (var s in strings)
            {
                trie.Add(s, Encoding.UTF8.GetBytes(s));
            }

            foreach (var s1 in strings)
            {
                Assert.Same(s1, trie.Find(Encoding.UTF8.GetBytes(s1)));
            }

            Assert.Null(trie.Find(Encoding.UTF8.GetBytes("Not Included")));
            Assert.Null(trie.Find(Encoding.UTF8.GetBytes("HELL")));

            Assert.Equal("HELLO", trie.Find(Encoding.UTF8.GetBytes("HELLO MUM")));
            Assert.Equal("HELLO WORLD".Length, trie.MaxDepth);

            trie.SetDefaultValue("DEFAULT");

            Assert.Equal("DEFAULT", trie.Find(Encoding.UTF8.GetBytes("Also Not Included")));
        }
 public virtual void TestBasics()
 {
     ByteTrie<string> trie = new ByteTrie<string>();
     string[] strings = new string[] { "HELLO", "HELLO WORLD", "HERBERT" };
     foreach (string s in strings)
     {
         trie.AddPath(s, Sharpen.Runtime.GetBytesForString(s));
     }
     foreach (string s_1 in strings)
     {
         NUnit.Framework.Assert.AreSame(s_1, trie.Find(Sharpen.Runtime.GetBytesForString(s_1)));
     }
     NUnit.Framework.Assert.IsNull(trie.Find(Sharpen.Runtime.GetBytesForString("Not Included")));
     NUnit.Framework.Assert.IsNull(trie.Find(Sharpen.Runtime.GetBytesForString("HELL")));
     Sharpen.Tests.AreEqual("HELLO", trie.Find(Sharpen.Runtime.GetBytesForString("HELLO MUM")));
     Sharpen.Tests.AreEqual("HELLO WORLD".Length, trie.GetMaxDepth());
     trie.SetDefaultValue("DEFAULT");
     Sharpen.Tests.AreEqual("DEFAULT", trie.Find(Sharpen.Runtime.GetBytesForString("Also Not Included")));
 }
 public void TestBasics()
 {
     var trie = new ByteTrie<string>();
     var strings = new[] { "HELLO", "HELLO WORLD", "HERBERT" };
     foreach (var s in strings)
     {
         trie.AddPath(s, Encoding.UTF8.GetBytes(s));
     }
     foreach (var s1 in strings)
     {
         Assert.Same(s1, trie.Find(Encoding.UTF8.GetBytes(s1)));
     }
     Assert.Null(trie.Find(Encoding.UTF8.GetBytes("Not Included")));
     Assert.Null(trie.Find(Encoding.UTF8.GetBytes("HELL")));
     Assert.Equal("HELLO", trie.Find(Encoding.UTF8.GetBytes("HELLO MUM")));
     Assert.Equal("HELLO WORLD".Length, trie.MaxDepth);
     trie.SetDefaultValue("DEFAULT");
     Assert.Equal("DEFAULT", trie.Find(Encoding.UTF8.GetBytes("Also Not Included")));
 }
        public static FileType?DetectFileType([NotNull] BufferedInputStream inputStream)
        {
            int maxByteCount = _root.GetMaxDepth();

            inputStream.Mark(maxByteCount);
            sbyte[] bytes     = new sbyte[maxByteCount];
            int     bytesRead = inputStream.Read(bytes);

            if (bytesRead == -1)
            {
                throw new IOException("Stream ended before file's magic number could be determined.");
            }
            inputStream.Reset();
            //noinspection ConstantConditions
            return(_root.Find(bytes));
        }
示例#5
0
 public Util.FileType CheckType(byte[] bytes)
 {
     return(bytes.RegionEquals(4, 4, _ftypBytes)
         ? _ftypTrie.Find(bytes, 8, 4)
         : Util.FileType.Unknown);
 }
        public static IEnumerable <JpegSegment> ReadSegments([NotNull] Stream stream)
        {
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Must be able to seek.", nameof(stream));
            }

            // first two bytes should be JPEG magic number
            var magicNumber = GetUInt16(stream);

            if (magicNumber != 0xFFD8)
            {
                throw new JpegProcessingException($"JPEG data should begin with 0xFFD8, not 0x{magicNumber:X4}.");
            }

            while (true)
            {
                var padding = 0;

                // Find the segment marker. Markers are zero or more 0xFF bytes, followed
                // by a 0xFF and then a byte not equal to 0x00 or 0xFF.
                var segmentIdentifier = stream.ReadByte();
                var segmentTypeByte   = stream.ReadByte();

                if (segmentTypeByte == -1)
                {
                    yield break;
                }

                // Read until we have a 0xFF byte followed by a byte that is not 0xFF or 0x00
                while (segmentIdentifier != 0xFF || segmentTypeByte == 0xFF || segmentTypeByte == 0)
                {
                    padding++;
                    segmentIdentifier = segmentTypeByte;
                    segmentTypeByte   = stream.ReadByte();

                    if (segmentTypeByte == -1)
                    {
                        yield break;
                    }
                }

                var segmentType = (JpegSegmentType)segmentTypeByte;
                var offset      = stream.Position - 2;

                // if there is a payload, then segment length includes the two size bytes
                if (segmentType.ContainsPayload())
                {
                    var pos = stream.Position;

                    // Read the 2-byte big-endian segment length (excludes two marker bytes)
                    var b1 = stream.ReadByte();
                    var b2 = stream.ReadByte();
                    if (b2 == -1)
                    {
                        yield break;
                    }
                    var segmentLength = unchecked ((ushort)(b1 << 8 | b2));

                    var preambleBytes = new byte[Math.Min(segmentLength, _appSegmentByPreambleBytes.MaxDepth)];
                    if (stream.Read(preambleBytes, 0, preambleBytes.Length) != preambleBytes.Length)
                    {
                        yield break;
                    }
                    var preamble = _appSegmentByPreambleBytes.Find(preambleBytes);

                    yield return(new JpegSegment(segmentType, segmentLength, padding, offset, preamble));

                    // A length of less than two would be an error
                    if (segmentLength < 2)
                    {
                        yield break;
                    }

                    stream.Position = pos + segmentLength;
                }
                else
                {
                    yield return(new JpegSegment(segmentType, 0, padding, offset, ""));
                }
            }
        }