{"action":"created","comment":{"url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments/36181575","id":36181575,"diff_hunk":"@@ -0,0 +1,137 @@\n+/*\n+ * Copyright 2015 MongoDB, Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *    http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package com.mongodb;\n+\n+import com.mongodb.client.GridFSUploadStream;\n+import com.mongodb.client.MongoCollection;\n+import org.bson.BsonBinary;\n+import org.bson.BsonDateTime;\n+import org.bson.BsonDocument;\n+import org.bson.BsonInt32;\n+import org.bson.BsonInt64;\n+import org.bson.BsonObjectId;\n+import org.bson.BsonString;\n+import org.bson.types.ObjectId;\n+\n+import java.io.IOException;\n+import java.io.OutputStream;\n+import java.security.MessageDigest;\n+import java.security.NoSuchAlgorithmException;\n+\n+import static com.mongodb.assertions.Assertions.notNull;\n+import static com.mongodb.util.Util.toHex;\n+\n+final class GridFSUploadStreamImpl extends OutputStream implements GridFSUploadStream {\n+    private final MongoCollection<BsonDocument> filesCollection;\n+    private final MongoCollection<BsonDocument> chunksCollection;\n+    private final ObjectId fileId;\n+    private final String filename;\n+    private final int chunkSizeInBytes;\n+    private final BsonDocument metadata;\n+    private final MessageDigest md5;\n+    private boolean closed;\n+    private byte[] buffer;\n+    private long lengthInBytes;\n+    private int bufferOffset;\n+    private int chunkIndex;\n+\n+    GridFSUploadStreamImpl(final MongoCollection<BsonDocument> filesCollection, final MongoCollection<BsonDocument> chunksCollection,\n+                           final ObjectId fileId, final String filename, final int chunkSizeInBytes, final BsonDocument metadata) {\n+        this.filesCollection = notNull(\"files collection\", filesCollection);\n+        this.chunksCollection = notNull(\"chunks collection\", chunksCollection);\n+        this.fileId = notNull(\"File Id\", fileId);\n+        this.filename = notNull(\"filename\", filename);\n+        this.chunkSizeInBytes = chunkSizeInBytes;\n+        this.metadata = metadata;\n+        try {\n+            md5 = MessageDigest.getInstance(\"MD5\");\n+        } catch (NoSuchAlgorithmException e) {\n+            throw new MongoGridFSException(\"No MD5 message digest available, cannot upload file\", e);\n+        }\n+        chunkIndex = 0;\n+        bufferOffset = 0;\n+        buffer = new byte[chunkSizeInBytes];\n+    }\n+\n+    @Override\n+    public ObjectId getFileId() {\n+        return fileId;\n+    }\n+\n+    @Override\n+    public OutputStream getOutputStream() {\n+        return this;\n+    }\n+\n+    @Override\n+    public void write(final int b) throws IOException {\n+        checkClosed();\n+        buffer[bufferOffset++] = (byte) (0xFF & b);\n+        lengthInBytes++;\n+        if (bufferOffset == chunkSizeInBytes) {\n+            writeChunks();\n+        }\n+    }\n+\n+    @Override\n+    public void close() throws IOException {\n+        checkClosed();\n+        writeChunks();\n+        closed = true;\n+        BsonDocument fileDocument = new BsonDocument(\"_id\", new BsonObjectId(fileId))\n+                .append(\"length\", new BsonInt64(lengthInBytes))\n+                .append(\"chunkSize\", new BsonInt32(chunkSizeInBytes))\n+                .append(\"uploadDate\", new BsonDateTime(System.currentTimeMillis()))\n+                .append(\"md5\", new BsonString(toHex(md5.digest())))\n+                .append(\"filename\", new BsonString(filename));\n+\n+        if (!metadata.isEmpty()) {\n+            fileDocument.append(\"metadata\", metadata);","path":"driver/src/main/com/mongodb/GridFSUploadStreamImpl.java","position":103,"original_position":103,"commit_id":"8d17bae8d682bdcc4201a52993da01043a7489a5","original_commit_id":"8d17bae8d682bdcc4201a52993da01043a7489a5","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"body":"Done.","created_at":"2015-08-04T12:25:56Z","updated_at":"2015-08-04T12:25:56Z","html_url":"https://github.com/rozza/mongo-java-driver/pull/100#discussion_r36181575","pull_request_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/100","_links":{"self":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments/36181575"},"html":{"href":"https://github.com/rozza/mongo-java-driver/pull/100#discussion_r36181575"},"pull_request":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/100"}}},"pull_request":{"url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/100","id":41249869,"html_url":"https://github.com/rozza/mongo-java-driver/pull/100","diff_url":"https://github.com/rozza/mongo-java-driver/pull/100.diff","patch_url":"https://github.com/rozza/mongo-java-driver/pull/100.patch","issue_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/100","number":100,"state":"open","locked":false,"title":"GridFS 2015","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"body":"\r\nHad to update codenarc as it couldn't parse the GridFS Smoke test.\r\nAs the yml tests are being refactored I paused work on that until they have been updated.\n\n<!-- Reviewable:start -->\n[<img src=\"https://reviewable.io/review_button.png\" height=40 alt=\"Review on Reviewable\"/>](https://reviewable.io/reviews/rozza/mongo-java-driver/100)\n<!-- Reviewable:end -->\n","created_at":"2015-07-30T15:49:54Z","updated_at":"2015-08-04T12:25:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a5cb33874313963fe94ef1790ad021ecad4c5d0a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/100/commits","review_comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/100/comments","review_comment_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/100/comments","statuses_url":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/8d17bae8d682bdcc4201a52993da01043a7489a5","head":{"label":"rozza:j1713","ref":"j1713","sha":"8d17bae8d682bdcc4201a52993da01043a7489a5","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"repo":{"id":10246360,"name":"mongo-java-driver","full_name":"rozza/mongo-java-driver","owner":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rozza/mongo-java-driver","description":"java driver for mongo","fork":true,"url":"https://api.github.com/repos/rozza/mongo-java-driver","forks_url":"https://api.github.com/repos/rozza/mongo-java-driver/forks","keys_url":"https://api.github.com/repos/rozza/mongo-java-driver/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rozza/mongo-java-driver/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rozza/mongo-java-driver/teams","hooks_url":"https://api.github.com/repos/rozza/mongo-java-driver/hooks","issue_events_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/events{/number}","events_url":"https://api.github.com/repos/rozza/mongo-java-driver/events","assignees_url":"https://api.github.com/repos/rozza/mongo-java-driver/assignees{/user}","branches_url":"https://api.github.com/repos/rozza/mongo-java-driver/branches{/branch}","tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/tags","blobs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/refs{/sha}","trees_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/{sha}","languages_url":"https://api.github.com/repos/rozza/mongo-java-driver/languages","stargazers_url":"https://api.github.com/repos/rozza/mongo-java-driver/stargazers","contributors_url":"https://api.github.com/repos/rozza/mongo-java-driver/contributors","subscribers_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscribers","subscription_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscription","commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/commits{/sha}","git_commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/commits{/sha}","comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/comments{/number}","issue_comment_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/comments{/number}","contents_url":"https://api.github.com/repos/rozza/mongo-java-driver/contents/{+path}","compare_url":"https://api.github.com/repos/rozza/mongo-java-driver/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rozza/mongo-java-driver/merges","archive_url":"https://api.github.com/repos/rozza/mongo-java-driver/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rozza/mongo-java-driver/downloads","issues_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues{/number}","pulls_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls{/number}","milestones_url":"https://api.github.com/repos/rozza/mongo-java-driver/milestones{/number}","notifications_url":"https://api.github.com/repos/rozza/mongo-java-driver/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rozza/mongo-java-driver/labels{/name}","releases_url":"https://api.github.com/repos/rozza/mongo-java-driver/releases{/id}","created_at":"2013-05-23T15:06:03Z","updated_at":"2015-05-07T14:16:45Z","pushed_at":"2015-07-30T15:50:41Z","git_url":"git://github.com/rozza/mongo-java-driver.git","ssh_url":"git@github.com:rozza/mongo-java-driver.git","clone_url":"https://github.com/rozza/mongo-java-driver.git","svn_url":"https://github.com/rozza/mongo-java-driver","homepage":"","size":42094,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"rozza:master","ref":"master","sha":"48488539ec59d82aeb90328f9bb6795ab67b361b","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"repo":{"id":10246360,"name":"mongo-java-driver","full_name":"rozza/mongo-java-driver","owner":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rozza/mongo-java-driver","description":"java driver for mongo","fork":true,"url":"https://api.github.com/repos/rozza/mongo-java-driver","forks_url":"https://api.github.com/repos/rozza/mongo-java-driver/forks","keys_url":"https://api.github.com/repos/rozza/mongo-java-driver/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rozza/mongo-java-driver/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rozza/mongo-java-driver/teams","hooks_url":"https://api.github.com/repos/rozza/mongo-java-driver/hooks","issue_events_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/events{/number}","events_url":"https://api.github.com/repos/rozza/mongo-java-driver/events","assignees_url":"https://api.github.com/repos/rozza/mongo-java-driver/assignees{/user}","branches_url":"https://api.github.com/repos/rozza/mongo-java-driver/branches{/branch}","tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/tags","blobs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/refs{/sha}","trees_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/{sha}","languages_url":"https://api.github.com/repos/rozza/mongo-java-driver/languages","stargazers_url":"https://api.github.com/repos/rozza/mongo-java-driver/stargazers","contributors_url":"https://api.github.com/repos/rozza/mongo-java-driver/contributors","subscribers_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscribers","subscription_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscription","commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/commits{/sha}","git_commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/commits{/sha}","comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/comments{/number}","issue_comment_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/comments{/number}","contents_url":"https://api.github.com/repos/rozza/mongo-java-driver/contents/{+path}","compare_url":"https://api.github.com/repos/rozza/mongo-java-driver/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rozza/mongo-java-driver/merges","archive_url":"https://api.github.com/repos/rozza/mongo-java-driver/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rozza/mongo-java-driver/downloads","issues_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues{/number}","pulls_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls{/number}","milestones_url":"https://api.github.com/repos/rozza/mongo-java-driver/milestones{/number}","notifications_url":"https://api.github.com/repos/rozza/mongo-java-driver/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rozza/mongo-java-driver/labels{/name}","releases_url":"https://api.github.com/repos/rozza/mongo-java-driver/releases{/id}","created_at":"2013-05-23T15:06:03Z","updated_at":"2015-05-07T14:16:45Z","pushed_at":"2015-07-30T15:50:41Z","git_url":"git://github.com/rozza/mongo-java-driver.git","ssh_url":"git@github.com:rozza/mongo-java-driver.git","clone_url":"https://github.com/rozza/mongo-java-driver.git","svn_url":"https://github.com/rozza/mongo-java-driver","homepage":"","size":42094,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/100"},"html":{"href":"https://github.com/rozza/mongo-java-driver/pull/100"},"issue":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/issues/100"},"comments":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/issues/100/comments"},"review_comments":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/100/comments"},"review_comment":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/100/commits"},"statuses":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/8d17bae8d682bdcc4201a52993da01043a7489a5"}}}}