public class IoUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_BUFFER_SIZE
默认缓存大小
|
static int |
DEFAULT_LARGE_BUFFER_SIZE
默认大缓存大小
|
static int |
DEFAULT_MIDDLE_BUFFER_SIZE
默认中等缓存大小
|
static int |
EOF
数据流末尾
|
| Modifier and Type | Method and Description |
|---|---|
static Sink |
appendingSink(File file) |
static boolean |
arrayRangeEquals(byte[] a,
int aOffset,
byte[] b,
int bOffset,
int byteCount) |
static Sink |
blackhole() |
static BufferedSink |
buffer(Sink sink) |
static BufferedSource |
buffer(Source source) |
static void |
checkOffsetAndCount(long size,
long offset,
long byteCount) |
static Checksum |
checksum(InputStream in,
Checksum checksum)
计算流的校验码,计算后关闭流
|
static long |
checksumCRC32(InputStream in)
计算流CRC32校验码,计算后关闭流
|
static void |
close(AutoCloseable closeable)
关闭
关闭失败不会抛出异常
|
static void |
close(Closeable closeable)
关闭
关闭失败不会抛出异常
|
static void |
close(Socket sock)
关闭
关闭失败不会抛出异常
|
static boolean |
contentEquals(InputStream input1,
InputStream input2)
对比两个流内容是否相同
内部会转换流为
BufferedInputStream |
static boolean |
contentEquals(Reader input1,
Reader input2)
对比两个Reader的内容是否一致
内部会转换流为
BufferedInputStream |
static boolean |
contentEqualsIgnoreEOL(Reader input1,
Reader input2)
对比两个流内容是否相同,忽略EOL字符
内部会转换流为
BufferedInputStream |
static long |
copy(FileInputStream in,
FileOutputStream out)
拷贝文件流,使用NIO
|
static long |
copy(InputStream in,
OutputStream out)
拷贝流,使用默认Buffer大小
|
static long |
copy(InputStream in,
OutputStream out,
int bufferSize)
拷贝流
|
static long |
copy(InputStream in,
OutputStream out,
int bufferSize,
StreamProgress streamProgress)
拷贝流
|
static void |
copy(InputStream input,
Writer output)
Copy bytes from an InputStream to chars on a
Writer using the default character encoding of the platform.
|
static void |
copy(InputStream input,
Writer output,
String encoding)
Copy bytes from an InputStream to chars on a
Writer using the specified character encoding.
|
static long |
copy(ReadableByteChannel in,
WritableByteChannel out,
int bufferSize,
StreamProgress streamProgress)
拷贝流,使用NIO,不会关闭流
|
static long |
copy(Reader reader,
Writer writer)
将Reader中的内容复制到Writer中 使用默认缓存大小
|
static long |
copy(Reader reader,
Writer writer,
int bufferSize)
将Reader中的内容复制到Writer中
|
static long |
copy(Reader reader,
Writer writer,
int bufferSize,
StreamProgress streamProgress)
将Reader中的内容复制到Writer中
|
static long |
copyByNIO(InputStream in,
OutputStream out,
int bufferSize,
StreamProgress streamProgress)
拷贝流 thanks to: https://github.com/venusdrogon/feilong-io/blob/master/src/main/java/com/feilong/io/IOWriteUtil.java
本方法不会关闭流
|
static PushbackReader |
getPushBackReader(Reader reader,
int pushBackSize)
获得
PushbackReader
如果是PushbackReader强转返回,否则新建 |
static BufferedReader |
getReader(InputStream in,
Charset charset)
获得一个Reader
|
static BufferedReader |
getReader(InputStream in,
String charsetName)
获得一个文件读取器
|
static BufferedReader |
getReader(Reader reader)
获得
BufferedReader
如果是BufferedReader强转返回,否则新建。如果提供的Reader为null返回null |
static OutputStreamWriter |
getWriter(OutputStream out,
Charset charset)
获得一个Writer
|
static OutputStreamWriter |
getWriter(OutputStream out,
String charsetName)
获得一个Writer
|
static String |
read(FileChannel fileChannel,
Charset charset)
从FileChannel中读取内容
|
static String |
read(FileChannel fileChannel,
String charsetName)
从FileChannel中读取内容,读取完毕后并不关闭Channel
|
static FastByteArrayOutputStream |
read(InputStream in)
从流中读取内容,读到输出流中
|
static String |
read(InputStream in,
Charset charset)
从流中读取内容,读取完毕后并不关闭流
|
static String |
read(InputStream in,
String charsetName)
从流中读取内容
|
static String |
read(Reader reader)
从Reader中读取String,读取完毕后并不关闭Reader
|
static byte[] |
readBytes(InputStream in)
从流中读取bytes
|
static byte[] |
readBytes(InputStream in,
int length)
读取指定长度的byte数组,不关闭流
|
static String |
readHex(InputStream in,
int length,
boolean toLowerCase)
读取16进制字符串
|
static String |
readHex28Lower(InputStream in)
从流中读取前28个byte并转换为16进制,字母部分使用小写
|
static String |
readHex28Upper(InputStream in)
从流中读取前28个byte并转换为16进制,字母部分使用大写
|
static void |
readLines(InputStream in,
Charset charset,
LineHandler lineHandler)
按行读取数据,针对每行的数据做处理
|
static <T extends Collection<String>> |
readLines(InputStream in,
Charset charset,
T collection)
从流中读取内容
|
static <T extends Collection<String>> |
readLines(InputStream in,
String charsetName,
T collection)
从流中读取内容
|
static void |
readLines(Reader reader,
LineHandler lineHandler)
按行读取数据,针对每行的数据做处理
Reader自带编码定义,因此读取数据的编码跟随其编码。 |
static <T extends Collection<String>> |
readLines(Reader reader,
T collection)
从Reader中读取内容
|
static <T> T |
readObj(InputStream in)
从流中读取内容,读到输出流中
|
static String |
readUtf8(FileChannel fileChannel)
从FileChannel中读取UTF-8编码内容
|
static void |
readUtf8Lines(InputStream in,
LineHandler lineHandler)
按行读取UTF-8编码数据,针对每行的数据做处理
|
static <T extends Collection<String>> |
readUtf8Lines(InputStream in,
T collection)
从流中读取内容,使用UTF-8编码
|
static int |
reverseBytesInt(int i) |
static long |
reverseBytesLong(long v) |
static short |
reverseBytesShort(short s) |
static Sink |
sink(File file) |
static Sink |
sink(OutputStream out) |
static Sink |
sink(Path path,
OpenOption... options) |
static Sink |
sink(Socket socket) |
static void |
sneakyRethrow(Throwable t)
即使被声明也不允许直接抛出
这是一种很糟糕的做饭,很容易遭到攻击
清理后捕获并重新抛出异常。参见Java Puzzlers #43。
|
static Source |
source(File file) |
static Source |
source(InputStream in) |
static Source |
source(Path path,
OpenOption... options) |
static Source |
source(Socket socket) |
static BufferedInputStream |
toBuffered(InputStream in)
|
static BufferedOutputStream |
toBuffered(OutputStream out)
|
static InputStream |
toInputStream(String input)
Convert the specified string to an input stream, encoded as bytes
using the default character encoding of the platform.
|
static InputStream |
toInputStream(String input,
String encoding)
Convert the specified string to an input stream, encoded as bytes
using the specified character encoding.
|
static InputStream |
toMarkSupportStream(InputStream in)
将
InputStream转换为支持mark标记的流
若原流支持mark标记,则返回原流,否则使用BufferedInputStream 包装之 |
static PushbackInputStream |
toPushbackStream(InputStream in,
int pushBackSize)
转换为
PushbackInputStream
如果传入的输入流已经是PushbackInputStream,强转返回,否则新建一个 |
static ByteArrayInputStream |
toStream(byte[] content)
String 转为流
|
static FileInputStream |
toStream(File file)
文件转为流
|
static ByteArrayInputStream |
toStream(String content,
Charset charset)
String 转为流
|
static ByteArrayInputStream |
toStream(String content,
String charsetName)
String 转为流
|
static String |
toString(byte[] input)
Get the contents of a byte[] as a String
using the default character encoding of the platform.
|
static String |
toString(byte[] input,
String encoding)
Get the contents of a byte[] as a String
using the specified character encoding.
|
static String |
toString(InputStream input,
String encoding)
Get the contents of an InputStream as a String
using the specified character encoding.
|
static String |
toString(Reader input)
Get the contents of a Reader as a String.
|
static void |
write(OutputStream out,
boolean isCloseOut,
byte[] content)
将byte[]写到流中
|
static void |
write(OutputStream out,
Charset charset,
boolean isCloseOut,
Object... contents)
将多部分内容写到流中,自动转换为字符串
|
static void |
write(OutputStream out,
String charsetName,
boolean isCloseOut,
Object... contents)
将多部分内容写到流中,自动转换为字符串
|
static void |
writeObjects(OutputStream out,
boolean isCloseOut,
Serializable... contents)
将多部分内容写到流中
|
static void |
writeUtf8(OutputStream out,
boolean isCloseOut,
Object... contents)
将多部分内容写到流中,自动转换为UTF-8字符串
|
public static final int DEFAULT_BUFFER_SIZE
public static final int DEFAULT_MIDDLE_BUFFER_SIZE
public static final int DEFAULT_LARGE_BUFFER_SIZE
public static final int EOF
public static void checkOffsetAndCount(long size,
long offset,
long byteCount)
public static short reverseBytesShort(short s)
public static int reverseBytesInt(int i)
public static long reverseBytesLong(long v)
public static void sneakyRethrow(Throwable t)
t - 异常public static boolean arrayRangeEquals(byte[] a,
int aOffset,
byte[] b,
int bOffset,
int byteCount)
public static long copy(Reader reader, Writer writer) throws InstrumentException
reader - Readerwriter - WriterInstrumentException - 异常public static long copy(Reader reader, Writer writer, int bufferSize) throws InstrumentException
reader - Readerwriter - WriterbufferSize - 缓存大小InstrumentException - 异常public static long copy(Reader reader, Writer writer, int bufferSize, StreamProgress streamProgress) throws InstrumentException
reader - Readerwriter - WriterbufferSize - 缓存大小streamProgress - 进度处理器InstrumentException - 异常public static long copy(InputStream in, OutputStream out) throws InstrumentException
in - 输入流out - 输出流InstrumentException - 异常public static long copy(InputStream in, OutputStream out, int bufferSize) throws InstrumentException
in - 输入流out - 输出流bufferSize - 缓存大小InstrumentException - 异常public static long copy(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws InstrumentException
in - 输入流out - 输出流bufferSize - 缓存大小streamProgress - 进度条InstrumentException - 异常public static long copyByNIO(InputStream in, OutputStream out, int bufferSize, StreamProgress streamProgress) throws InstrumentException
in - 输入流out - 输出流bufferSize - 缓存大小streamProgress - 进度条InstrumentException - 异常public static long copy(FileInputStream in, FileOutputStream out) throws InstrumentException
in - 输入out - 输出InstrumentException - 异常public static long copy(ReadableByteChannel in, WritableByteChannel out, int bufferSize, StreamProgress streamProgress) throws InstrumentException
in - ReadableByteChannelout - WritableByteChannelbufferSize - 缓冲大小,如果小于等于0,使用默认streamProgress - StreamProgress进度处理器InstrumentException - 异常public static BufferedReader getReader(InputStream in, String charsetName)
in - 输入流charsetName - 字符集名称public static BufferedReader getReader(InputStream in, Charset charset)
in - 输入流charset - 字符集public static BufferedReader getReader(Reader reader)
BufferedReader
如果是BufferedReader强转返回,否则新建。如果提供的Reader为null返回nullreader - 普通Reader,如果为null返回nullBufferedReader or nullpublic static PushbackReader getPushBackReader(Reader reader, int pushBackSize)
PushbackReader
如果是PushbackReader强转返回,否则新建reader - 普通ReaderpushBackSize - 推后的byte数PushbackReaderpublic static OutputStreamWriter getWriter(OutputStream out, String charsetName)
out - 输入流charsetName - 字符集public static OutputStreamWriter getWriter(OutputStream out, Charset charset)
out - 输入流charset - 字符集public static String read(InputStream in, String charsetName) throws InstrumentException
in - 输入流charsetName - 字符集InstrumentException - 异常public static String read(InputStream in, Charset charset) throws InstrumentException
in - 输入流,读取完毕后并不关闭流charset - 字符集InstrumentException - 异常public static FastByteArrayOutputStream read(InputStream in) throws InstrumentException
in - 输入流InstrumentException - 异常public static String read(Reader reader) throws InstrumentException
reader - ReaderInstrumentException - 异常public static String readUtf8(FileChannel fileChannel) throws InstrumentException
fileChannel - 文件管道InstrumentException - 异常public static String read(FileChannel fileChannel, String charsetName) throws InstrumentException
fileChannel - 文件管道charsetName - 字符集InstrumentException - 异常public static String read(FileChannel fileChannel, Charset charset) throws InstrumentException
fileChannel - 文件管道charset - 字符集InstrumentException - 异常public static byte[] readBytes(InputStream in) throws InstrumentException
in - InputStreamInstrumentException - 异常public static byte[] readBytes(InputStream in, int length) throws InstrumentException
in - InputStream,为null返回nulllength - 长度,小于等于0返回空byte数组InstrumentException - 异常public static String readHex(InputStream in, int length, boolean toLowerCase) throws InstrumentException
in - InputStreamlength - 长度toLowerCase - true 传换成小写格式 , false 传换成大写格式InstrumentException - 异常public static String readHex28Upper(InputStream in) throws InstrumentException
in - InputStreamInstrumentException - 异常public static String readHex28Lower(InputStream in) throws InstrumentException
in - InputStreamInstrumentException - 异常public static <T> T readObj(InputStream in) throws InstrumentException
T - 读取对象的类型in - 输入流InstrumentException - 异常public static <T extends Collection<String>> T readUtf8Lines(InputStream in, T collection) throws InstrumentException
T - 集合类型in - 输入流collection - 返回集合InstrumentException - 异常public static <T extends Collection<String>> T readLines(InputStream in, String charsetName, T collection) throws InstrumentException
T - 集合类型in - 输入流charsetName - 字符集collection - 返回集合InstrumentException - 异常public static <T extends Collection<String>> T readLines(InputStream in, Charset charset, T collection) throws InstrumentException
T - 集合类型in - 输入流charset - 字符集collection - 返回集合InstrumentException - 异常public static <T extends Collection<String>> T readLines(Reader reader, T collection) throws InstrumentException
T - 集合类型reader - Readercollection - 返回集合InstrumentException - 异常public static void readUtf8Lines(InputStream in, LineHandler lineHandler) throws InstrumentException
in - InputStreamlineHandler - 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方InstrumentException - 异常public static void readLines(InputStream in, Charset charset, LineHandler lineHandler) throws InstrumentException
in - InputStreamcharset - Charset编码lineHandler - 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方InstrumentException - 异常public static void readLines(Reader reader, LineHandler lineHandler) throws InstrumentException
Reader自带编码定义,因此读取数据的编码跟随其编码。reader - ReaderlineHandler - 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方InstrumentException - 异常public static ByteArrayInputStream toStream(String content, String charsetName)
content - 内容charsetName - 编码public static ByteArrayInputStream toStream(String content, Charset charset)
content - 内容charset - 编码public static ByteArrayInputStream toStream(byte[] content)
content - 内容bytespublic static FileInputStream toStream(File file)
file - 文件FileInputStreampublic static BufferedInputStream toBuffered(InputStream in)
in - InputStreamBufferedInputStreampublic static BufferedOutputStream toBuffered(OutputStream out)
out - OutputStreamBufferedOutputStreampublic static InputStream toMarkSupportStream(InputStream in)
InputStream转换为支持mark标记的流
若原流支持mark标记,则返回原流,否则使用BufferedInputStream 包装之in - 流InputStreampublic static PushbackInputStream toPushbackStream(InputStream in, int pushBackSize)
PushbackInputStream
如果传入的输入流已经是PushbackInputStream,强转返回,否则新建一个in - InputStreampushBackSize - 推后的byte数PushbackInputStreampublic static void write(OutputStream out, boolean isCloseOut, byte[] content) throws InstrumentException
out - 输出流isCloseOut - 写入完毕是否关闭输出流content - 写入的内容InstrumentException - 异常public static void writeUtf8(OutputStream out, boolean isCloseOut, Object... contents) throws InstrumentException
out - 输出流isCloseOut - 写入完毕是否关闭输出流contents - 写入的内容,调用toString()方法,不包括不会自动换行InstrumentException - 异常public static void write(OutputStream out, String charsetName, boolean isCloseOut, Object... contents) throws InstrumentException
out - 输出流charsetName - 写出的内容的字符集isCloseOut - 写入完毕是否关闭输出流contents - 写入的内容,调用toString()方法,不包括不会自动换行InstrumentException - 异常public static void write(OutputStream out, Charset charset, boolean isCloseOut, Object... contents) throws InstrumentException
out - 输出流charset - 写出的内容的字符集isCloseOut - 写入完毕是否关闭输出流contents - 写入的内容,调用toString()方法,不包括不会自动换行InstrumentException - 异常public static void writeObjects(OutputStream out, boolean isCloseOut, Serializable... contents) throws InstrumentException
out - 输出流isCloseOut - 写入完毕是否关闭输出流contents - 写入的内容InstrumentException - 异常public static void close(Closeable closeable)
closeable - 被关闭的对象public static void close(AutoCloseable closeable)
closeable - 被关闭的对象public static void close(Socket sock)
sock - 被关闭的对象public static boolean contentEquals(InputStream input1, InputStream input2) throws InstrumentException
BufferedInputStreaminput1 - 第一个流input2 - 第二个流InstrumentException - 异常public static boolean contentEquals(Reader input1, Reader input2) throws InstrumentException
BufferedInputStreaminput1 - 第一个readerinput2 - 第二个readerInstrumentException - 异常public static boolean contentEqualsIgnoreEOL(Reader input1, Reader input2) throws InstrumentException
BufferedInputStreaminput1 - 第一个流input2 - 第二个流InstrumentException - 异常public static long checksumCRC32(InputStream in) throws InstrumentException
in - 文件,不能为目录InstrumentException - 异常public static Checksum checksum(InputStream in, Checksum checksum) throws InstrumentException
in - 流checksum - ChecksumInstrumentException - 异常public static BufferedSource buffer(Source source)
public static BufferedSink buffer(Sink sink)
public static Sink sink(OutputStream out)
public static Sink sink(Socket socket) throws IOException
IOExceptionpublic static Source source(InputStream in)
public static Source source(File file) throws FileNotFoundException
FileNotFoundExceptionpublic static Source source(Path path, OpenOption... options) throws IOException
IOExceptionpublic static Sink sink(File file) throws FileNotFoundException
FileNotFoundExceptionpublic static Sink appendingSink(File file) throws FileNotFoundException
FileNotFoundExceptionpublic static Sink sink(Path path, OpenOption... options) throws IOException
IOExceptionpublic static Sink blackhole()
public static Source source(Socket socket) throws IOException
IOExceptionpublic static InputStream toInputStream(String input)
input - the string to convertpublic static InputStream toInputStream(String input, String encoding) throws IOException
input - the string to convertencoding - the encoding to use, null means platform defaultIOException - if the encoding is invalidpublic static String toString(InputStream input, String encoding) throws IOException
Character encoding names can be found at IANA.
This method buffers the input internally, so there is no need to use a BufferedInputStream.
input - the InputStream to read fromencoding - the encoding to use, null means platform defaultNullPointerException - if the input is nullIOException - if an I/O error occurspublic static void copy(InputStream input, Writer output)
This method buffers the input internally, so there is no need to use a BufferedInputStream.
This method uses InputStreamReader.
input - the InputStream to read fromoutput - the Writer to write topublic static void copy(InputStream input, Writer output, String encoding) throws IOException
This method buffers the input internally, so there is no need to use a BufferedInputStream.
Character encoding names can be found at IANA.
This method uses InputStreamReader.
input - the InputStream to read fromoutput - the Writer to write toencoding - the encoding to use, null means platform defaultNullPointerException - if the input or output is nullIOException - if an I/O error occurspublic static String toString(Reader input)
This method buffers the input internally, so there is no need to use a BufferedReader.
input - the Reader to read frompublic static String toString(byte[] input)
input - the byte array to read frompublic static String toString(byte[] input, String encoding) throws IOException
Character encoding names can be found at IANA.
input - the byte array to read fromencoding - the encoding to use, null means platform defaultNullPointerException - if the input is nullIOException - if an I/O error occurs (never occurs)Copyright © 2019. All rights reserved.