总结一下与字节数组相关的IO操作。
关于 把十六进制的位串转化为byte数组,请参阅 http://hw1287789687.iteye.com/blog/1882644
(1)从InputStream 读取字节数组
方式一:
/***
* Has been tested
*
* @param in
* @return
* @throws IOException
*/
public static byte[] readBytes3(InputStream in) throws IOException {
BufferedInputStream bufin = new BufferedInputStream(in);
int buffSize = BUFFSIZE_1024;
ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);
// System.out.println("Available bytes:" + in.available());
byte[] temp = new byte[4096];
int size = 0;
while ((size = bufin.read(temp)) != -1) {
out.write(temp, 0, size);
}
bufin.close();
in.close();
byte[] content = out.toByteArray();
return content;
}
说明:先把inputstream的字节读到ByteArrayOutputStream中,读完之后再调用toByteArray() 转化为字节数组。
方式二:
/***
* Has been tested
*
* @param in
* @return
* @throws IOException
*/
public static byte[] readBytes(InputStream in) throws IOException {
byte[] temp = new byte[in.available()];
byte[] result = new byte[0];
int size = 0;
while ((size = in.read(temp)) != -1) {
byte[] readBytes = new byte[size];
System.arraycopy(temp, 0, readBytes, 0, size);
result = mergeArray(result, readBytes);
}
return result;
}
/***
* 合并字节数组
*
* @param a
* @return
*/
public static byte[] mergeArray(byte[]... a) {
// 合并完之后数组的总长度
int index = 0;
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum = sum + a[i].length;
}
byte[] result = new byte[sum];
for (int i = 0; i < a.length; i++) {
int lengthOne = a[i].length;
if (lengthOne == 0) {
continue;
}
// 拷贝数组
System.arraycopy(a[i], 0, result, index, lengthOne);
index = index + lengthOne;
}
return result;
}
(2)把字节数组写入文件
/***
* write byte[] to file
*
* @param bytes
* @param destFile
* @throws IOException
*/
public static void writeBytesToFile(byte[] bytes, File destFile)
throws IOException {
FileOutputStream out = new FileOutputStream(destFile);
write2File(bytes, out);
}
/***
*
* @param bytes
* @param out
* @throws IOException
*/
public static void write2File(byte[] bytes, FileOutputStream out)
throws IOException {
out.write(bytes);
out.close();
}
(3)在已有字节数组基础上追加一个字节
/***
* append a byte.
*
* @param a
* @param b
* @return
*/
public static byte[] appandByte(byte[] a, byte b) {
int length = a.length;
byte[] resultBytes = new byte[length + 1];
System.arraycopy(a, 0, resultBytes, 0, length);
resultBytes[length] = b;
return resultBytes;
}
(4)比较两个字节数组是否相同
/***
* Compare two byte arrays whether are the same.
*
* @param a
* @param b
* @return
*/
public static boolean arrayIsEqual(byte[] a, byte[] b) {
if(a==null&&b==null){
return true;
}
if (a != null && b != null) {
if (a.length != b.length) {
return false;
} else {
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
}
}else {//one is null, the other is not null
return false;
}
return true;
}
(5)查找指定字节findTarget在指定字节数组source中的位置
/***
*
* @param source
* @param findTarget
* :key word
* @param pos
* :where start from
* @return index
*/
public static int findBytes(byte[] source, byte[] findTarget, int pos) {
int i, j, k = 0;
i = pos;
j = 0;
while (i < source.length && j < findTarget.length) {
if (source[i] == findTarget[j]) {
++i;
++j;
if (j == findTarget.length) {
k = k + 1;// k++
break;
// j = 0;
}
} else {
i = i - j + 1;
j = 0;
}
}
return k == 0 ? -1 : i - j;
}
测试代码:
@Test
public void test_arrayIsEqual2(){
System.out.println("test_filterFrontBytes");
byte[] a = new byte[] { 1, 2, 3, 4 };
byte[] b = new byte[] { 1, 2, 3 };
System.out.println( arrayIsEqual(a, b));
}
@Test
public void test_appandByte(){
byte[]bytes=new byte[]{1,2,3};
byte[]resultBytes=appandByte(bytes, (byte)32);
arrayIsEqual(resultBytes, new byte[]{1, 2, 3, 32});
}