Exemple #1
0
        /// <summary>
        /// Load synonyms with the given <seealso cref="SynonymMap.Parser"/> class.
        /// </summary>
        private SynonymMap LoadSynonyms(ResourceLoader loader, string cname, bool dedup, Analyzer analyzer)
        {
            CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);

            SynonymMap.Parser parser;
            Type clazz = loader.findClass(cname, typeof(SynonymMap.Parser));

            try
            {
                parser = clazz.getConstructor(typeof(bool), typeof(bool), typeof(Analyzer)).newInstance(dedup, expand, analyzer);
            }
            catch (Exception e)
            {
                throw new Exception(e);
            }

            if (File.Exists(synonyms))
            {
                decoder.Reset();
                parser.Parse(new InputStreamReader(loader.openResource(synonyms), decoder));
            }
            else
            {
                IList <string> files = splitFileNames(synonyms);
                foreach (string file in files)
                {
                    decoder.reset();
                    parser.Parse(new InputStreamReader(loader.openResource(file), decoder));
                }
            }
            return(parser.Build());
        }
Exemple #2
0
            internal virtual char[] Decode(sbyte[] ba, int off, int len)
            {
                int en = Scale(len, Cd.MaxCharsPerByte());

                char[] ca = new char[en];
                if (len == 0)
                {
                    return(ca);
                }
                if (Cd is ArrayDecoder)
                {
                    int clen = ((ArrayDecoder)Cd).decode(ba, off, len, ca);
                    return(SafeTrim(ca, clen, Cs, IsTrusted));
                }
                else
                {
                    Cd.Reset();
                    ByteBuffer bb = ByteBuffer.Wrap(ba, off, len);
                    CharBuffer cb = CharBuffer.Wrap(ca);
                    try
                    {
                        CoderResult cr = Cd.Decode(bb, cb, true);
                        if (!cr.Underflow)
                        {
                            cr.ThrowException();
                        }
                        cr = Cd.Flush(cb);
                        if (!cr.Underflow)
                        {
                            cr.ThrowException();
                        }
                    }
                    catch (CharacterCodingException x)
                    {
                        // Substitution is always enabled,
                        // so this shouldn't happen
                        throw new Error(x);
                    }
                    return(SafeTrim(ca, cb.Position(), Cs, IsTrusted));
                }
            }
Exemple #3
0
        // -- Character streams from channels --

        /// <summary>
        /// Constructs a reader that decodes bytes from the given channel using the
        /// given decoder.
        ///
        /// <para> The resulting stream will contain an internal input buffer of at
        /// least <tt>minBufferCap</tt> bytes.  The stream's <tt>read</tt> methods
        /// will, as needed, fill the buffer by reading bytes from the underlying
        /// channel; if the channel is in non-blocking mode when bytes are to be
        /// read then an <seealso cref="IllegalBlockingModeException"/> will be thrown.  The
        /// resulting stream will not otherwise be buffered, and it will not support
        /// the <seealso cref="Reader#mark mark"/> or <seealso cref="Reader#reset reset"/> methods.
        /// Closing the stream will in turn cause the channel to be closed.  </para>
        /// </summary>
        /// <param name="ch">
        ///         The channel from which bytes will be read
        /// </param>
        /// <param name="dec">
        ///         The charset decoder to be used
        /// </param>
        /// <param name="minBufferCap">
        ///         The minimum capacity of the internal byte buffer,
        ///         or <tt>-1</tt> if an implementation-dependent
        ///         default capacity is to be used
        /// </param>
        /// <returns>  A new reader </returns>
        public static Reader NewReader(ReadableByteChannel ch, CharsetDecoder dec, int minBufferCap)
        {
            CheckNotNull(ch, "ch");
            return(StreamDecoder.forDecoder(ch, dec.Reset(), minBufferCap));
        }