byte数组自动扩容


package com.digican.ztest;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

/**
* 数组自动扩容;在实际的socket通讯中经常会用到数组自动增加
* @author yaodaqing
*
*/
public class TestByte {

private static final int BUFFER_SIZE = 512;

public static void main(String[] args) {
try {
DataInputStream dis = new DataInputStream(new FileInputStream(new File("d:/abc.txt")));
byte[] b = new byte[BUFFER_SIZE];
int i = 0;
int t = 0;
while((i = dis.read()) != -1){
//开始:数组自动扩容
if(t==b.length){
byte[] temp = new byte[t+1];
System.arraycopy(b, 0, temp, 0, b.length);
b = temp;
temp = null;
}
b[t] = (byte)i;
t++;
//结束

//此处是处理b的程序。。。
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

你可能感兴趣的:(byte[]处理)