Dataset Viewer
Auto-converted to Parquet Duplicate
file_id
stringlengths
3
9
content
stringlengths
24
35.8k
repo
stringlengths
6
89
path
stringlengths
5
169
token_length
int64
30
8.19k
original_comment
stringlengths
5
9.64k
comment_type
stringclasses
2 values
detected_lang
stringclasses
1 value
masked_comment
stringlengths
11
35.8k
3_1
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @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; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @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); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
7,110
/** * 判断两个输入流是否严格相等 */
block_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两 <SUF>*/ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @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; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @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); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_7
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @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; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @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); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
7,110
// 还可以写入的字节数
line_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可 <SUF> if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @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; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @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); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_8
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @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; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @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); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
7,110
// 减去
line_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 <SUF> else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @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; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @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); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_9
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
nutzam/nutz
src/org/nutz/lang/Streams.java
7,110
// 写够了
line_comment
zh-cn
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
3_11
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
nutzam/nutz
src/org/nutz/lang/Streams.java
7,110
// 啥都没写,强制触发一下写
line_comment
zh-cn
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
3_12
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
nutzam/nutz
src/org/nutz/lang/Streams.java
7,110
// 这是考虑到 walnut 的输出流实现,比如你写一个空文件
line_comment
zh-cn
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
3_13
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
nutzam/nutz
src/org/nutz/lang/Streams.java
7,110
// 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了
line_comment
zh-cn
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
3_14
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
nutzam/nutz
src/org/nutz/lang/Streams.java
7,110
// 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动
line_comment
zh-cn
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
3_15
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
nutzam/nutz
src/org/nutz/lang/Streams.java
7,110
// 所以这里触发一个写,它就知道,喔你要写个空喔。
line_comment
zh-cn
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
3_29
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
nutzam/nutz
src/org/nutz/lang/Streams.java
7,110
"/**\r\n * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流(...TRUNCATED)
block_comment
zh-cn
"package org.nutz.lang;\r\n\r\nimport java.io.BufferedInputStream;\r\nimport java.io.BufferedOutputS(...TRUNCATED)
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
8