(window.webpackJsonp=window.webpackJsonp||[]).push([[1530],{1938:function(t,a,e){"use strict";e.r(a);var s=e(31),n=Object(s.a)({},(function(){var t=this,a=t.$createElement,e=t._self._c||a;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"bytebuffer"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#bytebuffer"}},[t._v("#")]),t._v(" ByteBuffer")]),t._v(" "),e("p",[t._v("The "),e("code",[t._v("ByteBuffer")]),t._v(" class was introduced in java 1.4 to ease working on binary data. It's especially suited to use with primitive type data. It allows the creation, but also subsequent manipulation of a "),e("code",[t._v("byte[]")]),t._v("s on a higher abstraction level")]),t._v(" "),e("h2",{attrs:{id:"basic-usage-creating-a-bytebuffer"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#basic-usage-creating-a-bytebuffer"}},[t._v("#")]),t._v(" Basic Usage - Creating a ByteBuffer")]),t._v(" "),e("p",[t._v("There's two ways to create a "),e("code",[t._v("ByteBuffer")]),t._v(", where one can be subdivided again.")]),t._v(" "),e("p",[t._v("If you have an already existing "),e("code",[t._v("byte[]")]),t._v(", you can "),e("strong",[t._v('"wrap"')]),t._v(" it into a "),e("code",[t._v("ByteBuffer")]),t._v(" to simplify processing:")]),t._v(" "),e("div",{staticClass:"language-java extra-class"},[e("pre",{pre:!0,attrs:{class:"language-java"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("byte")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" reqBuffer "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("new")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("byte")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("BUFFER_SIZE"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),t._v(" readBytes "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" socketInputStream"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("read")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("reqBuffer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("final")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("ByteBuffer")]),t._v(" reqBufferWrapper "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("ByteBuffer")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("wrap")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("reqBuffer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n")])])]),e("p",[t._v("This would be a possibility for code that handles low-level networking interactions")]),t._v(" "),e("p",[t._v("If you do not have an already existing "),e("code",[t._v("byte[]")]),t._v(", you can create a "),e("code",[t._v("ByteBuffer")]),t._v(" over an array that's specifically allocated for the buffer like this:")]),t._v(" "),e("div",{staticClass:"language-java extra-class"},[e("pre",{pre:!0,attrs:{class:"language-java"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("final")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("ByteBuffer")]),t._v(" respBuffer "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("ByteBuffer")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("allocate")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("RESPONSE_BUFFER_SIZE"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("putResponseData")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("respBuffer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\nsocketOutputStream"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("write")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("respBuffer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("array")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n")])])]),e("p",[t._v("If the code-path is extremely performance critical and you need "),e("strong",[t._v("direct system memory access")]),t._v(", the "),e("code",[t._v("ByteBuffer")]),t._v(" can even allocate "),e("strong",[t._v("direct")]),t._v(" buffers using "),e("code",[t._v("#allocateDirect()")])]),t._v(" "),e("h2",{attrs:{id:"basic-usage-using-directbytebuffer"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#basic-usage-using-directbytebuffer"}},[t._v("#")]),t._v(" Basic Usage - Using DirectByteBuffer")]),t._v(" "),e("p",[e("code",[t._v("DirectByteBuffer")]),t._v(" is special implementation of "),e("code",[t._v("ByteBuffer")]),t._v(" that has no "),e("code",[t._v("byte[]")]),t._v(" laying underneath.")]),t._v(" "),e("p",[t._v("We can allocate such ByteBuffer by calling:")]),t._v(" "),e("div",{staticClass:"language-java extra-class"},[e("pre",{pre:!0,attrs:{class:"language-java"}},[e("code",[e("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("ByteBuffer")]),t._v(" directBuffer "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("ByteBuffer")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("allocateDirect")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("16")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n")])])]),e("p",[t._v("This operation will allocate 16 bytes of memory. The contents of direct buffers "),e("strong",[t._v("may")]),t._v(" reside outside of the normal garbage-collected heap.")]),t._v(" "),e("p",[t._v("We can verify whether ByteBuffer is direct by calling:")]),t._v(" "),e("div",{staticClass:"language-java extra-class"},[e("pre",{pre:!0,attrs:{class:"language-java"}},[e("code",[t._v("directBuffer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("isDirect")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// true")]),t._v("\n\n")])])]),e("p",[t._v("The main characteristics of "),e("code",[t._v("DirectByteBuffer")]),t._v(" is that JVM will try to natively work on allocated memory without any additional buffering so operations performed on it may be faster then those performed on ByteBuffers with arrays lying underneath.")]),t._v(" "),e("p",[t._v("It is recomended to use "),e("code",[t._v("DirectByteBuffer")]),t._v(" with heavy IO operations that rely on speed of execution, like real time communication.")]),t._v(" "),e("p",[t._v("We have to be aware that if we try using "),e("code",[t._v("array()")]),t._v(" method we will get "),e("code",[t._v("UnsupportedOperationException")]),t._v(". So it is a good practice to chech whether our ByteBuffer has it (byte array) before we try to access it:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("\nbyte[] arrayOfBytes;\n if(buffer.hasArray()) {\n arrayOfBytes = buffer.array();\n }\n\n")])])]),e("p",[t._v("Another use of direct byte buffer is interop through JNI. Since a direct byte buffer does not use a "),e("code",[t._v("byte[]")]),t._v(", but an actual block of memory, it is possible to access that memory directly through a pointer in native code. This can save a bit of trouble and overhead on marshalling between the Java and native representation of data.")]),t._v(" "),e("p",[t._v("The JNI interface defines several functions to handle direct byte buffers: "),e("a",{attrs:{href:"http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#nio_support",target:"_blank",rel:"noopener noreferrer"}},[t._v("NIO Support"),e("OutboundLink")],1),t._v(".")]),t._v(" "),e("h2",{attrs:{id:"basic-usage-write-data-to-the-buffer"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#basic-usage-write-data-to-the-buffer"}},[t._v("#")]),t._v(" Basic Usage - Write Data to the Buffer")]),t._v(" "),e("p",[t._v("Given a "),e("code",[t._v("ByteBuffer")]),t._v(" instance one can write primitive-type data to it using "),e("strong",[t._v("relative")]),t._v(" and "),e("strong",[t._v("absolute")]),t._v(" "),e("code",[t._v("put")]),t._v(". The striking difference is that putting data using the "),e("strong",[t._v("relative")]),t._v(" method keeps track of the index the data is inserted at for you, while the absolute method always requires giving an index to "),e("code",[t._v("put")]),t._v(" the data at.")]),t._v(" "),e("p",[t._v("Both methods allow "),e("strong",[t._v('"chaining"')]),t._v(" calls. Given a sufficiently sized buffer one can accordingly do the following:")]),t._v(" "),e("div",{staticClass:"language-java extra-class"},[e("pre",{pre:!0,attrs:{class:"language-java"}},[e("code",[t._v("buffer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("putInt")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0xCAFEBABE")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("putChar")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'c'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("putFloat")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0.25")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("putLong")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0xDEADBEEFCAFEBABE")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n")])])]),e("p",[t._v("which is equivalent to:")]),t._v(" "),e("div",{staticClass:"language-java extra-class"},[e("pre",{pre:!0,attrs:{class:"language-java"}},[e("code",[t._v("buffer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("putInt")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0xCAFEBABE")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\nbuffer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("putChar")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v("'c'")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\nbuffer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("putFloat")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0.25")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\nbuffer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("putLong")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0xDEADBEEFCAFEBABE")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n")])])]),e("p",[t._v("Do note that the method operating on "),e("code",[t._v("byte")]),t._v("s is not named specially. Additionally note that it's also valid to pass both a "),e("code",[t._v("ByteBuffer")]),t._v(" and a "),e("code",[t._v("byte[]")]),t._v(" to "),e("code",[t._v("put")]),t._v(". Other than that, all primitive types have specialized "),e("code",[t._v("put")]),t._v("-methods.")]),t._v(" "),e("p",[t._v("An additional note: The index given when using absolute "),e("code",[t._v("put*")]),t._v(" is always counted in "),e("code",[t._v("byte")]),t._v("s.")]),t._v(" "),e("h4",{attrs:{id:"syntax"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#syntax"}},[t._v("#")]),t._v(" Syntax")]),t._v(" "),e("ul",[e("li",[t._v("byte[] arr = new byte[1000];")]),t._v(" "),e("li",[t._v("ByteBuffer buffer = ByteBuffer.wrap(arr);")]),t._v(" "),e("li",[t._v("ByteBuffer buffer = ByteBuffer.allocate(1024);")]),t._v(" "),e("li",[t._v("ByteBuffer buffer = ByteBuffer.allocateDirect(1024);")]),t._v(" "),e("li",[t._v("byte b = buffer.get();")]),t._v(" "),e("li",[t._v("byte b = buffer.get(10);")]),t._v(" "),e("li",[t._v("short s = buffer.getShort(10);")]),t._v(" "),e("li",[t._v("buffer.put((byte) 120);")]),t._v(" "),e("li",[t._v("buffer.putChar('a');")])])])}),[],!1,null,null,null);a.default=n.exports}}]);