0%

重温经典,特意看了下为什么读文件非得不让等于 -1

fileChannelIn.read(byteBuffer) != -1

EOF = End Of File,其默认值就是-1

这是一个约定好的结束符,不是认为改变的

代码

建议使用大文件进行测试,看效果比较明显

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@Test
public void testCopy() {
String oldFileName = "/Users/hisenyuan/hisen/blog/source/_posts/Test-Java-Code.md";
String newFileName = "/Users/hisenyuan/hisen/test/Test-Java-Code.md";
nioCopy(oldFileName, newFileName);
ioCopy(oldFileName, newFileName.replace(".md", ".md.bak"));
ioCopyByLine(oldFileName,newFileName.replace(".md",".bak." + System.currentTimeMillis()));
}

/**
* 利用NIO进行读写文件
*
* @param oldFileName 原文件的路径
* @param newFileName 新文件的路径
*/
public static void nioCopy(String oldFileName, String newFileName) {
try {
FileChannel fileChannelIn = new FileInputStream(new File(oldFileName)).getChannel();
FileChannel fileChannelOut = new FileOutputStream(new File(newFileName)).getChannel();
//获取文件大小
long size = fileChannelIn.size();
System.out.printf("文件大小为:%s byte \n", size);
//缓冲
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

long start = System.currentTimeMillis();
while (fileChannelIn.read(byteBuffer) != -1) {
//准备写
byteBuffer.flip();
fileChannelOut.write(byteBuffer);
//准备读
byteBuffer.clear();
}
long end = System.currentTimeMillis();
System.out.printf("NIO方式复制完成,耗时 %s 秒\n", (end - start) / 1000);
//关闭
fileChannelIn.close();
fileChannelOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* IO方式复制文件
*
* @param oldFileName
* @param newFileName
*/
public static void ioCopy(String oldFileName, String newFileName) {
try {
FileInputStream fileInputStream = new FileInputStream(new File(oldFileName));
FileOutputStream fileOutputStream = new FileOutputStream(new File(newFileName));

long length = new File(oldFileName).length();

System.out.printf("文件大小为:%s byte \n", length);
byte[] buffer = new byte[1024];

long start = System.currentTimeMillis();
int len = 0;
//EOF = End Of File,其默认值就是-1
while ((len = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
long end = System.currentTimeMillis();
System.out.printf("IO方式复制完成,耗时 %s 秒\n", (end - start) / 1000);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void ioCopyByLine(String oldFileName, String newFileName) {
try {
BufferedReader reader = new BufferedReader(new FileReader(oldFileName));
BufferedWriter writer = new BufferedWriter(new FileWriter(newFileName));
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
writer.flush();
}
} catch (IOException e) {
System.out.println("error:" + e);
}
}

最近在慢慢适应mbp

然后就在研究怎么方便的写博客

首先就是找了几款markdown编辑器,发现GitHub出品的Atom还不错

插件很丰富,然而下载了第一个terminal插件但是无效,一度折腾了很久

后来搜索了一下,发现这个插件很不错:platformio-ide-terminal

利用快捷键:control + 点(1左边那个)

就可以在当前界面呼出终端,而且是当前目录

也就是在_post目录下,写完了之后执行命令就上传了

还是很方便的。后续还需要慢慢熟悉更多的插件与工具

很多时候写功能或者接口需要进行压力测试,
今天发现jwt在生成token的时候,如果输入都是一样的
仅有一个签发时间不一样,生成的token是有可能是一样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void testCreate() {
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("hisenyuan").build();
ExecutorService pool = new ThreadPoolExecutor(
20,
50,
10000L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(10240),
namedThreadFactory,
new ThreadPoolExecutor.AbortPolicy());
for (int i = 0; i < 50; i++) {
// 需要提交的内容
pool.execute(this::createTokenTest);
}
pool.shutdown();
try {
while (!pool.awaitTermination(500, TimeUnit.MILLISECONDS)) {
LOGGER.debug("Waiting for terminate");
}
} catch (InterruptedException e) {
LOGGER.error(e);
}
}

springmvc正常情况下redirect并且设置指定响应码,异常情况下返回json数据

背景介绍

需求就是正常情况下能redirect到指定的页面
异常的情况下,能够返回JSON格式的错误信息
正常情况和异常情况都需要设置HTTP Code

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String webCharge(HttpServletRequest request, HttpServletResponse response) {
if (1==1) {
response.setStatus(200);
return "redirect:https://github.com/hisenyuan";
} else {
try {
response.setStatus(405);
response.getWriter().write(JSON.toJSONString("hisenyuan"));
return null;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}

一、背景

在各种系统需要加签的时
一般都会把参与签名的数据以get请求参数拼接起来
并且要求有序,这个方法会比较方便

二、实现

2.1 拼接为有序的get请求类字符串

1
2
3
4
5
6
7
8
9
10
public String getSortedStr(Map<String, String> unSortedStr) {
String sortedStr= unSortedStr
.entrySet()
.stream()
.filter(entry -> !StringUtil.isEmpty(entry.getValue()))
.sorted(Map.Entry.comparingByKey())
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&"));
return sortedStr;
}

2.2 把get类参数字符串转为map

1
2
3
4
5
6
7
8
9
private Map<String,String> getMapData(String getStr){
String[] strs = getStr.split("&");
HashMap<String, String> dataMap = new HashMap<>(16);
for (int i = 0; i < strs.length; i++) {
String[] str = strs[i].split("=");
dataMap.put(str[0], str[1]);
}
return dataMap;
}

对于get类字符串没有发现比较好的方法转换为map

这个接口为免费的

一、接口地址

等号后面的为需要翻译的英文

1
http://fanyi.youdao.com/openapi.do?keyfrom=xinlei&key=759115437&type=data&doctype=json&version=1.1&q=hisen

二、代码样例

把全世界200+国家和地区的名字翻译为英文,并且入库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Autowired
SmsCountryService countryService;

@Test
public void updateZhName(){
// 有道翻译接口
String url = "http://fanyi.youdao.com/openapi.do?keyfrom=xinlei&key=759115437&type=data&doctype=json&version=1.1&q=";
// 查询出所有的英文国家名字
List<SmsCountry> countries = countryService.queryAllName();
// httpclient
CloseableHttpClient client = HttpClientBuilder.create().build();
// 翻译每个国家的名字,并且更新数据库
for (SmsCountry country:countries) {
HttpGet request = new HttpGet(url + URLEncoder.encode(country.getName()));
try {
CloseableHttpResponse response = client.execute(request);
String str = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject jsonObject = JSON.parseObject(str);
// 取出json字符串中数组的值
String s = (String) jsonObject.getJSONArray("translation").get(0);
System.out.println(">>>>> " + s);
if (!StringUtil.isEmpty(s)){
SmsCountry smsCountry = new SmsCountry();
smsCountry.setId(country.getId());
smsCountry.setNameZh(s);
countryService.updateNameById(smsCountry);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

利用Java8的流和lambda表达式能很方便的对list对象进行去重
而且不会造成代码入侵

插播:Java8 对List进行求和、分组、提取对象单个属性:https://www.jianshu.com/p/c71eaeaaf30c

下面的例子仅供参考
github:https://github.com/hisenyuan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.hisen.collection.list.duplicate;

import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSON;
import Java.util.ArrayList;
import Java.util.List;
import Java.util.concurrent.ConcurrentHashMap;
import Java.util.function.Function;
import Java.util.function.Predicate;
import Java.util.stream.Collectors;

/**
* @author hisenyuan
* @time 2018/4/19 14:06
* @description list对象根据属性去重 lambda + stream
*/
public class ListDuplicateTest {


public static void main(String[] args) {
List<Person> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Person person = new Person();
person.setAge(10);
person.setName("hsien");
person.setWeight(i);
list.add(person);
}
Person bean = new Person();
bean.setName("hisenyuan");
bean.setAge(33);
bean.setWeight(65);
list.add(bean);

List<Person> collect = list.stream().filter(distinctByKey(Person::getName))
.collect(Collectors.toList());
System.out.println(JSON.toJSONString(collect));
}

/**
* 函数式接口 T -> bollean
* @param keyExtractor
* @param <T>
* @return
*/
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>(16);
return t -> map.putIfAbsent(keyExtractor.apply(t),Boolean.TRUE) == null;

// 这个也可以,不过感觉效率要低一些,线程不是那么安全
// Set<Object> seen = ConcurrentHashMap.newKeySet();
// return t -> seen.add(keyExtractor.apply(t));
}

/**
* 内部类
*/
static class Person {
private int age;
private String name;
private int weight;

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getWeight() {
return weight;
}

public Person() {
}
public void setWeight(int weight) {
this.weight = weight;
}
}
}

1
effective Java # 2

Builder模式,不直接生成想要的对象,而是让客户端利用所有必要的参数调用构造器,

得到一个builder对象。然后客户端在builder对象上调用类似于setter的方法,

来设置每个相关可选的参数,最后调用无参的build来生成不可变的对象。

完整代码+测试:github:完整代码+测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class NutritionFacts {

private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;

private NutritionFacts(Builder builder) {
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}

public static class Builder {

// Required parameters
private final int servingSize;
private final int servings;

private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;

public Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
}

public Builder calories(int val) {
calories = val;
return this;
}

public Builder fat(int val) {
this.fat = val;
return this;
}

public Builder carbohydrate(int val) {
this.carbohydrate = val;
return this;
}

public Builder sodium(int val) {
this.sodium = val;
return this;
}

public NutritionFacts build() {
return new NutritionFacts(this);
}
}



@Override
public String toString() {
return "NutritionFacts{" +
"servingSize=" + servingSize +
", servings=" + servings +
", calories=" + calories +
", fat=" + fat +
", sodium=" + sodium +
", carbohydrate=" + carbohydrate +
'}';
}
}

effective Java:

1
单元素的枚举类型已经成为实现Singleton的最佳方法

理由:

  1. 因为枚举单例有序列化和线程安全的保证
  2. 避免反射和并发困扰

单例模式模式:完整代码+测试

主要代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class EnumSingleton {

private EnumSingleton() {
}

public static EnumSingleton getInstance() {
return Singleton.INSTANCE.getInstance();
}

private enum Singleton {
INSTANCE;
private EnumSingleton singleton;

Singleton() {
singleton = new EnumSingleton();
}

public EnumSingleton getInstance() {
return singleton;
}
}
}