java socket接收保证能读完数据的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guanzhengyinqin/article/details/79402165

//    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);
    }

猜你喜欢

转载自blog.csdn.net/guanzhengyinqin/article/details/79402165