public static void loadProperties(String fileName) { InputStream is = null; try { pp = new Properties(); File f = Files.findFile(fileName); if (f == null) throw new RuntimeException("nutz-test.properties Not FOUND!!!"); is = Streams.fileIn(f); pp.load(is); pp.list(System.out); } catch (Exception e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(is); } }
/** * 根据一个文件路径建立一个输入流 * * @param file 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } }
@At @Ok("void") public void resource( @Param("deploymentId") String deploymentId, @Param("resourceName") String resourceName) throws IOException { Streams.writeAndClose( Mvcs.getResp().getOutputStream(), flowService.resource(deploymentId, resourceName)); }
/** * 从一个流读取数据库配置 * * @param in 输入流,包含配置信息 * @throws IOException 读取失败是抛出异常 */ public void init(InputStream in) throws IOException { Properties props = new Properties(); try { props.load(in); init(props); } finally { Streams.safeClose(in); } }
/** * 将一段字符串写入一个文本输出流,并将该流关闭 * * @param writer 文本输出流 * @param str 字符串 */ public static void writeAll(Writer writer, String str) { try { writer.write(str); writer.flush(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(writer); } }
/** 根据Markdown生成文档 */ @At({"/links/?"}) @Ok("beetl:/yvr/website/links.btl") public Object page(String type) throws IOException { String path = "/doc/" + type + ".md"; InputStream ins = getClass().getClassLoader().getResourceAsStream(path); if (ins == null) return HTTP_404; String cnt = Streams.readAndClose(new InputStreamReader(ins)); String[] tmp = cnt.split("\n", 2); String title = tmp[0].trim().split(" ", 2)[1].trim(); return _map("title", title, "cnt", cnt); }
/** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; }
// 判断编译等级 public static boolean isJDK6() { InputStream is = null; try { String classFileName = "/" + Lang.class.getName().replace('.', '/') + ".class"; is = Lang.class.getClassLoader().getResourceAsStream(classFileName); if (is != null && is.available() > 8) { is.skip(7); return is.read() > 49; } } catch (Throwable e) { } finally { Streams.safeClose(is); } return false; }
@Test public void test_blob() throws IOException { // For mysql only if (dao.meta().isMySql()) { dao.create(BinObject.class, true); BinObject obj = new BinObject(); obj.setXblob(new ByteArrayInputStream("中文".getBytes())); obj.setXclob(new StringReader("不是英文")); dao.insert(obj); BinObject db_obj = dao.fetch(BinObject.class); assertTrue(Streams.equals(new ByteArrayInputStream("中文".getBytes()), db_obj.getXblob())); assertEquals("不是英文", Lang.readAll(db_obj.getXclob())); } }
@Test // 如果报错且mysql的话,设置数据库的max_allowed_packet属性哦 public void test_big_blob() throws IOException { String path = "~/tmp/big.blob"; Files.createFileIfNoExists(path); OutputStream fos = Streams.fileOut(path); for (int i = 0; i < 5 * 1024; i++) { // 更多的数据需要不同的类型 fos.write(new byte[1024]); } fos.close(); dao.create(TheGoods.class, true); TheGoods tg = TheGoods.create("AAA", path); dao.insert(tg); new File(path).delete(); }
/** * 从一个文本输入流读取所有内容,并将该流关闭 * * @param reader 文本输入流 * @return 输入流所有内容 */ public static String readAll(Reader reader) { if (!(reader instanceof BufferedReader)) reader = new BufferedReader(reader); try { StringBuilder sb = new StringBuilder(); char[] data = new char[64]; int len; while (true) { if ((len = reader.read(data)) == -1) break; sb.append(data, 0, len); } return sb.toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(reader); } }
/** * 根据一个文件路径建立一个输入流 * * @param path 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) { } } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); }
public InputStream getAsciiStream() throws SQLException { return Streams.buff(Streams.fileIn(file)); }
public Reader getCharacterStream() throws SQLException { return Streams.fileInr(file); }