Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void downloadJar(String fileName, long crc32Server) throws IOException {
- System.out.println("Downloading file '" + fileName + "' from server '" + mServer + "'.");
- HttpURLConnection sourceConnection = null;
- BufferedInputStream inputStream = null;
- BufferedWriter fileWriter = null;
- long crc32Client;
- try {
- URL sourceURL = new URL(fileName);
- try {
- sourceConnection = (HttpURLConnection)sourceURL.openConnection();
- }
- catch (MalformedURLException exc) {
- throw new RuntimeException("Configured URL caused a MalformedURLException: ", exc);
- }
- sourceConnection.setRequestProperty("Accept-Encoding", "zip, jar");
- sourceConnection.connect();
- inputStream = new BufferedInputStream(sourceConnection.getInputStream());
- fileWriter = new BufferedWriter(new FileWriter(targetFolder + File.separator + fileName));
- CRC32 crc32 = new CRC32();
- for (int singleByte = inputStream.read(); singleByte != -1; singleByte = inputStream.read()) {
- fileWriter.write(singleByte);
- crc32.update(singleByte);
- }
- crc32Client = crc32.getValue();
- }
- finally {
- if (inputStream != null) {
- inputStream.close();
- }
- if (fileWriter != null) {
- fileWriter.flush();
- fileWriter.close();
- }
- if (sourceConnection != null) {
- sourceConnection.disconnect();
- }
- }
- if (crc32Client != crc32Server) {
- // deleteFile(fileName);
- throw new IOException("CRC32 did not match for file '" + fileName + "': " + crc32Client + "!="
- + crc32Server);
- }
- }
Add Comment
Please, Sign In to add comment