{"action":"created","comment":{"url":"https://api.github.com/repos/netty/netty/pulls/comments/58027811","id":58027811,"diff_hunk":"@@ -0,0 +1,232 @@\n+/*\n+ * Copyright 2016 The Netty Project\n+ *\n+ * The Netty Project licenses this file to you under the Apache License, version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance with the License. You may obtain a\n+ * 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 distributed under the License\n+ * is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\n+ * or implied. See the License for the specific language governing permissions and limitations under\n+ * the License.\n+ */\n+\n+package io.netty.handler.codec.redis;\n+\n+import io.netty.buffer.ByteBuf;\n+import io.netty.channel.ChannelHandlerContext;\n+import io.netty.handler.codec.ByteToMessageDecoder;\n+import io.netty.handler.codec.CodecException;\n+import io.netty.util.ByteProcessor;\n+\n+import java.util.List;\n+\n+/**\n+ * Decodes the Redis protocol into {@link RedisMessage} objects following\n+ * <a href=\"http://redis.io/topics/protocol\">RESP (REdis Serialization Protocol)</a>.\n+ *\n+ * {@link RedisMessage} parts can be aggregated to {@link RedisMessage} using\n+ * {@link RedisMessageAggregator} or processed directly.\n+ */\n+public class RedisDecoder extends ByteToMessageDecoder {\n+\n+    private static final int CRLF_LENGTH = 2;\n+    private static final long NULL_RESPONSE = -1;\n+\n+    private final ToLongProcessor toLongProcessor = new ToLongProcessor();\n+\n+    // current decoding states\n+    private State state;\n+    private RedisMessageType type;\n+\n+    // for decoding bulk string\n+    private long bulkStringLength;\n+\n+    private enum State {\n+        DECODE_TYPE,\n+        DECODE_INLINE, // SIMPLE_STRING, ERROR, INTEGER\n+        DECODE_LENGTH, // BULK_STRING, ARRAY_HEADER\n+        DECODE_BULK_STRING,\n+    }\n+\n+    public RedisDecoder() {\n+        resetDecoder();\n+    }\n+\n+    @Override\n+    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {\n+        try {\n+            boolean next;\n+            do {\n+                switch (state) {\n+                case DECODE_TYPE:\n+                    next = decodeType(in);\n+                    break;\n+                case DECODE_INLINE:\n+                    next = decodeInline(in, out);\n+                    break;\n+                case DECODE_LENGTH:\n+                    next = decodeLength(in, out);\n+                    break;\n+                case DECODE_BULK_STRING:\n+                    next = decodeBulkString(in, out);\n+                    break;\n+                default:\n+                    throw new CodecException(\"Unknown state: \" + state);\n+                }\n+            } while (next);\n+        } catch (Exception e) {\n+            resetDecoder();\n+            throw new CodecException(e);\n+        }\n+    }\n+\n+    private void resetDecoder() {\n+        state = State.DECODE_TYPE;\n+    }\n+\n+    private boolean decodeType(ByteBuf in) throws Exception {\n+        if (in.readableBytes() < 1) {\n+            return false;\n+        }\n+        type = RedisMessageType.valueOf(in.readByte());\n+        state = type.isInline() ? State.DECODE_INLINE : State.DECODE_LENGTH;\n+        return true;\n+    }\n+\n+    private boolean decodeInline(ByteBuf in, List<Object> out) throws Exception {\n+        ByteBuf lineBytes = readLine(in);\n+        if (lineBytes == null) {\n+            return false;\n+        }\n+        addMessageToOut(out, newInlineRedisMessage(type, lineBytes));\n+        return true;\n+    }\n+\n+    private boolean decodeLength(ByteBuf in, List<Object> out) throws Exception {\n+        ByteBuf lineByteBuf = readLine(in);\n+        if (lineByteBuf == null) {\n+            return false;\n+        }\n+        final long length = parseRedisNumber(lineByteBuf);\n+        if (type == RedisMessageType.ARRAY_HEADER) {\n+            addMessageToOut(out, RedisMessageFactory.createArrayHeader(length));\n+            return true;\n+        } else if (type == RedisMessageType.BULK_STRING) {\n+            bulkStringLength = length;\n+            state = State.DECODE_BULK_STRING;\n+            return true;\n+        } else {\n+            throw new CodecException(\"bad type: \" + type);\n+        }\n+    }\n+\n+    private boolean decodeBulkString(ByteBuf in, List<Object> out) throws Exception {\n+        if (bulkStringLength == NULL_RESPONSE) {\n+            // $-1\\r\\n\n+            addMessageToOut(out, RedisMessageFactory.nullBulkString());\n+        } else if (bulkStringLength == 0L) {\n+            // $0\\r\\n <here> \\r\\n\n+            if (in.readableBytes() < CRLF_LENGTH) {\n+                return false;\n+            }\n+            in.skipBytes(CRLF_LENGTH);\n+            addMessageToOut(out, RedisMessageFactory.emptyBulkString());\n+        } else if (bulkStringLength > 0L) {\n+            // ${bulkStringLength}\\r\\n <here> {data...}\\r\\n\n+            if (in.readableBytes() < bulkStringLength + CRLF_LENGTH) {\n+                return false;\n+            }\n+            ByteBuf content = in.readSlice((int) bulkStringLength);\n+            in.skipBytes(CRLF_LENGTH);","path":"codec-redis/src/main/java/io/netty/handler/codec/redis/RedisDecoder.java","position":143,"original_position":143,"commit_id":"d51aaf14760186811932b81a7badf4268dbc97af","original_commit_id":"d51aaf14760186811932b81a7badf4268dbc97af","user":{"login":"normanmaurer","id":439362,"avatar_url":"https://avatars.githubusercontent.com/u/439362?v=3","gravatar_id":"","url":"https://api.github.com/users/normanmaurer","html_url":"https://github.com/normanmaurer","followers_url":"https://api.github.com/users/normanmaurer/followers","following_url":"https://api.github.com/users/normanmaurer/following{/other_user}","gists_url":"https://api.github.com/users/normanmaurer/gists{/gist_id}","starred_url":"https://api.github.com/users/normanmaurer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/normanmaurer/subscriptions","organizations_url":"https://api.github.com/users/normanmaurer/orgs","repos_url":"https://api.github.com/users/normanmaurer/repos","events_url":"https://api.github.com/users/normanmaurer/events{/privacy}","received_events_url":"https://api.github.com/users/normanmaurer/received_events","type":"User","site_admin":false},"body":"never mind... I just saw that the skip is not done on the slices content but on the in buffer. Need more coffee :D","created_at":"2016-03-31T09:53:23Z","updated_at":"2016-03-31T09:53:23Z","html_url":"https://github.com/netty/netty/pull/5001#discussion_r58027811","pull_request_url":"https://api.github.com/repos/netty/netty/pulls/5001","_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/comments/58027811"},"html":{"href":"https://github.com/netty/netty/pull/5001#discussion_r58027811"},"pull_request":{"href":"https://api.github.com/repos/netty/netty/pulls/5001"}}},"pull_request":{"url":"https://api.github.com/repos/netty/netty/pulls/5001","id":63456402,"html_url":"https://github.com/netty/netty/pull/5001","diff_url":"https://github.com/netty/netty/pull/5001.diff","patch_url":"https://github.com/netty/netty/pull/5001.patch","issue_url":"https://api.github.com/repos/netty/netty/issues/5001","number":5001,"state":"open","locked":false,"title":"Implement codec-redis","user":{"login":"jongyeol","id":23586,"avatar_url":"https://avatars.githubusercontent.com/u/23586?v=3","gravatar_id":"","url":"https://api.github.com/users/jongyeol","html_url":"https://github.com/jongyeol","followers_url":"https://api.github.com/users/jongyeol/followers","following_url":"https://api.github.com/users/jongyeol/following{/other_user}","gists_url":"https://api.github.com/users/jongyeol/gists{/gist_id}","starred_url":"https://api.github.com/users/jongyeol/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jongyeol/subscriptions","organizations_url":"https://api.github.com/users/jongyeol/orgs","repos_url":"https://api.github.com/users/jongyeol/repos","events_url":"https://api.github.com/users/jongyeol/events{/privacy}","received_events_url":"https://api.github.com/users/jongyeol/received_events","type":"User","site_admin":false},"body":"Motivation:\r\n\r\n- To encode/decode RESP (REdis Serialization Protocol) using Netty\r\n- http://redis.io/topics/protocol\r\n\r\nModifications:\r\n\r\n- Add RedisEncoder\r\n- Add RedisObjectDecoder and RedisObjectAggregator\r\n- Add tests\r\n\r\nResult:\r\n\r\n- Added codec-redis\r\n- codec-redis can encode/decode RESP (REdis Serialization Protocol)","created_at":"2016-03-19T02:24:48Z","updated_at":"2016-03-31T09:53:23Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4e1265b8a1329c169804ee0a4f24a12d793823fe","assignee":{"login":"Scottmitch","id":7562868,"avatar_url":"https://avatars.githubusercontent.com/u/7562868?v=3","gravatar_id":"","url":"https://api.github.com/users/Scottmitch","html_url":"https://github.com/Scottmitch","followers_url":"https://api.github.com/users/Scottmitch/followers","following_url":"https://api.github.com/users/Scottmitch/following{/other_user}","gists_url":"https://api.github.com/users/Scottmitch/gists{/gist_id}","starred_url":"https://api.github.com/users/Scottmitch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Scottmitch/subscriptions","organizations_url":"https://api.github.com/users/Scottmitch/orgs","repos_url":"https://api.github.com/users/Scottmitch/repos","events_url":"https://api.github.com/users/Scottmitch/events{/privacy}","received_events_url":"https://api.github.com/users/Scottmitch/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/netty/netty/milestones/84","html_url":"https://github.com/netty/netty/milestones/4.1.0.Final","labels_url":"https://api.github.com/repos/netty/netty/milestones/84/labels","id":561880,"number":84,"title":"4.1.0.Final","description":null,"creator":{"login":"trustin","id":173918,"avatar_url":"https://avatars.githubusercontent.com/u/173918?v=3","gravatar_id":"","url":"https://api.github.com/users/trustin","html_url":"https://github.com/trustin","followers_url":"https://api.github.com/users/trustin/followers","following_url":"https://api.github.com/users/trustin/following{/other_user}","gists_url":"https://api.github.com/users/trustin/gists{/gist_id}","starred_url":"https://api.github.com/users/trustin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trustin/subscriptions","organizations_url":"https://api.github.com/users/trustin/orgs","repos_url":"https://api.github.com/users/trustin/repos","events_url":"https://api.github.com/users/trustin/events{/privacy}","received_events_url":"https://api.github.com/users/trustin/received_events","type":"User","site_admin":false},"open_issues":52,"closed_issues":20,"state":"open","created_at":"2014-02-07T08:42:42Z","updated_at":"2016-03-30T21:24:17Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/netty/netty/pulls/5001/commits","review_comments_url":"https://api.github.com/repos/netty/netty/pulls/5001/comments","review_comment_url":"https://api.github.com/repos/netty/netty/pulls/comments{/number}","comments_url":"https://api.github.com/repos/netty/netty/issues/5001/comments","statuses_url":"https://api.github.com/repos/netty/netty/statuses/d51aaf14760186811932b81a7badf4268dbc97af","head":{"label":"jongyeol:feature/4.1/codec_redis","ref":"feature/4.1/codec_redis","sha":"d51aaf14760186811932b81a7badf4268dbc97af","user":{"login":"jongyeol","id":23586,"avatar_url":"https://avatars.githubusercontent.com/u/23586?v=3","gravatar_id":"","url":"https://api.github.com/users/jongyeol","html_url":"https://github.com/jongyeol","followers_url":"https://api.github.com/users/jongyeol/followers","following_url":"https://api.github.com/users/jongyeol/following{/other_user}","gists_url":"https://api.github.com/users/jongyeol/gists{/gist_id}","starred_url":"https://api.github.com/users/jongyeol/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jongyeol/subscriptions","organizations_url":"https://api.github.com/users/jongyeol/orgs","repos_url":"https://api.github.com/users/jongyeol/repos","events_url":"https://api.github.com/users/jongyeol/events{/privacy}","received_events_url":"https://api.github.com/users/jongyeol/received_events","type":"User","site_admin":false},"repo":{"id":23009757,"name":"netty","full_name":"jongyeol/netty","owner":{"login":"jongyeol","id":23586,"avatar_url":"https://avatars.githubusercontent.com/u/23586?v=3","gravatar_id":"","url":"https://api.github.com/users/jongyeol","html_url":"https://github.com/jongyeol","followers_url":"https://api.github.com/users/jongyeol/followers","following_url":"https://api.github.com/users/jongyeol/following{/other_user}","gists_url":"https://api.github.com/users/jongyeol/gists{/gist_id}","starred_url":"https://api.github.com/users/jongyeol/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jongyeol/subscriptions","organizations_url":"https://api.github.com/users/jongyeol/orgs","repos_url":"https://api.github.com/users/jongyeol/repos","events_url":"https://api.github.com/users/jongyeol/events{/privacy}","received_events_url":"https://api.github.com/users/jongyeol/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jongyeol/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":true,"url":"https://api.github.com/repos/jongyeol/netty","forks_url":"https://api.github.com/repos/jongyeol/netty/forks","keys_url":"https://api.github.com/repos/jongyeol/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jongyeol/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jongyeol/netty/teams","hooks_url":"https://api.github.com/repos/jongyeol/netty/hooks","issue_events_url":"https://api.github.com/repos/jongyeol/netty/issues/events{/number}","events_url":"https://api.github.com/repos/jongyeol/netty/events","assignees_url":"https://api.github.com/repos/jongyeol/netty/assignees{/user}","branches_url":"https://api.github.com/repos/jongyeol/netty/branches{/branch}","tags_url":"https://api.github.com/repos/jongyeol/netty/tags","blobs_url":"https://api.github.com/repos/jongyeol/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jongyeol/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jongyeol/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/jongyeol/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jongyeol/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/jongyeol/netty/languages","stargazers_url":"https://api.github.com/repos/jongyeol/netty/stargazers","contributors_url":"https://api.github.com/repos/jongyeol/netty/contributors","subscribers_url":"https://api.github.com/repos/jongyeol/netty/subscribers","subscription_url":"https://api.github.com/repos/jongyeol/netty/subscription","commits_url":"https://api.github.com/repos/jongyeol/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/jongyeol/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/jongyeol/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/jongyeol/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/jongyeol/netty/contents/{+path}","compare_url":"https://api.github.com/repos/jongyeol/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jongyeol/netty/merges","archive_url":"https://api.github.com/repos/jongyeol/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jongyeol/netty/downloads","issues_url":"https://api.github.com/repos/jongyeol/netty/issues{/number}","pulls_url":"https://api.github.com/repos/jongyeol/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/jongyeol/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/jongyeol/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jongyeol/netty/labels{/name}","releases_url":"https://api.github.com/repos/jongyeol/netty/releases{/id}","deployments_url":"https://api.github.com/repos/jongyeol/netty/deployments","created_at":"2014-08-16T03:28:08Z","updated_at":"2016-02-25T12:45:55Z","pushed_at":"2016-03-30T18:06:26Z","git_url":"git://github.com/jongyeol/netty.git","ssh_url":"git@github.com:jongyeol/netty.git","clone_url":"https://github.com/jongyeol/netty.git","svn_url":"https://github.com/jongyeol/netty","homepage":"http://netty.io/","size":39471,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"4.1"}},"base":{"label":"netty:4.1","ref":"4.1","sha":"cee38ed2b6ba93a351047f718941c1c11880f1c7","user":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"repo":{"id":1064563,"name":"netty","full_name":"netty/netty","owner":{"login":"netty","id":473791,"avatar_url":"https://avatars.githubusercontent.com/u/473791?v=3","gravatar_id":"","url":"https://api.github.com/users/netty","html_url":"https://github.com/netty","followers_url":"https://api.github.com/users/netty/followers","following_url":"https://api.github.com/users/netty/following{/other_user}","gists_url":"https://api.github.com/users/netty/gists{/gist_id}","starred_url":"https://api.github.com/users/netty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/netty/subscriptions","organizations_url":"https://api.github.com/users/netty/orgs","repos_url":"https://api.github.com/users/netty/repos","events_url":"https://api.github.com/users/netty/events{/privacy}","received_events_url":"https://api.github.com/users/netty/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/netty/netty","description":"Netty project - an event-driven asynchronous network application framework","fork":false,"url":"https://api.github.com/repos/netty/netty","forks_url":"https://api.github.com/repos/netty/netty/forks","keys_url":"https://api.github.com/repos/netty/netty/keys{/key_id}","collaborators_url":"https://api.github.com/repos/netty/netty/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/netty/netty/teams","hooks_url":"https://api.github.com/repos/netty/netty/hooks","issue_events_url":"https://api.github.com/repos/netty/netty/issues/events{/number}","events_url":"https://api.github.com/repos/netty/netty/events","assignees_url":"https://api.github.com/repos/netty/netty/assignees{/user}","branches_url":"https://api.github.com/repos/netty/netty/branches{/branch}","tags_url":"https://api.github.com/repos/netty/netty/tags","blobs_url":"https://api.github.com/repos/netty/netty/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/netty/netty/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/netty/netty/git/refs{/sha}","trees_url":"https://api.github.com/repos/netty/netty/git/trees{/sha}","statuses_url":"https://api.github.com/repos/netty/netty/statuses/{sha}","languages_url":"https://api.github.com/repos/netty/netty/languages","stargazers_url":"https://api.github.com/repos/netty/netty/stargazers","contributors_url":"https://api.github.com/repos/netty/netty/contributors","subscribers_url":"https://api.github.com/repos/netty/netty/subscribers","subscription_url":"https://api.github.com/repos/netty/netty/subscription","commits_url":"https://api.github.com/repos/netty/netty/commits{/sha}","git_commits_url":"https://api.github.com/repos/netty/netty/git/commits{/sha}","comments_url":"https://api.github.com/repos/netty/netty/comments{/number}","issue_comment_url":"https://api.github.com/repos/netty/netty/issues/comments{/number}","contents_url":"https://api.github.com/repos/netty/netty/contents/{+path}","compare_url":"https://api.github.com/repos/netty/netty/compare/{base}...{head}","merges_url":"https://api.github.com/repos/netty/netty/merges","archive_url":"https://api.github.com/repos/netty/netty/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/netty/netty/downloads","issues_url":"https://api.github.com/repos/netty/netty/issues{/number}","pulls_url":"https://api.github.com/repos/netty/netty/pulls{/number}","milestones_url":"https://api.github.com/repos/netty/netty/milestones{/number}","notifications_url":"https://api.github.com/repos/netty/netty/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/netty/netty/labels{/name}","releases_url":"https://api.github.com/repos/netty/netty/releases{/id}","deployments_url":"https://api.github.com/repos/netty/netty/deployments","created_at":"2010-11-09T09:22:21Z","updated_at":"2016-03-31T09:37:44Z","pushed_at":"2016-03-31T07:26:49Z","git_url":"git://github.com/netty/netty.git","ssh_url":"git@github.com:netty/netty.git","clone_url":"https://github.com/netty/netty.git","svn_url":"https://github.com/netty/netty","homepage":"http://netty.io/","size":58160,"stargazers_count":6497,"watchers_count":6497,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3303,"mirror_url":null,"open_issues_count":322,"forks":3303,"open_issues":322,"watchers":6497,"default_branch":"4.1"}},"_links":{"self":{"href":"https://api.github.com/repos/netty/netty/pulls/5001"},"html":{"href":"https://github.com/netty/netty/pull/5001"},"issue":{"href":"https://api.github.com/repos/netty/netty/issues/5001"},"comments":{"href":"https://api.github.com/repos/netty/netty/issues/5001/comments"},"review_comments":{"href":"https://api.github.com/repos/netty/netty/pulls/5001/comments"},"review_comment":{"href":"https://api.github.com/repos/netty/netty/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/netty/netty/pulls/5001/commits"},"statuses":{"href":"https://api.github.com/repos/netty/netty/statuses/d51aaf14760186811932b81a7badf4268dbc97af"}}}}