一、概述

  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);
}

003-Spring 中的StreamUtils的更多相关文章

  1. Spring 中StreamUtils教程

    本文我们介绍StreamUtils类使用.StreamUtils是spring中用于处理流的类,是java.io包中inputStream和outputStream,不是java8中Steam.使用时 ...

  2. Velocity初探小结--Velocity在spring中的配置和使用

    最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...

  3. Spring中Bean的作用域、生命周期

                                   Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...

  4. Spring中Bean的实例化

                                    Spring中Bean的实例化 在介绍Bean的三种实例化的方式之前,我们首先需要介绍一下什么是Bean,以及Bean的配置方式. 如果 ...

  5. 模拟实现Spring中的注解装配

    本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...

  6. Spring中常见的bean创建异常

    Spring中常见的bean创建异常 1. 概述     本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...

  7. Spring中配置数据源的4种形式

    不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ...

  8. spring中InitializingBean接口使用理解

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...

  9. Quartz 在 Spring 中如何动态配置时间--转

    原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...

随机推荐

  1. W25Q128页数和扇区数

    int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size){ *block_size = 4 ...

  2. Atitit.uml2 api 的编程代码实现设计uml开发 使用eclipse jar java 版本

    Atitit.uml2 api 的编程代码实现设计uml开发 使用eclipse jar java 版本 1. clipse提供了UML的底层Java包, 1 2. MDTUML2Getting St ...

  3. python c example2:pylame2

    #include <Python.h> #include <lame.h> //pyton object variables typedef struct{ PyObject_ ...

  4. 参数化--每个vuser使用不同的参数值

    使用controller并发时,每个vuser从参数文件中取一个值,设置如下图:

  5. MVC-Model

    用模型取代字典理由: **使用字典的坏处 一般情况下,存入数据和取出数据都使用“字典类型的key”,编写这些key时,编译时不会有任何的友善提示,需要手敲,容易出错. dict[@“name”] = ...

  6. SHA信息摘要

    SHA算法是在MD4的基础上演进而来的,通过SHA算法能够获得一个固定长度的摘要信息.   SHA算法系列有SHA-1(也成为SHA),SHA-224,SHA-256,SHA-384和SHA-512这 ...

  7. 【Python + ATX基于uiaotumator2】之Android—APP自动化简易例子

    上代码: import uiautomator2 as u2 from time import sleep d = u2.connect_usb('608ad0fe') #打开小卖 # d(text= ...

  8. ini文件

    *.INI内容 [NETWORK] ServerIP=100.100.100.53 程序: main() { char ip[16]; DWORD num=0; num=GetPrivateProfi ...

  9. 微信 oauth 授权3

    3. 请求 2的url会得到 { "access_token": "OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2Cot ...

  10. html中div获取焦点,去掉input div等获取焦点时候的边框

    经测试只有在IE chrome才会在获取焦点时有边框 使用CSS div{ outline:none; } DIV焦点事件详解 --[focus和tabIndex]​ 摘自:http://my.osc ...