Class IoKit

java.lang.Object
org.aoju.bus.core.toolkit.IoKit

public class IoKit extends Object
IO工具类 IO工具类只是辅助流的读写,并不负责关闭流 原因是流可能被多次读写,读写关闭后容易造成问题
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • DEFAULT_BUFFER_SIZE

      public static final int DEFAULT_BUFFER_SIZE
      默认缓存大小 8192
      See Also:
    • DEFAULT_LARGE_BUFFER_SIZE

      public static final int DEFAULT_LARGE_BUFFER_SIZE
      默认大缓存大小 32768
      See Also:
    • EOF

      public static final int EOF
      数据流末尾
      See Also:
  • Method Details

    • checkOffsetAndCount

      public static void checkOffsetAndCount(long size, long offset, long byteCount)
    • reverseBytesShort

      public static short reverseBytesShort(short s)
    • reverseBytesInt

      public static int reverseBytesInt(int i)
    • reverseBytesLong

      public static long reverseBytesLong(long v)
    • sneakyRethrow

      public static void sneakyRethrow(Throwable t)
      即使被声明也不允许直接抛出 这是一种很糟糕的做饭,很容易遭到攻击 清理后捕获并重新抛出异常 参见Java Puzzlers #43
      Parameters:
      t - 异常
    • arrayRangeEquals

      public static boolean arrayRangeEquals(byte[] a, int aOffset, byte[] b, int bOffset, int byteCount)
    • copy

      public static long copy(Reader reader, Writer writer)
      将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Reader
      Parameters:
      reader - Reader
      writer - Writer
      Returns:
      拷贝的字节数
    • copy

      public static long copy(Reader reader, Writer writer, int bufferSize)
      将Reader中的内容复制到Writer中,拷贝后不关闭Reader
      Parameters:
      reader - Reader
      writer - Writer
      bufferSize - 缓存大小
      Returns:
      传输的byte数
    • copy

      public static long copy(Reader reader, Writer writer, int bufferSize, Progress progress)
      将Reader中的内容复制到Writer中,拷贝后不关闭Reader
      Parameters:
      reader - Reader
      writer - Writer
      bufferSize - 缓存大小
      progress - 进度处理器
      Returns:
      传输的byte数
    • copy

      public static long copy(Reader reader, Writer writer, int bufferSize, long count, Progress progress)
      将Reader中的内容复制到Writer中,拷贝后不关闭Reader
      Parameters:
      reader - Reader,非空
      writer - Writer,非空
      bufferSize - 缓存大小,-1表示默认
      count - 最大长度,-1表示无限制
      progress - 进度处理器,null表示无
      Returns:
      传输的byte数
    • copy

      public static long copy(InputStream in, OutputStream out)
      拷贝流,使用默认Buffer大小,拷贝后不关闭流
      Parameters:
      in - 输入流
      out - 输出流
      Returns:
      传输的byte数
    • copy

      public static long copy(InputStream in, OutputStream out, int bufferSize)
      拷贝流,拷贝后不关闭流
      Parameters:
      in - 输入流
      out - 输出流
      bufferSize - 缓存大小
      Returns:
      传输的byte数
    • copy

      public static long copy(InputStream in, OutputStream out, int bufferSize, Progress progress)
      拷贝流,拷贝后不关闭流
      Parameters:
      in - 输入流
      out - 输出流
      bufferSize - 缓存大小
      progress - 进度条
      Returns:
      传输的byte数
    • copy

      public static long copy(InputStream in, OutputStream out, int bufferSize, long count, Progress progress)
      拷贝流,拷贝后不关闭流
      Parameters:
      in - 输入流
      out - 输出流
      bufferSize - 缓存大小
      count - 总拷贝长度,-1表示无限制
      progress - 进度条
      Returns:
      传输的byte数
    • copy

      public static long copy(FileChannel inChannel, FileChannel outChannel) throws InternalException
      拷贝文件Channel,使用NIO,拷贝后不会关闭channel
      Parameters:
      inChannel - FileChannel
      outChannel - FileChannel
      Returns:
      拷贝的字节数
      Throws:
      InternalException - IO异常
    • copy

      public static long copy(ReadableByteChannel in, WritableByteChannel out) throws InternalException
      拷贝流,使用NIO,不会关闭channel
      Parameters:
      in - ReadableByteChannel
      out - WritableByteChannel
      Returns:
      拷贝的字节数
      Throws:
      InternalException - IO异常
    • copy

      public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize) throws InternalException
      拷贝流,使用NIO,不会关闭channel
      Parameters:
      in - ReadableByteChannel
      out - WritableByteChannel
      bufferSize - 缓冲大小,如果小于等于0,使用默认
      Returns:
      拷贝的字节数
      Throws:
      InternalException - IO异常
    • copy

      public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize, Progress progress) throws InternalException
      拷贝流,使用NIO,不会关闭channel
      Parameters:
      in - ReadableByteChannel
      out - WritableByteChannel
      bufferSize - 缓冲大小,如果小于等于0,使用默认
      progress - Progress进度处理器
      Returns:
      拷贝的字节数
      Throws:
      InternalException - IO异常
    • copy

      public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize, long count, Progress progress) throws InternalException
      拷贝流,使用NIO,不会关闭channel
      Parameters:
      in - ReadableByteChannel
      out - WritableByteChannel
      bufferSize - 缓冲大小,如果小于等于0,使用默认
      count - 读取总长度
      progress - Progress进度处理器
      Returns:
      拷贝的字节数
      Throws:
      InternalException - IO异常
    • copy

      public static long copy(FileInputStream in, FileOutputStream out)
      文件拷贝实现
      Parameters:
      in - 输入
      out - 输出
      Returns:
      拷贝的字节数
    • getReader

      public static BufferedReader getReader(InputStream in, String charsetName)
      获得一个文件读取器
      Parameters:
      in - 输入流
      charsetName - 字符集名称
      Returns:
      BufferedReader对象
    • getReader

      public static BufferedReader getReader(InputStream in, Charset charset)
      获得一个Reader
      Parameters:
      in - 输入流
      charset - 字符集
      Returns:
      BufferedReader对象
    • getReader

      public static BufferedReader getReader(BOMInputStream in)
      BOMInputStream中获取Reader
      Parameters:
      in - BOMInputStream
      Returns:
      BufferedReader
    • getReader

      public static BOMReader getReader(InputStream in)
      Parameters:
      in - InputStream
      Returns:
      BOMReader
    • getReader

      public static BufferedReader getReader(Reader reader)
      获得BufferedReader 如果是BufferedReader强转返回,否则新建 如果提供的Reader为null返回null
      Parameters:
      reader - 普通Reader,如果为null返回null
      Returns:
      BufferedReader or null
    • getPushBackReader

      public static PushbackReader getPushBackReader(Reader reader, int pushBackSize)
      获得PushbackReader 如果是PushbackReader强转返回,否则新建
      Parameters:
      reader - 普通Reader
      pushBackSize - 推后的byte数
      Returns:
      PushbackReader
    • getWriter

      public static OutputStreamWriter getWriter(OutputStream out, String charsetName)
      获得一个Writer
      Parameters:
      out - 输入流
      charsetName - 字符集
      Returns:
      OutputStreamWriter对象
    • getWriter

      public static OutputStreamWriter getWriter(OutputStream out, Charset charset)
      获得一个Writer
      Parameters:
      out - 输入流
      charset - 字符集
      Returns:
      OutputStreamWriter对象
    • read

      public static String read(InputStream in, String charsetName) throws InternalException
      从流中读取内容
      Parameters:
      in - 输入流
      charsetName - 字符集
      Returns:
      内容
      Throws:
      InternalException - 异常
    • read

      public static String read(InputStream in, Charset charset) throws InternalException
      从流中读取内容,读取完毕后关闭流
      Parameters:
      in - 输入流,读取完毕后并不关闭流
      charset - 字符集
      Returns:
      内容
      Throws:
      InternalException - 异常
    • read

      public static FastByteOutputStream read(InputStream in) throws InternalException
      从流中读取内容,读到输出流中,读取完毕后关闭流
      Parameters:
      in - 输入流
      Returns:
      输出流
      Throws:
      InternalException - 异常
    • read

      public static FastByteOutputStream read(InputStream in, boolean isClose) throws InternalException
      从流中读取内容,读到输出流中,读取完毕后可选是否关闭流
      Parameters:
      in - 输入流
      isClose - 读取完毕后是否关闭流
      Returns:
      输出流
      Throws:
      InternalException - IO异常
    • read

      public static String read(Reader reader) throws InternalException
      从Reader中读取String,读取完毕后并关闭Reader
      Parameters:
      reader - Reader
      Returns:
      String
      Throws:
      InternalException - 异常
    • read

      public static String read(Reader reader, boolean isClose) throws InternalException
      Reader中读取String
      Parameters:
      reader - Reader
      isClose - 是否关闭Reader
      Returns:
      String
      Throws:
      InternalException - IO异常
    • read

      public static String read(FileChannel fileChannel) throws InternalException
      从FileChannel中读取UTF-8编码内容
      Parameters:
      fileChannel - 文件管道
      Returns:
      内容
      Throws:
      InternalException - 异常
    • read

      public static String read(FileChannel fileChannel, String charsetName) throws InternalException
      从FileChannel中读取内容,读取完毕后并不关闭Channel
      Parameters:
      fileChannel - 文件管道
      charsetName - 字符集
      Returns:
      内容
      Throws:
      InternalException - 异常
    • read

      public static String read(FileChannel fileChannel, Charset charset) throws InternalException
      从FileChannel中读取内容
      Parameters:
      fileChannel - 文件管道
      charset - 字符集
      Returns:
      内容
      Throws:
      InternalException - 异常
    • read

      public static String read(ReadableByteChannel channel, Charset charset) throws InternalException
      从流中读取内容,读取完毕后并不关闭流
      Parameters:
      channel - 可读通道,读取完毕后并不关闭通道
      charset - 字符集
      Returns:
      内容
      Throws:
      InternalException - IO异常
    • read

      public static FastByteOutputStream read(ReadableByteChannel channel) throws InternalException
      从流中读取内容,读到输出流中
      Parameters:
      channel - 可读通道,读取完毕后并不关闭通道
      Returns:
      输出流
      Throws:
      InternalException - IO异常
    • readBytes

      public static byte[] readBytes(InputStream in) throws InternalException
      从流中读取bytes
      Parameters:
      in - InputStream
      Returns:
      bytes
      Throws:
      InternalException - 异常
    • readBytes

      public static byte[] readBytes(InputStream in, boolean isClose) throws InternalException
      从流中读取bytes
      Parameters:
      in - InputStream
      isClose - 是否关闭输入流
      Returns:
      bytes
      Throws:
      InternalException - IO异常
    • readBytes

      public static byte[] readBytes(InputStream in, int length) throws InternalException
      读取指定长度的byte数组,不关闭流
      Parameters:
      in - InputStream,为null返回null
      length - 长度,小于等于0返回空byte数组
      Returns:
      bytes
      Throws:
      InternalException - 异常
    • readHex

      public static String readHex(InputStream in, int length, boolean toLowerCase) throws InternalException
      读取16进制字符串
      Parameters:
      in - InputStream
      length - 长度
      toLowerCase - true 传换成小写格式 , false 传换成大写格式
      Returns:
      16进制字符串
      Throws:
      InternalException - 异常
    • readHex28Upper

      public static String readHex28Upper(InputStream in) throws InternalException
      从流中读取前28个byte并转换为16进制,字母部分使用大写
      Parameters:
      in - InputStream
      Returns:
      16进制字符串
      Throws:
      InternalException - 异常
    • readHex28Lower

      public static String readHex28Lower(InputStream in) throws InternalException
      从流中读取前28个byte并转换为16进制,字母部分使用小写
      Parameters:
      in - InputStream
      Returns:
      16进制字符串
      Throws:
      InternalException - 异常
    • readObject

      public static <T> T readObject(InputStream in) throws InternalException
      从流中读取内容,读到输出流中
      Type Parameters:
      T - 读取对象的类型
      Parameters:
      in - 输入流
      Returns:
      输出流
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(InputStream in, T collection) throws InternalException
      从流中读取内容,使用UTF-8编码
      Type Parameters:
      T - 集合类型
      Parameters:
      in - 输入流
      collection - 返回集合
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(InputStream in, String charsetName, T collection) throws InternalException
      从流中读取内容
      Type Parameters:
      T - 集合类型
      Parameters:
      in - 输入流
      charsetName - 字符集
      collection - 返回集合
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(InputStream in, Charset charset, T collection) throws InternalException
      从流中读取内容
      Type Parameters:
      T - 集合类型
      Parameters:
      in - 输入流
      charset - 字符集
      collection - 返回集合
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(Reader reader, T collection) throws InternalException
      从Reader中读取内容
      Type Parameters:
      T - 集合类型
      Parameters:
      reader - Reader
      collection - 返回集合
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readLines

      public static void readLines(InputStream in, XConsumer<String> lineHandler) throws InternalException
      按行读取UTF-8编码数据,针对每行的数据做处理
      Parameters:
      in - InputStream
      lineHandler - 行处理接口,实现accept方法用于编辑一行的数据后入到指定地方
      Throws:
      InternalException - 异常
    • readLines

      public static void readLines(InputStream in, Charset charset, XConsumer<String> lineHandler) throws InternalException
      按行读取数据,针对每行的数据做处理
      Parameters:
      in - InputStream
      charset - Charset编码
      lineHandler - 行处理接口,实现accept方法用于编辑一行的数据后入到指定地方
      Throws:
      InternalException - 异常
    • readLines

      public static void readLines(Reader reader, XConsumer<String> lineHandler) throws InternalException
      按行读取数据,针对每行的数据做处理 Reader自带编码定义,因此读取数据的编码跟随其编码
      Parameters:
      reader - Reader
      lineHandler - 行处理接口,实现accept方法用于编辑一行的数据后入到指定地方
      Throws:
      InternalException - 异常
    • toStream

      public static ByteArrayInputStream toStream(String content, String charsetName)
      String 转为流
      Parameters:
      content - 内容
      charsetName - 编码
      Returns:
      字节流
    • toStream

      public static ByteArrayInputStream toStream(String content, Charset charset)
      String 转为流
      Parameters:
      content - 内容
      charset - 编码
      Returns:
      字节流
    • toStream

      public static ByteArrayInputStream toStream(String content)
      String 转为UTF-8编码的字节流流
      Parameters:
      content - 内容
      Returns:
      字节流
    • toStream

      public static FileInputStream toStream(File file)
      文件转为流
      Parameters:
      file - 文件
      Returns:
      FileInputStream
    • toStream

      public static ByteArrayInputStream toStream(byte[] content)
      String 转为流
      Parameters:
      content - 内容bytes
      Returns:
      字节流
    • toStream

      public static ByteArrayInputStream toStream(FastByteOutputStream out)
      Parameters:
      out - ByteArrayOutputStream
      Returns:
      字节流
    • toMarkSupportStream

      public static InputStream toMarkSupportStream(InputStream in)
      InputStream转换为支持mark标记的流 若原流支持mark标记,则返回原流,否则使用BufferedInputStream 包装之
      Parameters:
      in - 流
      Returns:
      InputStream
    • toPushbackStream

      public static PushbackInputStream toPushbackStream(InputStream in, int pushBackSize)
      转换为PushbackInputStream 如果传入的输入流已经是PushbackInputStream,强转返回,否则新建一个
      Parameters:
      in - InputStream
      pushBackSize - 推后的byte数
      Returns:
      PushbackInputStream
    • toAvailableStream

      public static InputStream toAvailableStream(InputStream in)
      将指定InputStream 转换为InputStream.available()方法可用的流。 在Socket通信流中,服务端未返回数据情况下InputStream.available()方法始终为0 因此,在读取前需要调用InputStream.read()读取一个字节(未返回会阻塞),一旦读取到了,InputStream.available()方法就正常了。 需要注意的是,在网络流中,是按照块来传输的,所以 InputStream.available() 读取到的并非最终长度,而是此次块的长度。 此方法返回对象的规则为:
      • FileInputStream 返回原对象,因为文件流的available方法本身可用
      • 其它InputStream 返回PushbackInputStream
      Parameters:
      in - 被转换的流
      Returns:
      转换后的流,可能为PushbackInputStream
    • toBuffered

      public static BufferedInputStream toBuffered(InputStream in)
      Parameters:
      in - InputStream
      Returns:
      BufferedInputStream
    • toBuffered

      public static BufferedInputStream toBuffered(InputStream in, int bufferSize)
      Parameters:
      in - InputStream
      bufferSize - buffer size
      Returns:
      BufferedInputStream
    • toBuffered

      public static BufferedOutputStream toBuffered(OutputStream out)
      Parameters:
      out - OutputStream
      Returns:
      BufferedOutputStream
    • toBuffered

      public static BufferedOutputStream toBuffered(OutputStream out, int bufferSize)
      Parameters:
      out - OutputStream
      bufferSize - buffer size
      Returns:
      BufferedOutputStream
    • toBuffered

      public static BufferedReader toBuffered(Reader reader)
      Parameters:
      reader - Reader
      Returns:
      BufferedReader
    • toBuffered

      public static BufferedReader toBuffered(Reader reader, int bufferSize)
      Parameters:
      reader - Reader
      bufferSize - buffer size
      Returns:
      BufferedReader
    • toBuffered

      public static BufferedWriter toBuffered(Writer writer)
      Parameters:
      writer - Writer
      Returns:
      BufferedWriter
    • toBuffered

      public static BufferedWriter toBuffered(Writer writer, int bufferSize)
      Parameters:
      writer - Writer
      bufferSize - buffer size
      Returns:
      BufferedWriter
    • toPushBackReader

      public static PushbackReader toPushBackReader(Reader reader, int pushBackSize)
      获得PushbackReader 如果是PushbackReader强转返回,否则新建
      Parameters:
      reader - 普通Reader
      pushBackSize - 推后的byte数
      Returns:
      PushbackReader
    • write

      public static void write(OutputStream out, boolean isCloseOut, byte[] content) throws InternalException
      将byte[]写到流中
      Parameters:
      out - 输出流
      isCloseOut - 写入完毕是否关闭输出流
      content - 写入的内容
      Throws:
      InternalException - 异常
    • writeUtf8

      public static void writeUtf8(OutputStream out, boolean isCloseOut, Object... contents) throws InternalException
      将多部分内容写到流中,自动转换为UTF-8字符串
      Parameters:
      out - 输出流
      isCloseOut - 写入完毕是否关闭输出流
      contents - 写入的内容,调用toString()方法,不包括不会自动换行
      Throws:
      InternalException - 异常
    • write

      public static void write(OutputStream out, String charsetName, boolean isCloseOut, Object... contents) throws InternalException
      将多部分内容写到流中,自动转换为字符串
      Parameters:
      out - 输出流
      charsetName - 写出的内容的字符集
      isCloseOut - 写入完毕是否关闭输出流
      contents - 写入的内容,调用toString()方法,不包括不会自动换行
      Throws:
      InternalException - 异常
    • write

      public static void write(OutputStream out, Charset charset, boolean isCloseOut, Object... contents) throws InternalException
      将多部分内容写到流中,自动转换为字符串
      Parameters:
      out - 输出流
      charset - 写出的内容的字符集
      isCloseOut - 写入完毕是否关闭输出流
      contents - 写入的内容,调用toString()方法,不包括不会自动换行
      Throws:
      InternalException - 异常
    • write

      public static void write(OutputStream out, boolean isCloseOut, Serializable object) throws InternalException
      将多部分内容写到流中
      Parameters:
      out - 输出流
      isCloseOut - 写入完毕是否关闭输出流
      object - 写入的对象内容
      Throws:
      InternalException
    • writeObjects

      public static void writeObjects(OutputStream out, boolean isCloseOut, Serializable... contents) throws InternalException
      将多部分内容写到流中
      Parameters:
      out - 输出流
      isCloseOut - 写入完毕是否关闭输出流
      contents - 写入的内容
      Throws:
      InternalException - 异常
    • close

      public static void close(Closeable closeable)
      关闭 关闭失败不会抛出异常
      Parameters:
      closeable - 被关闭的对象
    • close

      public static void close(AutoCloseable autoCloseable)
      关闭 关闭失败不会抛出异常
      Parameters:
      autoCloseable - 被关闭的对象
    • close

      public static void close(Socket socket)
      关闭 关闭失败不会抛出异常
      Parameters:
      socket - 被关闭的对象
    • close

      public static void close(ServerSocket serverSocket)
      Parameters:
      serverSocket - 被关闭的对象 关闭serverSocket,忽略任何已检查的异常。 如果serverSocket为空,则不执行任何操作
    • close

      public static void close(AsynchronousSocketChannel channel)
      Parameters:
      channel - 需要被关闭的通道
    • flush

      public static void flush(Flushable flushable)
      从缓存中刷出数据
      Parameters:
      flushable - Flushable
    • contentEquals

      public static boolean contentEquals(InputStream input1, InputStream input2) throws InternalException
      对比两个流内容是否相同 内部会转换流为 BufferedInputStream
      Parameters:
      input1 - 第一个流
      input2 - 第二个流
      Returns:
      两个流的内容一致返回true, 否则false
      Throws:
      InternalException - 异常
    • contentEquals

      public static boolean contentEquals(Reader input1, Reader input2) throws InternalException
      对比两个Reader的内容是否一致 内部会转换流为 BufferedInputStream
      Parameters:
      input1 - 第一个reader
      input2 - 第二个reader
      Returns:
      两个流的内容一致返回true, 否则false
      Throws:
      InternalException - 异常
    • contentEqualsIgnoreEOL

      public static boolean contentEqualsIgnoreEOL(Reader input1, Reader input2) throws InternalException
      对比两个流内容是否相同,忽略EOL字符 内部会转换流为 BufferedInputStream
      Parameters:
      input1 - 第一个流
      input2 - 第二个流
      Returns:
      两个流的内容一致返回true, 否则false
      Throws:
      InternalException - 异常
    • checksumCRC32

      public static long checksumCRC32(InputStream in) throws InternalException
      计算流CRC32校验码,计算后关闭流
      Parameters:
      in - 文件,不能为目录
      Returns:
      CRC32值
      Throws:
      InternalException - 异常
    • checksum

      public static Checksum checksum(InputStream in, Checksum checksum) throws InternalException
      计算流的校验码,计算后关闭流
      Parameters:
      in - 流
      checksum - Checksum
      Returns:
      Checksum
      Throws:
      InternalException - 异常
    • buffer

      public static BufferSource buffer(Source source)
      返回缓冲区从source读取的字节流 返回的源将对其内存缓冲区执行批量读取
      Parameters:
      source - 字节流
      Returns:
      返回缓冲区
    • buffer

      public static BufferSink buffer(Sink sink)
      返回一个新接收器,该接收器缓冲写sink 返回的接收器将批量写入sink
      Parameters:
      sink - 接收一个字节流
      Returns:
      接收缓冲区
    • sink

      public static Sink sink(OutputStream out)
      返回一个向out写入的接收器
      Parameters:
      out - 输出流
      Returns:
      接收缓冲区
    • sink

      public static Sink sink(Socket socket) throws IOException
      返回一个向socket写入的接收器。优先选择这个方法, 而不是sink(OutputStream),因为这个方法支持超时 当套接字写超时时,套接字将由任务线程异步关闭
      Parameters:
      socket - 套接字
      Returns:
      接收器
      Throws:
      IOException - IO异常
    • source

      public static Source source(InputStream in)
      返回从in中读取的缓冲数据
      Parameters:
      in - 数据输入流
      Returns:
      缓冲数据
    • source

      public static Source source(File file) throws FileNotFoundException
      返回从file读取的缓冲数据
      Parameters:
      file - 文件
      Returns:
      缓冲数据
      Throws:
      FileNotFoundException - 文件未找到
    • source

      public static Source source(Path path, OpenOption... options) throws IOException
      返回从path读取的缓冲数据
      Parameters:
      path - 路径
      options - 选项
      Returns:
      缓冲数据
      Throws:
      IOException - IO异常
    • sink

      public static Sink sink(File file) throws FileNotFoundException
      返回一个向file写入的接收器
      Parameters:
      file - 文件
      Returns:
      接收器
      Throws:
      FileNotFoundException - 文件未找到
    • appendingSink

      public static Sink appendingSink(File file) throws FileNotFoundException
      返回一个附加到file的接收器
      Parameters:
      file - 文件
      Returns:
      接收器
      Throws:
      FileNotFoundException - 文件未找到
    • sink

      public static Sink sink(Path path, OpenOption... options) throws IOException
      返回一个向path写入的接收器.
      Parameters:
      path - 路径
      options - 属性
      Returns:
      写入的数据的接收器
      Throws:
      IOException - IO异常
    • blackhole

      public static Sink blackhole()
      返回一个都不写的接收器
      Returns:
      接收器
    • source

      public static Source source(Socket socket) throws IOException
      返回从socket读取的缓存信息。与source(InputStream)相比, 更喜欢这个方法, 因为这个方法支持超时。当套接字读取超时时,套接字将由任务线程异步关闭
      Parameters:
      socket - 套接字
      Returns:
      取的缓存信息
      Throws:
      IOException - IO异常
    • toInputStream

      public static InputStream toInputStream(String input)
      将指定的字符串转换为输入流,使用平台的默认字符编码编码为字节
      Parameters:
      input - 要转换的字符串
      Returns:
      一个输入流
    • toInputStream

      public static InputStream toInputStream(String input, String encoding) throws IOException
      将指定的字符串转换为输入流,使用指定的字符编码编码为字节
      Parameters:
      input - 要转换的字符串
      encoding - 要使用的编码,null表示平台默认值
      Returns:
      一个输入流
      Throws:
      IOException - 如果编码无效
    • toString

      public static String toString(InputStream input, String encoding) throws IOException
      使用指定的字符编码将InputStream的内容作为字符串获取 这个方法在内部缓冲输入,所以不需要使用BufferedInputStream
      Parameters:
      input - 要从中读取的InputStream
      encoding - 要使用的编码,null表示平台默认值
      Returns:
      所请求的字符串
      Throws:
      NullPointerException - 如果输入为空
      IOException - 如果发生I/O错误
    • copy

      public static void copy(InputStream input, Writer output)
      使用平台的默认字符编码将字节从InputStream复制到写入器上的字符 该方法在内部缓冲输入,因此不需要使用 BufferedInputStream 方法使用 InputStreamReader
      Parameters:
      input - 要从中读取的InputStream
      output - 要写入的输出者
    • copy

      public static void copy(InputStream input, Writer output, String encoding) throws IOException
      使用指定的字符编码将字节从InputStream复制到写入器上的字符. 该方法在内部缓冲输入,因此不需要使用 BufferedInputStream 方法使用 InputStreamReader
      Parameters:
      input - 要从中读取的InputStream
      output - 要写入的输出者
      encoding - 要使用的编码,null表示平台默认值
      Throws:
      NullPointerException - 如果输入为空
      IOException - 如果发生I/O错误
    • toString

      public static String toString(Reader input)
      以字符串的形式获取阅读器的内容
      Parameters:
      input - 读取信息
      Returns:
      所请求的字符串
    • toString

      public static String toString(byte[] input)
      使用平台的默认字符编码将byte[]的内容作为字符串获取
      Parameters:
      input - 要从中读取的字节数组
      Returns:
      所请求的字符串
    • toString

      public static String toString(byte[] input, String encoding) throws IOException
      使用指定的字符编码将字节[]的内容作为字符串获取
      Parameters:
      input - 要从中读取的字节数组
      encoding - 要使用的编码,null表示平台默认值
      Returns:
      所请求的字符串
      Throws:
      NullPointerException - 如果输入为空
      IOException - 如果发生I/O错误(从未发生)