Beispiel #1
0
        /**
         * Detects encoding from a stream.
         *
         * @param inp the stream
         * @return a Reader with the deduced encoding.
         * @throws IOException if IO went wrong
         * @throws UnsupportedEncodingException if unsupported encoding was detected
         */
        virtual public StreamReader DetectEncoding(Stream inp)
        {
            byte[] b4    = new byte[4];
            int    count = inp.Read(b4, 0, b4.Length);

            if (count != 4)
            {
                throw new IOException("Insufficient length");
            }
            String encoding    = XMLUtil.GetEncodingName(b4);
            String decl        = null;
            int    bytesNumber = 0;

            if (encoding.Equals("UTF-8"))
            {
                StringBuilder sb = new StringBuilder();
                int           c;
                while (bytesNumber < 1028 && ((c = inp.ReadByte()) != -1))
                {
                    if (c == '>')
                    {
                        break;
                    }
                    sb.Append((char)c);
                    bytesNumber++;
                }
                decl = sb.ToString();
            }
            else if (encoding.Equals("CP037"))
            {
                MemoryStream bi = new MemoryStream();
                int          c;
                while (bytesNumber < 1028 && ((c = inp.ReadByte()) != -1))
                {
                    if (c == 0x6e) // that's '>' in ebcdic
                    {
                        break;
                    }
                    bi.WriteByte((byte)c);
                    bytesNumber++;
                }
                decl = Encoding.GetEncoding(37).GetString(bi.ToArray());
            }
            if (decl != null)
            {
                decl = EncodingUtil.GetDeclaredEncoding(decl);
                if (decl != null)
                {
                    encoding = decl;
                }
            }
            if (inp.CanSeek)
            {
                inp.Seek(0, SeekOrigin.Begin);
            }
            return(new StreamReader(inp, IanaEncodings.GetEncodingEncoding(encoding)));
        }
Beispiel #2
0
        /**
         * Parses the XML document firing the events to the handler.
         * @param doc the document handler
         * @param in the document. The encoding is deduced from the stream. The stream is not closed
         * @throws IOException on error
         */
        public static void Parse(ISimpleXMLDocHandler doc, Stream inp)
        {
            byte[] b4    = new byte[4];
            int    count = inp.Read(b4, 0, b4.Length);

            if (count != 4)
            {
                throw new IOException(MessageLocalization.GetComposedMessage("insufficient.length"));
            }
            String encoding = XMLUtil.GetEncodingName(b4);
            String decl     = null;

            if (encoding.Equals("UTF-8"))
            {
                StringBuilder sb = new StringBuilder();
                int           c;
                while ((c = inp.ReadByte()) != -1)
                {
                    if (c == '>')
                    {
                        break;
                    }
                    sb.Append((char)c);
                }
                decl = sb.ToString();
            }
            else if (encoding.Equals("CP037"))
            {
                MemoryStream bi = new MemoryStream();
                int          c;
                while ((c = inp.ReadByte()) != -1)
                {
                    if (c == 0x6e) // that's '>' in ebcdic
                    {
                        break;
                    }
                    bi.WriteByte((byte)c);
                }
                decl = Encoding.GetEncoding(37).GetString(bi.ToArray());//cp037 ebcdic
            }
            if (decl != null)
            {
                decl = GetDeclaredEncoding(decl);
                if (decl != null)
                {
                    encoding = decl;
                }
            }
            Parse(doc, new StreamReader(inp, IanaEncodings.GetEncodingEncoding(encoding)));
        }