mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
扩展IoUtil、BooleanUtil两个工具类
This commit is contained in:
parent
a38e3e52af
commit
eefdd7fc4f
@ -186,6 +186,45 @@ public class IoUtil extends NioUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 复制InputStream中的内容到byte[]中,并返回复制后的byte[]
|
||||||
|
* @param in InputStream
|
||||||
|
* @return 返回从InputStream中复制出来的byte[]内容
|
||||||
|
* @throws IOException
|
||||||
|
* @author ZRH 455741807@qq.com
|
||||||
|
*/
|
||||||
|
public static byte[] copyToByteArray(InputStream in) throws IOException {
|
||||||
|
if (in == null) {
|
||||||
|
return new byte[0];
|
||||||
|
}
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
|
||||||
|
copy(in, out);
|
||||||
|
return out.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 复制InputStream中的内容为字符串,并返回字符串内容
|
||||||
|
* @param in InputStream
|
||||||
|
* @param charset 编码
|
||||||
|
* @return 返回从InputStream中复制出来的字符串内容,如果InputStream为null,返回“”
|
||||||
|
* @throws IOException
|
||||||
|
* @author ZRH 455741807@qq.com
|
||||||
|
*/
|
||||||
|
public static String copyToString(InputStream in, Charset charset) throws IOException {
|
||||||
|
if (in == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder out = new StringBuilder(DEFAULT_BUFFER_SIZE);
|
||||||
|
InputStreamReader reader = new InputStreamReader(in, charset);
|
||||||
|
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
|
||||||
|
int charsRead;
|
||||||
|
while ((charsRead = reader.read(buffer)) != -1) {
|
||||||
|
out.append(buffer, 0, charsRead);
|
||||||
|
}
|
||||||
|
return out.toString();
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------------------- Copy end
|
// -------------------------------------------------------------------------------------- Copy end
|
||||||
|
|
||||||
// -------------------------------------------------------------------------------------- getReader and getWriter start
|
// -------------------------------------------------------------------------------------- getReader and getWriter start
|
||||||
|
@ -18,6 +18,23 @@ public class BooleanUtil {
|
|||||||
/** 表示为假的字符串 */
|
/** 表示为假的字符串 */
|
||||||
private static final Set<String> FALSE_SET = CollUtil.newHashSet("false", "no", "n", "f", "0", "off", "否", "错", "假", "錯", "×");
|
private static final Set<String> FALSE_SET = CollUtil.newHashSet("false", "no", "n", "f", "0", "off", "否", "错", "假", "錯", "×");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证前端传过来的字符串,是不是正确的bool值
|
||||||
|
* @param boolStr
|
||||||
|
* @return boolStr是TRUE_SET、FALSE_SET中的字符串时,才返回true;其余内容返回false
|
||||||
|
* @author ZRH 455741807@qq.com
|
||||||
|
*/
|
||||||
|
public static boolean verifyBooleanString(String boolStr){
|
||||||
|
if(StrUtil.isBlank(boolStr)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
boolStr = boolStr.toLowerCase();
|
||||||
|
if(TRUE_SET.contains(boolStr) || FALSE_SET.contains(boolStr)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取相反值
|
* 取相反值
|
||||||
*
|
*
|
||||||
|
@ -5,8 +5,8 @@ import cn.hutool.core.util.RandomUtil;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
public class IoUtilTest {
|
public class IoUtilTest {
|
||||||
|
|
||||||
@ -32,4 +32,28 @@ public class IoUtilTest {
|
|||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void copyToByteArrayTest() throws Exception {
|
||||||
|
try(InputStream is1 = ResourceUtil.getStream("hutool.jpg");
|
||||||
|
InputStream is2 = ResourceUtil.getStream("hutool.jpg")){
|
||||||
|
byte[] copiedBytes = IoUtil.copyToByteArray(is1);
|
||||||
|
byte[] readBytes = IoUtil.readBytes(is2);
|
||||||
|
Assert.assertArrayEquals(readBytes, copiedBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void copyToStringTest() throws Exception {
|
||||||
|
String str = "abc123";
|
||||||
|
try(InputStream is1 = new ByteArrayInputStream(str.getBytes(Charset.defaultCharset()));
|
||||||
|
InputStream is2 = new ByteArrayInputStream(str.getBytes(Charset.defaultCharset()))){
|
||||||
|
String copiedString = IoUtil.copyToString(is1, Charset.defaultCharset());
|
||||||
|
String readString = IoUtil.read(is2, Charset.defaultCharset());
|
||||||
|
Assert.assertEquals(readString, copiedString);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,40 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class BooleanUtilTest {
|
public class BooleanUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testVerifyBooleanString(){
|
||||||
|
/*true的各种形式*/
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("true"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("yes"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("t"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("OK"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("1"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("On"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("是"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("对"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("真"));
|
||||||
|
|
||||||
|
/*false的各种形式*/
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("false"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("no"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("n"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("f"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("0"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("off"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("否"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("错"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("假"));
|
||||||
|
Assert.assertTrue(BooleanUtil.verifyBooleanString("錯"));
|
||||||
|
|
||||||
|
/*非正常的bool字符串*/
|
||||||
|
Assert.assertFalse(BooleanUtil.verifyBooleanString(null));
|
||||||
|
Assert.assertFalse(BooleanUtil.verifyBooleanString(""));
|
||||||
|
Assert.assertFalse(BooleanUtil.verifyBooleanString("x"));
|
||||||
|
Assert.assertFalse(BooleanUtil.verifyBooleanString("a"));
|
||||||
|
Assert.assertFalse(BooleanUtil.verifyBooleanString("99"));
|
||||||
|
Assert.assertFalse(BooleanUtil.verifyBooleanString("q23"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toBooleanTest() {
|
public void toBooleanTest() {
|
||||||
Assert.assertTrue(BooleanUtil.toBoolean("true"));
|
Assert.assertTrue(BooleanUtil.toBoolean("true"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user