inputStream = socket.getInputStream(); bufferedInputStream =new BufferedInputStream(inputStream); byteArrayOutputStream=new ByteArrayOutputStream(); byte[] buffer=null; int len=0; byte []buf=new byte[2048]; while((len=bufferedInputStream.read(buf))!=-1){ byteArrayOutputStream.write(buf, 0, len); } byteArrayOutputStream.flush(); buffer=byteArrayOutputStream.toByteArray();