1、
2、
3、
作者:wuxinliulei
链接:https://www.zhihu.com/question/20304015/answer/71645651
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
/**
*检验一个字符串是否是经过Base64处理过的
*
*/
public class TestBase64
{
public static final char[] BASE64_CODE = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', '+', '/', '=' };
public static final int MAX_BUFF_SIZE = 4000000;
public static boolean doCheck() throws IOException
{
final File file = new File("src.txt");
final BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
ByteBuffer buffer = ByteBuffer.allocate(1024);
String line = null;
String lastLine = null;
while ((line = br.readLine()) != null)
{
lastLine = line;
final byte[] bytes = line.getBytes();
tryAllocate(buffer, bytes.length);
buffer.put(bytes);
}
// 检查最后两个字节
final byte[] lastLineBytes = lastLine.getBytes();
// 等号个数
int equalsNum = 0;
for (int i = lastLineBytes.length - 1; i >= lastLineBytes.length - 2; i--)
{
if (lastLineBytes[i] == '=')
{
equalsNum++;
}
}
final byte[] src = buffer.toString().getBytes();
for (int i = 0; i < src.length - equalsNum; i++)
{
final char c = (char) src[i];
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '+' || c == '/')
{
continue;
}
return false;
}
if ((src.length - equalsNum) % 4 != 0)
{
return false;
}
return true;
}
public static ByteBuffer tryAllocate(ByteBuffer buffer, int length)
{
if (length > buffer.remaining())
{
buffer.flip();
return ByteBuffer.allocate(roundup(buffer.limit() + length)).put(buffer);
}
return buffer;
}
public static int roundup(int length)
{
if (length > MAX_BUFF_SIZE)
{
throw new IllegalArgumentException("length too large!");
}
int capacity = 16;
while (length < capacity)
{
capacity = capacity << 1;
}
return capacity;
}
}
4、
网址为
https://blog.csdn.net/wo541075754/article/details/81734770
这篇博客详细的介绍了Base64编码原理