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());
}
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");
}
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();
}
}
}
Aggregations