Search in sources :

Example 31 with Reader

use of java.io.Reader in project cogtool by cogtool.

the class ObjectPersister method load.

// registerForPersistence
/**
     * Load an Object from a file.  This method assumes that all error checking
     * and prompting/correction for the validity of the file has already
     * been completed.
     *
     * @param src the File location of the saved Object
     * @return a new instantiated Object
     * @throws java.io.IOException if any file operation fails or the SAX XML
     *         parser fails
     */
public Object load(File src) throws IOException {
    // See if an object is already loaded for the given file
    String canonicalFileName = src.getCanonicalPath();
    PersistInfo info = getInfoByName(canonicalFileName);
    if (info != null) {
        return info.obj;
    }
    // Attempt to create a file of 3x size in the temp directory
    // to hold the expanded XML serialization.
    File sizeCheckFile = File.createTempFile(tmpFilePrefix, ".size", tmpDir);
    try {
        checkDiskSpace(sizeCheckFile, 3 * src.length(), "Not enough temp space on disk to open file: " + src);
    } finally {
        sizeCheckFile.delete();
    }
    // If we're here, no exception was thrown; create checkpoint directory
    File chkptFile = createTempDirectory();
    // Open file as ZipFile and decompress
    ZipFile zip = null;
    try {
        zip = new ZipFile(src);
        // Unzip the file to the checkpoint directory
        ZipUtil.unzip(zip, chkptFile);
    } catch (ZipException ex) {
        IOException newE = new IOException("load encountered zip compression error");
        newE.initCause(ex);
        throw newE;
    } finally {
        if (zip != null) {
            zip.close();
        }
    }
    // Load object from the expanded XML serialization
    ObjectLoader l = new ObjectLoader();
    Object obj = null;
    Reader reader = null;
    try {
        reader = new InputStreamReader(new FileInputStream(new File(chkptFile, PERSIST_FILE)), "UTF-8");
        Collection<?> objSet = l.load(new InputSource(reader), null);
        // There should be only one top-level object
        Iterator<?> objs = objSet.iterator();
        if (objs.hasNext()) {
            obj = objs.next();
        }
        // Register this file for future lookup, both by object
        // and by file name
        info = new PersistInfo(obj, chkptFile, src);
        objInfos.put(obj, info);
        fileInfos.put(canonicalFileName, info);
    } catch (ParserConfigurationException e) {
        IOException newE = new IOException("load encountered parser error");
        newE.initCause(e);
        throw newE;
    } catch (SAXException e) {
        IOException newE = new IOException("load encountered SAX error");
        newE.initCause(e);
        throw newE;
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return obj;
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileReader(java.io.FileReader) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) ZipFile(java.util.zip.ZipFile) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 32 with Reader

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

the class CheBootstrap method readConfigurationAliases.

private Map<String, Set<String>> readConfigurationAliases() {
    URL aliasesResource = getClass().getClassLoader().getResource(PROPERTIES_ALIASES_CONFIG_FILE);
    Map<String, Set<String>> aliases = new HashMap<>();
    if (aliasesResource != null) {
        Properties properties = new Properties();
        File aliasesFile = new File(aliasesResource.getFile());
        try (Reader reader = Files.newReader(aliasesFile, Charset.forName("UTF-8"))) {
            properties.load(reader);
        } catch (IOException e) {
            throw new IllegalStateException(format("Unable to read configuration aliases from file %s", aliasesFile), e);
        }
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            String value = (String) entry.getValue();
            aliases.put((String) entry.getKey(), Splitter.on(',').splitToList(value).stream().map(String::trim).collect(toSet()));
        }
    }
    return aliases;
}
Also used : Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) HashMap(java.util.HashMap) Reader(java.io.Reader) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with Reader

use of java.io.Reader in project sharding-jdbc by dangdangdotcom.

the class PreparedStatementAdapterTest method assertSetClob.

@Test
public void assertSetClob() throws SQLException {
    Reader reader = new SerializableStringReader();
    actual.setClob(1, (Clob) null);
    actual.setClob(2, reader);
    actual.setClob(3, reader, 100L);
    assertParameter(actual, 1, null);
    assertParameter(actual, 2, reader);
    assertParameter(actual, 3, reader);
}
Also used : Reader(java.io.Reader) StringReader(java.io.StringReader) AbstractShardingDataBasesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest) Test(org.junit.Test)

Example 34 with Reader

use of java.io.Reader in project sharding-jdbc by dangdangdotcom.

the class PreparedStatementAdapterTest method assertSetCharacterStream.

@Test
public void assertSetCharacterStream() throws SQLException {
    Reader reader = new SerializableStringReader();
    actual.setCharacterStream(1, reader);
    actual.setCharacterStream(2, reader, 100);
    actual.setCharacterStream(3, reader, 100L);
    assertParameter(actual, 1, reader);
    assertParameter(actual, 2, reader);
    assertParameter(actual, 3, reader);
}
Also used : Reader(java.io.Reader) StringReader(java.io.StringReader) AbstractShardingDataBasesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.db.AbstractShardingDataBasesOnlyDBUnitTest) Test(org.junit.Test)

Example 35 with Reader

use of java.io.Reader in project druid by druid-io.

the class HadoopDruidIndexerConfig method fromDistributedFileSystem.

@SuppressWarnings("unchecked")
public static HadoopDruidIndexerConfig fromDistributedFileSystem(String path) {
    try {
        Path pt = new Path(path);
        FileSystem fs = pt.getFileSystem(new Configuration());
        Reader reader = new InputStreamReader(fs.open(pt));
        return fromMap((Map<String, Object>) HadoopDruidIndexerConfig.JSON_MAPPER.readValue(reader, new TypeReference<Map<String, Object>>() {
        }));
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) InputStreamReader(java.io.InputStreamReader) FileSystem(org.apache.hadoop.fs.FileSystem) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Map(java.util.Map) 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