Search in sources :

Example 1 with LibraryException

use of org.ansj.exception.LibraryException in project ansj_seg by NLPchina.

the class File2Stream method multiple.

private InputStream multiple(String path) throws FileNotFoundException {
    File[] libs = new File[0];
    File file = new File(path);
    if (file.exists() && file.canRead()) {
        if (file.isFile()) {
            libs = new File[1];
            libs[0] = file;
        } else if (file.isDirectory()) {
            File[] files = file.listFiles(new FileFilter() {

                public boolean accept(File file) {
                    return file.canRead() && !file.isHidden() && !file.isDirectory();
                }
            });
            if (files != null && files.length > 0) {
                libs = files;
            }
        }
    }
    if (libs.length == 0) {
        throw new LibraryException("not find any file in path : " + path);
    }
    if (libs.length == 1) {
        return new FileInputStream(libs[0]);
    }
    Vector<InputStream> vector = new Vector<>(libs.length);
    for (int i = 0; i < libs.length; i++) {
        vector.add(new FileInputStream(libs[i]));
    }
    return new SequenceInputStream(vector.elements());
}
Also used : SequenceInputStream(java.io.SequenceInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) LibraryException(org.ansj.exception.LibraryException) FileFilter(java.io.FileFilter) File(java.io.File) Vector(java.util.Vector) FileInputStream(java.io.FileInputStream)

Example 2 with LibraryException

use of org.ansj.exception.LibraryException in project ansj_seg by NLPchina.

the class File2Stream method toStream.

@Override
public InputStream toStream(String path) {
    LOG.info("path to stream " + path);
    if (path.startsWith("file://")) {
        path = path.substring(7);
    }
    File file = new File(path);
    if (file.exists() && file.canRead()) {
        try {
            if (file.isDirectory()) {
                return multiple(path);
            } else {
                return new FileInputStream(file);
            }
        } catch (Exception e) {
            throw new LibraryException(e);
        }
    }
    throw new LibraryException(" path :" + path + " file:" + file.getAbsolutePath() + " not found or can not to read");
}
Also used : LibraryException(org.ansj.exception.LibraryException) File(java.io.File) FileInputStream(java.io.FileInputStream) LibraryException(org.ansj.exception.LibraryException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with LibraryException

use of org.ansj.exception.LibraryException in project ansj_seg by NLPchina.

the class Jdbc2Stream method toStream.

@Override
public InputStream toStream(String path) {
    path = path.substring(7);
    String[] split = path.split("\\|");
    String jdbc = split[0];
    String username = split[1];
    String password = split[2];
    String sqlStr = split[3];
    String logStr = jdbc + "|" + username + "|********|" + sqlStr;
    SimpleDataSource ds = null;
    try {
        ds = new SimpleDataSource();
        ds.setJdbcUrl(jdbc);
        ds.setUsername(username);
        ds.setPassword(password);
        Dao dao = new NutDao(ds);
        Sql sql = Sqls.create(sqlStr);
        Sql execute = dao.execute(sql.setCallback(new SqlCallback() {

            @Override
            public byte[] invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                ByteArrayOutputStream baos = new ByteArrayOutputStream(100 * 1024);
                while (rs.next()) {
                    try {
                        int count = rs.getMetaData().getColumnCount();
                        for (int i = 1; i < count; i++) {
                            baos.write(String.valueOf(rs.getObject(i)).getBytes());
                            baos.write(TAB);
                        }
                        baos.write(String.valueOf(rs.getObject(count)).getBytes());
                        baos.write(LINE);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return baos.toByteArray();
            }
        }));
        return new ByteArrayInputStream((byte[]) execute.getResult());
    } catch (Exception e) {
        throw new LibraryException("err to load by jdbc " + logStr);
    } finally {
        if (ds != null) {
            ds.close();
        }
    }
}
Also used : NutDao(org.nutz.dao.impl.NutDao) Connection(java.sql.Connection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) SQLException(java.sql.SQLException) LibraryException(org.ansj.exception.LibraryException) Sql(org.nutz.dao.sql.Sql) Dao(org.nutz.dao.Dao) NutDao(org.nutz.dao.impl.NutDao) SqlCallback(org.nutz.dao.sql.SqlCallback) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleDataSource(org.nutz.dao.impl.SimpleDataSource) ResultSet(java.sql.ResultSet) LibraryException(org.ansj.exception.LibraryException)

Aggregations

LibraryException (org.ansj.exception.LibraryException)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileFilter (java.io.FileFilter)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Vector (java.util.Vector)1 Dao (org.nutz.dao.Dao)1 NutDao (org.nutz.dao.impl.NutDao)1 SimpleDataSource (org.nutz.dao.impl.SimpleDataSource)1 Sql (org.nutz.dao.sql.Sql)1 SqlCallback (org.nutz.dao.sql.SqlCallback)1