003-Spring 中的StreamUtils
一、概述
StreamUtils是spring中用于处理流的类,是java.io包中inputStream和outputStream,不是java8中Steam。使用时仅依赖spring-core
二、使用
基本的输入流读取成字符串
public static String getStringFromInputStream(InputStream input) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(input, writer, "UTF-8"); return writer.toString(); }
2.1、拷贝inputStream内容至outputStream:StreamUtils.copy(in, out);
两个参数,第一个为输入,第二个为拷贝至的
@Test public void whenCopyInputStreamToOutputStream_thenCorrect() throws IOException { String inputFileName = "src/test/resources/input.txt"; String outputFileName = "src/test/resources/output.txt"; File outputFile = new File(outputFileName); InputStream in = new FileInputStream(inputFileName); OutputStream out = new FileOutputStream(outputFile); StreamUtils.copy(in, out); assertTrue(outputFile.exists()); String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); assertEquals(inputFileContent, outputFileContent); }
2.2、拷贝inputStream部分内容至outputStream,使用copyRange()方法拷贝一定范围的内容:
copyRange方法有四个参数,inputStream,outputStream,开始拷贝位置,结束拷贝位置。如果我们指定的长度超过inputStream的长度呢?copyRange方法仅拷贝至流的结尾。
@Test public void whenCopyRangeOfInputStreamToOutputStream_thenCorrect() throws IOException { String inputFileName = "src/test/resources/input.txt"; String outputFileName = "src/test/resources/output.txt"; File outputFile = new File(outputFileName); InputStream in = new FileInputStream(inputFileName); OutputStream out = new FileOutputStream(outputFileName); StreamUtils.copyRange(in, out, 1, 10); assertTrue(outputFile.exists()); String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); assertEquals(inputFileContent.substring(1, 11), outputFileContent); }
2.3、拷贝字符串至outputStream
copy方法带三个参数:被拷贝的字符串,写文件时指定的字符集,指定目的地(outputStream)
@Test public void whenCopyStringToOutputStream_thenCorrect() throws IOException { String string = "Should be copied to OutputStream."; String outputFileName = "src/test/resources/output2.txt"; File outputFile = new File(outputFileName); OutputStream out = new FileOutputStream(outputFileName); StreamUtils.copy(string, StandardCharsets.UTF_8, out); assertTrue(outputFile.exists()); String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); assertEquals(outputFileContent, string); }
2.4、将inputStream内容拷贝为字符串
@Test public void whenCopyInputStreamToString_thenCorrect() throws IOException { String inputFileName = "src/test/resources/input.txt"; InputStream is = new FileInputStream(inputFileName); String content = StreamUtils.copyToString(is, StandardCharsets.UTF_8); String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); System.out.println(content); assertEquals(inputFileContent, content); }
2.5、拷贝inputStream内容至字节数组
@Test public void whenCopyInputStreamToByteArray_thenCorrect() throws IOException { String inputFileName = "src/test/resources/input.txt"; InputStream is = new FileInputStream(inputFileName); byte[] out = StreamUtils.copyToByteArray(is); String content = new String(out); String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); assertEquals(inputFileContent, content); }
2.6、删除流中所有剩余数据
StreamUtils.drain(in);
2.7、获得一个有效空输入流
public InputStream getInputStream() { return StreamUtils.emptyInput(); }
2.8、两个重载方法nonClosing(),inputStream和outputStream流可以作为参数,用于返回无需关闭的inputStream和outputStream流
public InputStream getNonClosingInputStream() throws IOException { InputStream in = new FileInputStream("src/test/resources/input.txt"); return StreamUtils.nonClosing(in); }