{"id":225558,"date":"2020-12-07T00:21:11","date_gmt":"2020-12-06T16:21:11","guid":{"rendered":"http:\/\/4563.org\/?p=225558"},"modified":"2020-12-07T00:21:11","modified_gmt":"2020-12-06T16:21:11","slug":"%e5%85%b3%e4%ba%8e-nio-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=225558","title":{"rendered":"\u5173\u4e8e NIO \u7f51\u7edc\u7f16\u7a0b\u7684\u4e00\u4e2a\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e NIO \u7f51\u7edc\u7f16\u7a0b\u7684\u4e00\u4e2a\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Joker123456789 <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5148\u4e0a\u4ee3\u7801<\/p>\n<pre><code>ByteBuffer readBuffer = ByteBuffer.allocate(requestSize);\/\/ \u8fd9\u91cc\u662f 1024*1024*1024 = 1G readBuffer.clear();  SocketChannel socketChannel = (SocketChannel) selectionKey.channel();  while (socketChannel.read(readBuffer) &gt; 0) {}  \/* \u83b7\u53d6\u8bf7\u6c42\u62a5\u6587 *\/ readBuffer.flip(); byte[] bytes = new byte[readBuffer.limit()]; readBuffer.get(bytes); <\/code><\/pre>\n<p>\u5982\u4e0a\u9762\u4ee3\u7801\u6240\u793a\uff0c\u7f13\u51b2\u533a ByteBuffer \u7684\u5bb9\u91cf\u8bbe\u7f6e\u6210\u4e86 1G\uff0c\u4f46\u662f\u4e0a\u4f20\u4e00\u4e2a 30M \u5de6\u53f3\u7684\u6587\u4ef6\uff0c\u7ecf\u5e38\u4f1a\u4e22\u5305\uff0c\u5bfc\u81f4\u6587\u4ef6\u4e0d\u5168\uff0c\u7136\u540e formData \u89e3\u6790\u65f6\u62a5\u9519\u3002<\/p>\n<p>\u8fd9\u4e2a\u7f13\u51b2\u533a\u7684\u5bb9\u91cf\u5982\u679c\u6bd4\u6587\u4ef6\u5927\u5f88\u591a\uff0c\u5219\u4e00\u70b9\u95ee\u9898\u90fd\u6ca1\uff0c\u4f46\u662f\u6211\u53c8\u4e0d\u80fd\u628a\u7f13\u51b2\u533a\u8bbe\u7f6e\u7684\u592a\u5927\uff0c\u5426\u5219\u4f1a\u5806\u6ea2\u51fa\u3002<\/p>\n<p>\u9ebb\u70e6\u719f\u6089 NIO \u7684 \u5927\u795e\u4eec\u6307\u70b9\u4e00\u4e0b<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4562801\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BBCCBB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a read(buf)\u65b9\u6cd5\u8fd4\u56de 0 \u4e5f\u662f\u6b63\u5e38\u7684\u73b0\u8c61, \u5404\u79cd\u60c5\u51b5\u4f1a\u8fd4\u56de 0, \u4f46\u662f\u662f\u6b63\u5e38\u73b0\u8c61, \u6240\u4ee5\u8fd9\u91cc\u8fd9\u4e2a while \u5e94\u8be5\u4e0d\u5bf9<\/p>\n<p>\u8981\u81ea\u5df1\u5b9a\u4e00\u4e2a\u534f\u8bae, \u6570\u636e\u7f13\u5b58\u8d77\u6765, \u53ea\u8981 read \u4e0d\u662f\u8fd4\u56de-1, \u5c31\u7ee7\u7eed\u5f80 buf \u91cc\u5199\u6570\u636e, \u76f4\u5230\u8fbe\u5230\u4f60\u5b9a\u4e49\u7684\u534f\u8bae\u7684\u5305\u7684\u7ed3\u675f\u70b9..<\/p>\n<p>\u6bd4\u5982\u5b9a\u957f\u7684\u534f\u8bae. |&#8212; length&#8211; | your data |, eg: \u5f00\u5934 4 \u4e2a\u5b57\u8282\u4ee3\u8868\u6570\u636e\u5305\u957f\u5ea6, \u8bfb\u5230\u8fd9\u4e48\u957f\u7684\u6570\u636e\u624d\u7b97\u5b8c\u6210\u8bfb\u53d6.<\/p>\n<p>\u53ef\u4ee5\u76f4\u63a5\u7528 netty, \u770b\u770b\u91cc\u9762\u7684 LengthFieldBasedFrameDecoder \u5b9e\u73b0, \u76f4\u63a5\u7528 netty \u5427. \u770b\u61c2\u91cc\u9762\u7684\u5b9e\u73b0\u5c31\u884c.                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562802\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BBCCBB <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u767e\u5ea6\u7684\u8fd4\u56de 0 \u7684\u60c5\u51b5:<\/p>\n<p>\u5176\u5b9e read \u8fd4\u56de 0 \u6709 3 \u79cd\u60c5\u51b5\uff0c\u4e00\u662f\u67d0\u4e00\u65f6\u523b socketChannel \u4e2d\u5f53\u524d\uff08\u6ce8\u610f\u662f\u5f53\u524d\uff09\u6ca1\u6709\u6570\u636e\u53ef\u4ee5\u8bfb\uff0c\u8fd9\u65f6\u4f1a\u8fd4\u56de 0\uff0c\u5176\u6b21\u662f bytebuffer \u7684 position \u7b49\u4e8e limit \u4e86\uff0c\u5373 bytebuffer \u7684 remaining \u7b49\u4e8e 0\uff0c\u8fd9\u4e2a\u65f6\u5019\u4e5f\u4f1a\u8fd4\u56de 0\uff0c\u6700\u540e\u4e00\u79cd\u60c5\u51b5\u5c31\u662f\u5ba2\u6237\u7aef\u7684\u6570\u636e\u53d1\u9001\u5b8c\u6bd5\u4e86\uff08\u6ce8\u610f\u770b\u540e\u9762\u7684\u7a0b\u5e8f\u91cc\u6709\u8fd9\u6837\u5b50\u7684\u4ee3\u7801\uff09\uff0c\u8fd9\u4e2a\u65f6\u5019\u5ba2\u6237\u7aef\u60f3\u83b7\u53d6\u670d\u52a1\u7aef\u7684\u53cd\u9988\u8c03\u7528\u4e86 recv \u51fd\u6570\uff0c\u82e5\u670d\u52a1\u7aef\u7ee7\u7eed read\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u4f1a\u8fd4\u56de 0 \u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562803\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Joker123456789 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @BBCCBB \u8c22\u8c22\uff0c\u6211\u5c1d\u8bd5\u4e86\u4e00\u4e0b\u6539\u6210 -1\uff0c\u4f46\u662f \u8fd9\u6837\u4e00\u6765\u5c31\u4e00\u76f4\u6b7b\u5faa\u73af\u4e86\uff0c\u53ea\u6709\u5f3a\u884c\u53d6\u6d88\u672c\u6b21\u8bf7\u6c42 \u624d\u4f1a\u53d8\u6210-1\uff0c\u5426\u5219\u5230 0 \u5c31\u4e0b\u4e0d\u53bb\u4e86\uff0c\u6709\u70b9\u5934\u75bc\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562804\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sagaxu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             socket \u662f\u4e2a stream\uff0c\u4f60\u6709\u4e24\u4e2a\u9009\u62e9\uff0c<br \/>1. \u77ed\u8fde\u63a5\uff0c\u6bcf\u6b21\u5199\u5165\u5b8c close\uff0c\u8bfb\u7684\u8bfb\u5230-1 \u7ed3\u675f<br \/>2. \u957f\u8fde\u63a5\uff0c\u81ea\u5b9a\u4e49\u534f\u8bae\u786e\u5b9a\u8fb9\u754c\uff0c\u652f\u6301\u590d\u7528<\/p>\n<p>\u5faa\u73af\u8bfb\uff0c-1 \u5c31\u7ed3\u675f\uff0c0 \u7ee7\u7eed\u7b49\u5f85\u53ef\u8bfb\u901a\u77e5\uff0c\u9047\u5230\u8fb9\u754c\u5c31\u89e3\u51fa\u4e00\u4e2a\u5b8c\u6574\u62a5\u6587\u653e\u5165\u5904\u7406\u961f\u5217                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562805\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Joker123456789 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sagaxu <\/p>\n<p>\u6211\u53ea\u80fd\u7528\u7b2c\u4e8c\u79cd\u65b9\u6848\u4e86\uff0c\u56e0\u4e3a\u6211\u662f\u57fa\u4e8e NIO \u5728\u5f00\u53d1 http \u670d\u52a1\uff0c\u6240\u4ee5\u9700\u8981\u7b49\u5f85\u54cd\u5e94\uff0c\u5ba2\u6237\u7aef\u4e0d\u80fd\u5173\u95ed\u7684<\/p>\n<p>\u4f46\u662f\u7b2c\u4e8c\u79cd\u65b9\u6848\u6709\u4e2a\u95ee\u9898\uff0c\u4e0d\u77e5\u9053\u8fb9\u754c\u5982\u4f55\u786e\u5b9a\uff0c\u56e0\u4e3a\u5ba2\u6237\u7aef\u4f20\u6765\u7684\u6570\u636e\u662f\u53d8\u5316\u591a\u7aef\u7684\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562806\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : laminux29 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1.Buffer \u53ea\u662f\u4e2a\u4e34\u65f6\u7f13\u5b58\uff0crequestSize \u6ca1\u5fc5\u8981\u7b49\u4e8e file size\uff0c\u4e0d\u7136\u767e\u5ea6\u7f51\u76d8\u7684 SVIP \u4ef7\u683c\u5f97\u7ffb\u51e0\u500d\u3002\u4f60\u4e0b\u8f7d\u4e00\u4e2a\u8fc5\u96f7\uff0c\u4ee5\u53ca\u5176\u4ed6 BT \u8f6f\u4ef6\uff0c\u770b\u770b\u91cc\u9762\u7684\u786c\u76d8\u7f13\u5b58\u6700\u5927\u503c\u662f\u591a\u5c11\u3002<\/p>\n<p>\u4e00\u822c\u6765\u8bf4 requestSize \u7684 sizes \u662f 4k \u7684\u500d\u6570\uff0c\u8001\u65e7\u670d\u52a1\u7aef\u4e00\u822c\u662f 4\/8\/16 KB\uff0c\u518d\u6709\u94b1\u7684 bat \u4e5f\u6700\u591a 1\/2MB \u3002<\/p>\n<p>2.\u5efa\u8bae debug\uff0c\u770b\u770b ByteBuffer.allocate \u4e4b\u540e\u91cc\u9762\u5230\u5e95\u662f\u4ec0\u4e48\uff0c\u6709\u6ca1\u6709\u5fc5\u8981\u518d\u505a clear()\u3002<\/p>\n<p>3.\u91cc\u9762\u5f88\u591a\u65b9\u6cd5\u90fd\u4f1a\u629b\u5f02\u5e38\uff0c\u5efa\u8bae\u9605\u8bfb\u6587\u6863\uff0c\u8be5\u5904\u7406\u7684\u4e00\u5b9a\u8981\u5904\u7406\u3002<\/p>\n<p>4.while (socketChannel.read(readBuffer) &gt; 0) {} \uff0c\u8fd9\u4e5f\u662f\u6ca1\u8bfb\u6587\u6863\u9020\u6210\u7684\u3002\u5148\u4e0d\u8bf4\u5f02\u5e38\u5904\u7406\uff0cread \u65b9\u6cd5\u6709\u51e0\u79cd\u8fd4\u56de\u503c\uff0c\u8ba4\u771f\u770b\u4e00\u4e0b\u6587\u6863\u3002<\/p>\n<p>5.\u91cd\u65b0\u6539\u6b63\u540e\uff0c\u5148\u627e\u4e2a 100 \u5b57\u8282\u6587\u6863\u6d4b\u8bd5\u4e00\u4e0b\uff0cjava \u7aef\u6536\u5230\u540e\u7acb\u9a6c write file\uff0c\u7136\u540e\u4e0e source file \u5bf9\u6bd4\u4e00\u4e0b\u5185\u5bb9\u3002\u6ca1\u95ee\u9898\u540e\u518d\u6d4b\u8bd5 100KB file \u3001100MB file \u751a\u81f3\u51e0\u5341 GB \u7684 file \u3002\u5148 md5 \u5bf9\u6bd4\uff0c\u4e0d\u4e00\u6837\u540e\u518d\u627e\u4e00\u6b3e\u57fa\u4e8e byte \u5bf9\u6bd4\u7684\u8f6f\u4ef6\uff0c\u6bd4\u5982 BeyondCompare \u3001TextDiff \u751a\u81f3 Ultra Edit \u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562807\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zacharyjia <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Joker123456789 Http \u7684\u8bdd\uff0cheader \u91cc\u9762\u5e94\u8be5\u6709\u5f53\u524d\u8bf7\u6c42\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u6839\u636e\u5b83\u5224\u65ad\u8fb9\u754c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562808\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cheng6563 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             http \u4e00\u662f\u770b content-length \u5224\u65ad\u957f\u5ea6\uff0c<br \/>\u5982\u679c\u6ca1\u6709 content-length \u5219\u662f\u4e00\u884c\u7a7a\u884c\u8868\u793a\u7ed3\u675f\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562809\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chenshun00 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             NIO \u5f00\u53d1 http\uff0c\u4f60\u8fd9\u6837\u89e3\u6790 http \u7684\u5440\uff0c\u53ef\u4ee5\u5148\u94fe\u63a5\u4e00\u4e0b netty \u7684 http \u534f\u8bae\u662f\u600e\u4e48\u505a\u7684\u3002 \u8fd9\u6837\u7684\u8bfb\u53d6\u80af\u5b9a\u662f\u4e0d\u884c\u7684.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562810\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Joker123456789 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zacharyjia <br \/>@cheng6563 <br \/>@laminux29 <br \/>@BBCCBB <\/p>\n<p>\u8c22\u8c22\u5927\u4f6c\u4eec\uff0c\u5df2\u7ecf\u89e3\u51b3\u4e86\uff0c\u901a\u8fc7\u67e5\u770b\u8bfb\u5230\u7684\u5185\u5bb9\u4e2d\u6709\u6ca1\u6709 rnrn \u6765\u5224\u65ad\u5934\u8bfb\u5b8c\u4e86\u6ca1\uff0c\u5982\u679c\u8bfb\u5b8c\u4e86\u5219\u83b7\u53d6\u5230 Content-Length\uff0c \u7136\u540e\u7ee7\u7eed\u8bfb body\uff0c\u518d\u5224\u65ad\u5df2\u7ecf\u8bfb\u5230\u7684 body \u7684\u957f\u5ea6\u662f\u5426&gt;= Content-Length \u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562811\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lei2j <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3a\u5565\u4e0d\u7528 netty \u5462                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562812\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Joker123456789 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lei2j \u4e0d\u60f3\u4f9d\u8d56\u7b2c\u4e09\u65b9\uff0cnetty \u5f88\u597d\u5f88\u5f3a\u5927\uff0c\u4f46\u662f\u91cc\u9762\u6211\u7528\u4e0d\u5230\u7684\u4e1c\u897f\u4e5f\u6709\u5f88\u591a\uff0c\u4e0d\u591f\u5c0f\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4562813\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shentar <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Joker123456789 \u770b\u8d77\u6765\u9700\u8981\u7528 select \u554a\uff0c\u76d1\u542c\u8bfb\u5199\u4e8b\u4ef6\u3002\u8fd8\u662f\u8981\u8bfb\u4e00\u4e0b netty\uff0cjetty \u7684\u7f51\u7edc\u5904\u7406\u90e8\u5206\uff1a https:\/\/codefine.site\/652.html                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e NIO \u7f51\u7edc\u7f16\u7a0b\u7684\u4e00\u4e2a\u95ee\u9898 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/225558"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=225558"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/225558\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=225558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=225558"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=225558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}