Exemplo n.º 1
0
        /// <summary>
        /// Detect the character encoding form this byte array.
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static DetectionResult DetectFromBytes(byte[] bytes)
        {
            var detector = new CharsetDetector();

            detector.Feed(bytes, 0, bytes.Length);
            return(detector.DataEnd());
        }
Exemplo n.º 2
0
        private static void ReadStream(Stream stream, long?maxBytes, CharsetDetector detector)
        {
            const int bufferSize = 1024;

            byte[] buff = new byte[bufferSize];
            int    read;
            long   readTotal = 0;

            var toRead = CalcToRead(maxBytes, readTotal, bufferSize);

            while ((read = stream.Read(buff, 0, toRead)) > 0)
            {
                detector.Feed(buff, 0, read);

                if (maxBytes != null)
                {
                    readTotal += read;
                    if (readTotal >= maxBytes)
                    {
                        return;
                    }

                    toRead = CalcToRead(maxBytes, readTotal, bufferSize);
                }

                if (detector._done)
                {
                    return;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Detect the character encoding form this byte array.
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static DetectionResult DetectFromBytes(byte[] bytes)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

            var detector = new CharsetDetector();

            detector.Feed(bytes, 0, bytes.Length);
            return(detector.DataEnd());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Detect the character encoding by reading the stream.
        ///
        /// Note: stream position is not reset before and after.
        /// </summary>
        /// <param name="stream">The steam. </param>
        public static DetectionResult DetectFromStream(Stream stream)
        {
            var detector = new CharsetDetector();

            byte[] buff = new byte[1024];
            int    read;

            while ((read = stream.Read(buff, 0, buff.Length)) > 0 && !detector._done)
            {
                detector.Feed(buff, 0, read);
            }
            return(detector.DataEnd());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Detect the character encoding by reading the stream.
        ///
        /// Note: stream position is not reset before and after.
        /// </summary>
        /// <param name="stream">The steam. </param>
        /// <param name="maxBytesToRead">max bytes to read from <paramref name="stream"/>. If <c>null</c>, then no max</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="maxBytesToRead"/> 0 or lower.</exception>
        public static DetectionResult DetectFromStream(Stream stream, long?maxBytesToRead)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (maxBytesToRead <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxBytesToRead));
            }

            var detector = new CharsetDetector();

            ReadStream(stream, maxBytesToRead, detector);
            return(detector.DataEnd());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Detect the character encoding form this byte array.
        /// It searchs for BOM from bytes[offset].
        /// </summary>
        /// <param name="bytes">The byte array containing the text</param>
        /// <param name="offset">The zero-based byte offset in buffer at which to begin reading the data from</param>
        /// <param name="len">The maximum number of bytes to be read</param>
        /// <returns></returns>
        public static DetectionResult DetectFromBytes(byte[] bytes, int offset, int len)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (len < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(len));
            }
            if (bytes.Length < offset + len)
            {
                throw new ArgumentException($"{nameof(len)} is greater than the number of bytes from {nameof(offset)} to the end of the array.");
            }

            var detector = new CharsetDetector();

            detector.Feed(bytes, offset, len);
            return(detector.DataEnd());
        }