Search in sources :

Example 36 with Reader

use of java.io.Reader in project hackpad by dropbox.

the class CachingModuleScriptProviderBase method getModuleScript.

public ModuleScript getModuleScript(Context cx, String moduleId, URI moduleUri, Scriptable paths) throws Exception {
    final CachedModuleScript cachedModule1 = getLoadedModule(moduleId);
    final Object validator1 = getValidator(cachedModule1);
    final ModuleSource moduleSource = (moduleUri == null) ? moduleSourceProvider.loadSource(moduleId, paths, validator1) : moduleSourceProvider.loadSource(moduleUri, validator1);
    if (moduleSource == ModuleSourceProvider.NOT_MODIFIED) {
        return cachedModule1.getModule();
    }
    if (moduleSource == null) {
        return null;
    }
    final Reader reader = moduleSource.getReader();
    try {
        final int idHash = moduleId.hashCode();
        synchronized (loadLocks[(idHash >>> loadLockShift) & loadLockMask]) {
            final CachedModuleScript cachedModule2 = getLoadedModule(moduleId);
            if (cachedModule2 != null) {
                if (!equal(validator1, getValidator(cachedModule2))) {
                    return cachedModule2.getModule();
                }
            }
            final URI sourceUri = moduleSource.getUri();
            final ModuleScript moduleScript = new ModuleScript(cx.compileReader(reader, sourceUri.toString(), 1, moduleSource.getSecurityDomain()), sourceUri, moduleSource.getBase());
            putLoadedModule(moduleId, moduleScript, moduleSource.getValidator());
            return moduleScript;
        }
    } finally {
        reader.close();
    }
}
Also used : Reader(java.io.Reader) ModuleScript(org.mozilla.javascript.commonjs.module.ModuleScript) URI(java.net.URI)

Example 37 with Reader

use of java.io.Reader in project elasticsearch by elastic.

the class MustacheScriptEngineService method compile.

/**
     * Compile a template string to (in this case) a Mustache object than can
     * later be re-used for execution to fill in missing parameter values.
     *
     * @param templateSource a string representing the template to compile.
     * @return a compiled template object for later execution.
     * */
@Override
public Object compile(String templateName, String templateSource, Map<String, String> params) {
    final MustacheFactory factory = createMustacheFactory(params);
    Reader reader = new FastStringReader(templateSource);
    return factory.compile(reader, "query-template");
}
Also used : FastStringReader(org.elasticsearch.common.io.FastStringReader) FastStringReader(org.elasticsearch.common.io.FastStringReader) Reader(java.io.Reader) MustacheFactory(com.github.mustachejava.MustacheFactory)

Example 38 with Reader

use of java.io.Reader in project elasticsearch by elastic.

the class IcuTokenizerFactoryTests method testIcuCustomizeRuleFile.

public void testIcuCustomizeRuleFile() throws IOException {
    TestAnalysis analysis = createTestAnalysis();
    // test the tokenizer with single rule file
    TokenizerFactory tokenizerFactory = analysis.tokenizer.get("user_rule_tokenizer");
    ICUTokenizer tokenizer = (ICUTokenizer) tokenizerFactory.create();
    Reader reader = new StringReader("One-two punch.  Brang-, not brung-it.  This one--not that one--is the right one, -ish.");
    tokenizer.setReader(reader);
    assertTokenStreamContents(tokenizer, new String[] { "One-two", "punch", "Brang", "not", "brung-it", "This", "one", "not", "that", "one", "is", "the", "right", "one", "ish" });
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) ICUTokenizer(org.apache.lucene.analysis.icu.segmentation.ICUTokenizer)

Example 39 with Reader

use of java.io.Reader in project elasticsearch by elastic.

the class IcuTokenizerFactoryTests method testSimpleIcuTokenizer.

public void testSimpleIcuTokenizer() throws IOException {
    TestAnalysis analysis = createTestAnalysis();
    TokenizerFactory tokenizerFactory = analysis.tokenizer.get("icu_tokenizer");
    ICUTokenizer tokenizer = (ICUTokenizer) tokenizerFactory.create();
    Reader reader = new StringReader("向日葵, one-two");
    tokenizer.setReader(reader);
    assertTokenStreamContents(tokenizer, new String[] { "向日葵", "one", "two" });
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) ICUTokenizer(org.apache.lucene.analysis.icu.segmentation.ICUTokenizer)

Example 40 with Reader

use of java.io.Reader in project che by eclipse.

the class JavadocContentAccess2 method getContentsFromInputStream.

private static String getContentsFromInputStream(InputStream in, String encoding) throws CoreException {
    final int defaultFileSize = 15 * 1024;
    StringBuffer buffer = new StringBuffer(defaultFileSize);
    Reader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(in, encoding), defaultFileSize);
        char[] readBuffer = new char[2048];
        int charCount = reader.read(readBuffer);
        while (charCount > 0) {
            buffer.append(readBuffer, 0, charCount);
            charCount = reader.read(readBuffer);
        }
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, "JavaPlugin.getPluginId()", e.getMessage(), e));
    } finally {
        try {
            if (reader != null) {
                //this will also close the InputStream wrapped in the reader
                reader.close();
            }
        } catch (IOException e) {
        //ignore
        }
    }
    return buffer.toString();
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) InputStreamReader(java.io.InputStreamReader) CoreException(org.eclipse.core.runtime.CoreException) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Aggregations

Reader (java.io.Reader)1498 InputStreamReader (java.io.InputStreamReader)526 StringReader (java.io.StringReader)498 IOException (java.io.IOException)348 BufferedReader (java.io.BufferedReader)242 InputStream (java.io.InputStream)219 TokenStream (org.apache.lucene.analysis.TokenStream)171 Test (org.junit.Test)170 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)159 Connection (java.sql.Connection)137 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)126 FileReader (java.io.FileReader)108 FileInputStream (java.io.FileInputStream)107 File (java.io.File)105 BeforeClass (org.junit.BeforeClass)99 Tokenizer (org.apache.lucene.analysis.Tokenizer)91 SqlSession (org.apache.ibatis.session.SqlSession)83 StringWriter (java.io.StringWriter)81 ArrayList (java.util.ArrayList)77 Writer (java.io.Writer)63