Exemple #1
0
        /// <summary>
        /// Writes a string to the file at the given offset from the beginning of the file. Any present data is overwritten.
        /// </summary>
        /// <param name="byteOffset">The byte offset relative to the beginning of the file.</param>
        /// <param name="content">The text to be written represented via a string.</param>
        public void WriteToFile(long byteOffset, string content)
        {
            lock (Mutex)
            {
                if (!_fileStream.CanSeek)
                {
                    throw new NotSupportedException();
                }
            }

            if (content == null)
            {
                content = string.Empty;
            }

            var byteArray = InputEncoding.GetBytes(content);

            if (byteOffset == 0 && InputEncoding is UTF7Encoding)
            {
                // UTF-7 does not have a BOM by default in C#. Add the BOM.
                byteArray = new byte[] { 0x2b, 0x2f, 0x76, 0x38 }.Concat(byteArray).ToArray();
            }

            lock (Mutex)
            {
                _fileStream.Seek(byteOffset, SeekOrigin.Begin);
                _fileStream.Write(byteArray);
                _fileStream.Flush();
            }
        }
Exemple #2
0
        /// <summary>
        /// Reads continuous text of given length of bytes from a given offset from the file and returns it as a string.
        /// </summary>
        /// <param name="byteOffset">The byte offset relative to the beginning of the file.</param>
        /// <param name="byteCount">The number of bytes that should be read from the file.</param>
        /// <returns></returns>
        public string ReadFromFile(long byteOffset, int byteCount)
        {
            lock (Mutex)
            {
                if (!_fileStream.CanSeek)
                {
                    throw new NotSupportedException();
                }
            }

            var byteArray = new byte[byteCount];

            if (byteOffset == 0 && InputEncoding is UTF7Encoding)
            {
                // UTF-7 does not expect a BOM by default in C#. Skip the BOM.
                byteOffset = 4;
            }

            lock (Mutex)
            {
                _fileStream.Seek(byteOffset, SeekOrigin.Begin);
                _fileStream.Read(byteArray, 0, byteCount);
            }

            return(InputEncoding.GetString(byteArray));
        }
Exemple #3
0
 /// <summary>
 ///     Creates bookmarks container from stream
 ///     If AutoDetectEncoding is true, tries to use encoding from HTML headers
 /// </summary>
 /// <param name="inputStream">Stream containing valid HTML</param>
 /// <returns>Bookmarks container</returns>
 public override BookmarkFolder Read(Stream inputStream)
 {
     using (var ms = new MemoryStream())
     {
         inputStream.CopyTo(ms);
         var content = ms.ToArray();
         if (AutoDetectEncoding)
         {
             InputEncoding = content.GetEncoding();
             var headerLenghtBytes = HeaderLength * InputEncoding.GetMaxByteCount(1);
             var toRead            = headerLenghtBytes > 0 && headerLenghtBytes < content.Length ? headerLenghtBytes : content.Length;
             var header            = InputEncoding.GetString(content, 0, toRead);
             InputEncoding = GetEncoding(header);
         }
         return(Read(InputEncoding.GetString(content, 0, content.Length)));
     }
 }
Exemple #4
0
        public void DecodeString(string encodedKey, char?expectedDecodedKey, SpecialKeys?expectedDecodedSpecialKey)
        {
            var pressedKey = InputEncoding.DecodeString(encodedKey);

            if (expectedDecodedSpecialKey.HasValue)
            {
                Assert.NotNull(pressedKey);
                pressedKey.Value.Match(key => Assert.Equal(expectedDecodedSpecialKey.Value, key), key => throw new XunitException());
            }
            else if (expectedDecodedKey.HasValue)
            {
                Assert.NotNull(pressedKey);
                pressedKey.Value.Match(key => throw new XunitException(), key => Assert.Equal(expectedDecodedKey, key));
            }
            else
            {
                Assert.Null(pressedKey);
            }
        }