java socket接收保證能讀完資料的方法
阿新 • • 發佈:2019-01-26
// private static byte[] readData(InputStream in,byte[] bData) throws IOException{
// int readLength = in.read(bData);
// if(readLength!=bData.length){
// byte[] temp2 = readData(in,new byte[bData.length-readLength]);
// System.arraycopy(temp2, 0, bData, readLength, temp2.length);
// return bData;
// }else{
// return bData;
// }
// }
// private static void readData(InputStream in,byte[] bData) throws IOException{
// readData(in,bData,0,bData.length);
// }
// private static void readData(InputStream in,byte[] bData,int off,int length) throws IOException{
// int readLength = in.read(bData, off, length);
// if(readLength!=length){
// readData(in,bData,readLength+off,length-readLength);
// }
// }
// private static void readData(InputStream in,byte[] bData,int off,int length) throws IOException{
//
// while(true){
// int readLength = in.read(bData, off, length);
// if(readLength!=length){
// off = readLength+off;
// length = length-readLength;
// }else{
// break;
// }
// }
// }
// private static void readData(InputStream in,byte[] bData,int off,int length) throws IOException{
// int readLength = 0;
// do{
// off = readLength+off;
// length = length-readLength;
// readLength = in.read(bData, off, length);
// }while(readLength!=length);
// }
/**
* 最終使用此方法
* @param in 輸入流
* @param bData 讀取資料
* @throws IOException
*/
private static void readData(InputStream in,byte[] bData) throws IOException{
int off = 0;
int length = bData.length;
int readLength = 0;
do{
off = readLength+off;
length = length-readLength;
readLength = in.read(bData, off, length);
}while(readLength!=length);
}