{"action":"created","comment":{"url":"https://api.github.com/repos/novoda/download-manager/pulls/comments/34985441","id":34985441,"diff_hunk":"@@ -0,0 +1,87 @@\n+package com.novoda.downloadmanager.lib;\n+\n+import java.io.File;\n+import java.io.FileDescriptor;\n+import java.io.FileInputStream;\n+import java.io.FileOutputStream;\n+import java.io.IOException;\n+import java.io.InputStream;\n+import java.io.OutputStream;\n+import static com.novoda.downloadmanager.lib.IOHelpers.closeAfterWrite;\n+import static com.novoda.downloadmanager.lib.IOHelpers.closeQuietly;\n+\n+class TarFileTruncator {\n+\n+    private static final String COPY_SUFFIX = \".tmp\";\n+    private static final int BLOCK_SIZE = 512;\n+\n+    public long truncateIfNeeded(String fileOriginal) throws StopRequestException {\n+        String fileCopy = fileOriginal + COPY_SUFFIX;\n+        long newLength = copyFileTruncatingLastBytesIfNeeded(fileOriginal, fileCopy);\n+        replace(fileOriginal, fileCopy);\n+        return newLength;\n+    }\n+\n+    private long copyFileTruncatingLastBytesIfNeeded(String fileOriginal, String fileCopy) throws StopRequestException {\n+        OutputStream out = null;\n+        FileDescriptor outFd = null;\n+        InputStream in = null;\n+        try {\n+            FileInputStream fileInputStream = new FileInputStream(fileOriginal);\n+            FileOutputStream fileOutputStream = new FileOutputStream(fileCopy);\n+            out = fileOutputStream;\n+            outFd = fileOutputStream.getFD();\n+            in = fileInputStream;\n+            byte[] buffer = new byte[BLOCK_SIZE];\n+            int readLast;\n+            int readTotal = 0;\n+            do {\n+                readLast = readBlock(fileInputStream, buffer);\n+                if ((readLast == -1) || isFullOfZeroes(buffer)) {\n+                    return readTotal;\n+                }\n+                out.write(buffer, 0, readLast);\n+                readTotal += readLast;\n+            } while (readLast == BLOCK_SIZE);\n+            return readTotal;\n+        } catch (IOException e) {\n+            throw new StopRequestException(DownloadStatus.FILE_ERROR, e);\n+        } finally {\n+            closeAfterWrite(out, outFd);\n+            closeQuietly(in);\n+        }\n+    }\n+\n+    private boolean isFullOfZeroes(byte[] buffer) {\n+        for (byte b : buffer) {\n+            if (b != 0x0) {\n+                return false;\n+            }\n+        }\n+        return true;\n+    }\n+\n+    private int readBlock(FileInputStream fileInputStream, byte[] buffer) throws IOException {\n+        int read = 0;\n+        int readLast;\n+        while (read < BLOCK_SIZE) {\n+            readLast = fileInputStream.read(buffer, read, BLOCK_SIZE - read);\n+            if (readLast == -1) {\n+                return read;\n+            }\n+            read += readLast;\n+        }\n+        return read;\n+    }\n+\n+    private void replace(String fileOriginal, String fileCopy) {\n+        File oldFile = new File(fileOriginal);\n+        File newFile = new File(fileCopy);\n+        boolean deleted = oldFile.delete();\n+        boolean renamed = newFile.renameTo(oldFile);\n+        if(!deleted || !renamed) {","path":"library/src/main/java/com/novoda/downloadmanager/lib/TarFileTruncator.java","position":82,"original_position":82,"commit_id":"8a380fdfd71cb5f783c2eb7c8cf7781c8b39543c","original_commit_id":"165468cdcc94fea2567e3a062488d73418752760","user":{"login":"amlcurran","id":666285,"avatar_url":"https://avatars.githubusercontent.com/u/666285?v=3","gravatar_id":"","url":"https://api.github.com/users/amlcurran","html_url":"https://github.com/amlcurran","followers_url":"https://api.github.com/users/amlcurran/followers","following_url":"https://api.github.com/users/amlcurran/following{/other_user}","gists_url":"https://api.github.com/users/amlcurran/gists{/gist_id}","starred_url":"https://api.github.com/users/amlcurran/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amlcurran/subscriptions","organizations_url":"https://api.github.com/users/amlcurran/orgs","repos_url":"https://api.github.com/users/amlcurran/repos","events_url":"https://api.github.com/users/amlcurran/events{/privacy}","received_events_url":"https://api.github.com/users/amlcurran/received_events","type":"User","site_admin":false},"body":"hmmm, checkstyle should have picked this up...","created_at":"2015-07-20T11:21:22Z","updated_at":"2015-07-20T11:21:22Z","html_url":"https://github.com/novoda/download-manager/pull/120#discussion_r34985441","pull_request_url":"https://api.github.com/repos/novoda/download-manager/pulls/120","_links":{"self":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/comments/34985441"},"html":{"href":"https://github.com/novoda/download-manager/pull/120#discussion_r34985441"},"pull_request":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/120"}}},"pull_request":{"url":"https://api.github.com/repos/novoda/download-manager/pulls/120","id":40340186,"html_url":"https://github.com/novoda/download-manager/pull/120","diff_url":"https://github.com/novoda/download-manager/pull/120.diff","patch_url":"https://github.com/novoda/download-manager/pull/120.patch","issue_url":"https://api.github.com/repos/novoda/download-manager/issues/120","number":120,"state":"open","locked":false,"title":"Allow tar updates","user":{"login":"Dorvaryn","id":610924,"avatar_url":"https://avatars.githubusercontent.com/u/610924?v=3","gravatar_id":"","url":"https://api.github.com/users/Dorvaryn","html_url":"https://github.com/Dorvaryn","followers_url":"https://api.github.com/users/Dorvaryn/followers","following_url":"https://api.github.com/users/Dorvaryn/following{/other_user}","gists_url":"https://api.github.com/users/Dorvaryn/gists{/gist_id}","starred_url":"https://api.github.com/users/Dorvaryn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dorvaryn/subscriptions","organizations_url":"https://api.github.com/users/Dorvaryn/orgs","repos_url":"https://api.github.com/users/Dorvaryn/repos","events_url":"https://api.github.com/users/Dorvaryn/events{/privacy}","received_events_url":"https://api.github.com/users/Dorvaryn/received_events","type":"User","site_admin":false},"body":"This adds the functionality to allow for updates of tar files locally on device after the server file has been updated.\r\n\r\nThis idea is that when the allowTarFileUpdates flag is set the downloaded tar file is processed and the last zero bytes are removed to allow for update. The download is then paused instead of being completed.\r\n\r\nWhenever the file has been updated we can hit resume to allow for a download of the additional bytes needed to update the tar file.","created_at":"2015-07-20T10:45:35Z","updated_at":"2015-07-20T11:21:22Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8c44dc0bf1715ca1bcb0ab179f12550f5d9f607b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/novoda/download-manager/pulls/120/commits","review_comments_url":"https://api.github.com/repos/novoda/download-manager/pulls/120/comments","review_comment_url":"https://api.github.com/repos/novoda/download-manager/pulls/comments{/number}","comments_url":"https://api.github.com/repos/novoda/download-manager/issues/120/comments","statuses_url":"https://api.github.com/repos/novoda/download-manager/statuses/8a380fdfd71cb5f783c2eb7c8cf7781c8b39543c","head":{"label":"novoda:allow_tar_updates","ref":"allow_tar_updates","sha":"8a380fdfd71cb5f783c2eb7c8cf7781c8b39543c","user":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"repo":{"id":35211795,"name":"download-manager","full_name":"novoda/download-manager","owner":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/novoda/download-manager","description":"Download manager based on AOSP DM but allowing downloading to internal private storage","fork":false,"url":"https://api.github.com/repos/novoda/download-manager","forks_url":"https://api.github.com/repos/novoda/download-manager/forks","keys_url":"https://api.github.com/repos/novoda/download-manager/keys{/key_id}","collaborators_url":"https://api.github.com/repos/novoda/download-manager/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/novoda/download-manager/teams","hooks_url":"https://api.github.com/repos/novoda/download-manager/hooks","issue_events_url":"https://api.github.com/repos/novoda/download-manager/issues/events{/number}","events_url":"https://api.github.com/repos/novoda/download-manager/events","assignees_url":"https://api.github.com/repos/novoda/download-manager/assignees{/user}","branches_url":"https://api.github.com/repos/novoda/download-manager/branches{/branch}","tags_url":"https://api.github.com/repos/novoda/download-manager/tags","blobs_url":"https://api.github.com/repos/novoda/download-manager/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/novoda/download-manager/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/novoda/download-manager/git/refs{/sha}","trees_url":"https://api.github.com/repos/novoda/download-manager/git/trees{/sha}","statuses_url":"https://api.github.com/repos/novoda/download-manager/statuses/{sha}","languages_url":"https://api.github.com/repos/novoda/download-manager/languages","stargazers_url":"https://api.github.com/repos/novoda/download-manager/stargazers","contributors_url":"https://api.github.com/repos/novoda/download-manager/contributors","subscribers_url":"https://api.github.com/repos/novoda/download-manager/subscribers","subscription_url":"https://api.github.com/repos/novoda/download-manager/subscription","commits_url":"https://api.github.com/repos/novoda/download-manager/commits{/sha}","git_commits_url":"https://api.github.com/repos/novoda/download-manager/git/commits{/sha}","comments_url":"https://api.github.com/repos/novoda/download-manager/comments{/number}","issue_comment_url":"https://api.github.com/repos/novoda/download-manager/issues/comments{/number}","contents_url":"https://api.github.com/repos/novoda/download-manager/contents/{+path}","compare_url":"https://api.github.com/repos/novoda/download-manager/compare/{base}...{head}","merges_url":"https://api.github.com/repos/novoda/download-manager/merges","archive_url":"https://api.github.com/repos/novoda/download-manager/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/novoda/download-manager/downloads","issues_url":"https://api.github.com/repos/novoda/download-manager/issues{/number}","pulls_url":"https://api.github.com/repos/novoda/download-manager/pulls{/number}","milestones_url":"https://api.github.com/repos/novoda/download-manager/milestones{/number}","notifications_url":"https://api.github.com/repos/novoda/download-manager/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/novoda/download-manager/labels{/name}","releases_url":"https://api.github.com/repos/novoda/download-manager/releases{/id}","created_at":"2015-05-07T09:41:52Z","updated_at":"2015-07-20T07:03:21Z","pushed_at":"2015-07-20T11:19:34Z","git_url":"git://github.com/novoda/download-manager.git","ssh_url":"git@github.com:novoda/download-manager.git","clone_url":"https://github.com/novoda/download-manager.git","svn_url":"https://github.com/novoda/download-manager","homepage":"","size":3985,"stargazers_count":54,"watchers_count":54,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":19,"forks":4,"open_issues":19,"watchers":54,"default_branch":"master"}},"base":{"label":"novoda:master","ref":"master","sha":"24ba1af03192e4d1973350d5dd8accf7315c7ea2","user":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"repo":{"id":35211795,"name":"download-manager","full_name":"novoda/download-manager","owner":{"login":"novoda","id":74874,"avatar_url":"https://avatars.githubusercontent.com/u/74874?v=3","gravatar_id":"","url":"https://api.github.com/users/novoda","html_url":"https://github.com/novoda","followers_url":"https://api.github.com/users/novoda/followers","following_url":"https://api.github.com/users/novoda/following{/other_user}","gists_url":"https://api.github.com/users/novoda/gists{/gist_id}","starred_url":"https://api.github.com/users/novoda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/novoda/subscriptions","organizations_url":"https://api.github.com/users/novoda/orgs","repos_url":"https://api.github.com/users/novoda/repos","events_url":"https://api.github.com/users/novoda/events{/privacy}","received_events_url":"https://api.github.com/users/novoda/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/novoda/download-manager","description":"Download manager based on AOSP DM but allowing downloading to internal private storage","fork":false,"url":"https://api.github.com/repos/novoda/download-manager","forks_url":"https://api.github.com/repos/novoda/download-manager/forks","keys_url":"https://api.github.com/repos/novoda/download-manager/keys{/key_id}","collaborators_url":"https://api.github.com/repos/novoda/download-manager/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/novoda/download-manager/teams","hooks_url":"https://api.github.com/repos/novoda/download-manager/hooks","issue_events_url":"https://api.github.com/repos/novoda/download-manager/issues/events{/number}","events_url":"https://api.github.com/repos/novoda/download-manager/events","assignees_url":"https://api.github.com/repos/novoda/download-manager/assignees{/user}","branches_url":"https://api.github.com/repos/novoda/download-manager/branches{/branch}","tags_url":"https://api.github.com/repos/novoda/download-manager/tags","blobs_url":"https://api.github.com/repos/novoda/download-manager/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/novoda/download-manager/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/novoda/download-manager/git/refs{/sha}","trees_url":"https://api.github.com/repos/novoda/download-manager/git/trees{/sha}","statuses_url":"https://api.github.com/repos/novoda/download-manager/statuses/{sha}","languages_url":"https://api.github.com/repos/novoda/download-manager/languages","stargazers_url":"https://api.github.com/repos/novoda/download-manager/stargazers","contributors_url":"https://api.github.com/repos/novoda/download-manager/contributors","subscribers_url":"https://api.github.com/repos/novoda/download-manager/subscribers","subscription_url":"https://api.github.com/repos/novoda/download-manager/subscription","commits_url":"https://api.github.com/repos/novoda/download-manager/commits{/sha}","git_commits_url":"https://api.github.com/repos/novoda/download-manager/git/commits{/sha}","comments_url":"https://api.github.com/repos/novoda/download-manager/comments{/number}","issue_comment_url":"https://api.github.com/repos/novoda/download-manager/issues/comments{/number}","contents_url":"https://api.github.com/repos/novoda/download-manager/contents/{+path}","compare_url":"https://api.github.com/repos/novoda/download-manager/compare/{base}...{head}","merges_url":"https://api.github.com/repos/novoda/download-manager/merges","archive_url":"https://api.github.com/repos/novoda/download-manager/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/novoda/download-manager/downloads","issues_url":"https://api.github.com/repos/novoda/download-manager/issues{/number}","pulls_url":"https://api.github.com/repos/novoda/download-manager/pulls{/number}","milestones_url":"https://api.github.com/repos/novoda/download-manager/milestones{/number}","notifications_url":"https://api.github.com/repos/novoda/download-manager/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/novoda/download-manager/labels{/name}","releases_url":"https://api.github.com/repos/novoda/download-manager/releases{/id}","created_at":"2015-05-07T09:41:52Z","updated_at":"2015-07-20T07:03:21Z","pushed_at":"2015-07-20T11:19:34Z","git_url":"git://github.com/novoda/download-manager.git","ssh_url":"git@github.com:novoda/download-manager.git","clone_url":"https://github.com/novoda/download-manager.git","svn_url":"https://github.com/novoda/download-manager","homepage":"","size":3985,"stargazers_count":54,"watchers_count":54,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":19,"forks":4,"open_issues":19,"watchers":54,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/120"},"html":{"href":"https://github.com/novoda/download-manager/pull/120"},"issue":{"href":"https://api.github.com/repos/novoda/download-manager/issues/120"},"comments":{"href":"https://api.github.com/repos/novoda/download-manager/issues/120/comments"},"review_comments":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/120/comments"},"review_comment":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/novoda/download-manager/pulls/120/commits"},"statuses":{"href":"https://api.github.com/repos/novoda/download-manager/statuses/8a380fdfd71cb5f783c2eb7c8cf7781c8b39543c"}}}}