a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
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
package com.basic.security.manager;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class ByteArrayManager {
 
    public static Map<Integer, List<byte[]>> byteArrayListMap = new HashMap<>();
    public static int lengthIndex = 0;
 
    public static synchronized byte[] newBytes(int length) {
        byte[] newBytes = null;
        try {
            List<byte[]> byteArrayList = byteArrayListMap.get(length);
            if (byteArrayList == null) {
                byteArrayList = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    byte[] bytes = new byte[length];
                    byteArrayList.add(bytes);
                }
                byteArrayListMap.put(length, byteArrayList);
            }
            lengthIndex = lengthIndex % byteArrayList.size();
            newBytes = byteArrayList.get(lengthIndex);
            lengthIndex++;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (newBytes == null) {
            newBytes = new byte[length];
        }
        return newBytes;
    }
}