Java?????????????????
???????????? ???????[ 2016/3/7 10:18:17 ] ??????????????? ???????????
?????????????????????????棨??????棩????????????????????????
??????????-->??д????-->???????-->??д???-->???-->?ο????
?????????????????????????????????ο???
????????????
??????????Щ???????????DirectBuffer?б?????Щ??????????????????????????????????????ο?????Щ?????
???????????????
??????Java???к??????????????磺
????byte??????????8λbit?????1B
????short??16λbit?????2B
????int??32λbit?????4B
????long?? 64λbit?????8B
????char??16λbit?????2B
????float??32λbit?????4B
????double??64λbit?????8B
???????????????????????λ?????洢?????????????????????????
????byte??char??short??????????????int?????????????long????????????long??float??double???????
???????С??
???????????????????????к?????????????????????????????????н??????
????????????λ ??? ????Ч???
????С???????λ ??? ????Ч???
???????????????char?????????????????????????洢?????????????μ?????????????a:
????????λ ????λ
???????? 00 96
????С??? 96 00
????String??new String??????
????????"hello"??new String("hello")??????
?????????"hello"??JVM??????????????????в?????????"hello"???????????????????????????????У??????????????????????"a"+"b"???????3???????????"a"??"b"??"ab"??
??????new String("hello")?????????????????????????hello???????????????á?
??????д????
?????????????У????allocateDirect(int byte_length)?????????檔????????????????????????Byte?????飬?????????????????????????
?????????????????????????????????????磺
????put(byte) ???????byte
????put(byte[]) ???????byte????
????putChar(char) ???????
????putInt(int) ????Int
????putLong(long) ????long
???????….???????÷??????????ο??????????
?????????????????д?????
??????????????index????????????????????position??λ?y??в??????
???????濴??????position????????????????????
???????????????
???????м???????????
????mark-->position-->limit-->capacity
????????????remaining=limit-position??
???????????????????
???????λ??——position
????position?????????????????????λ?á?????????
????ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
????buffer.putChar('a');
????System.out.println(buffer);
????buffer.putChar('c');
????System.out.println(buffer);
????buffer.putInt(10);
????System.out.println(buffer);
???????????char??2?????????Int??4?????????position??λ?÷????:
????2??4??8
???????Position??λ??????????????λ?????????????????????????
??????????????洢?????????????????position??λ??????????????????±???2??
????java.nio.DirectByteBuffer[pos=2 lim=1024 cap=1024]
????java.nio.DirectByteBuffer[pos=4 lim=1024 cap=1024]
????java.nio.DirectByteBuffer[pos=8 lim=1024 cap=1024]
????position???????position()????????????position(int)???á?
????//position(int)?????????
????public final Buffer position(int newPosition) {
????if ((newPosition > limit) || (newPosition < 0))
????throw new IllegalArgumentException();
????position = newPosition;
????if (mark > position) mark = -1;
????return this;
????}
???????position??λ?????limitС????mark??
???????????——capacity
????capacity?????????????????????????????????????
????capacity????????capacity()??????á?
?????????С——limit
????????????????????????????С??????????????????Limit?????????á?
????limit????????limit()???????limit(int)???????á?
???????limit???mark??position????capacityС??
????//limit(int)?????????
????public final Buffer limit(int newLimit) {
????if ((newLimit > capacity) || (newLimit < 0))
????throw new IllegalArgumentException();
????limit = newLimit;
????if (position > limit) position = limit;
????if (mark > limit) mark = -1;
????return this;
????}
???????λ??——mark
????mark????????????????????????position???????????????????????β????????λ???????????ν??л????
???????????mark()???????б???
???????reset()?????????????
???????rewind()???????г????
????//mark???????????position??????-1
????public final Buffer mark() {
????mark = position;
????return this;
????}
????//reset????????mark??λ???position??λ???????С??mark??λ???????????
????public final Buffer reset() {
????int m = mark;
????if (m < 0)
????throw new InvalidMarkException();
????position = m;
????return this;
????}
????//????mark?-1.position?0
????public final Buffer rewind() {
????position = 0;
????mark = -1;
????return this;
????}
??????e???
????ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
????buffer.putChar('a');
????buffer.putChar('c');
????System.out.println("?????????? " + buffer);
????buffer.mark();// ???mark??λ??
????buffer.position(30);// ?????position??????mark??????mark???????
????System.out.println("reset? " + buffer);
????buffer.reset();// ????reset ??reset???position=mark
????System.out.println("reset?? " + buffer);
????buffer.rewind();//???????position???0??mark???-1
????System.out.println("??????? " + buffer);
??????????????μ????н????
?????????????? java.nio.DirectByteBuffer[pos=4 lim=1024 cap=1024]
????reset? java.nio.DirectByteBuffer[pos=30 lim=1024 cap=1024]
????reset?? java.nio.DirectByteBuffer[pos=4 lim=1024 cap=1024]
??????????? java.nio.DirectByteBuffer[pos=0 lim=1024 cap=1024]
?????????——remaing
????remaing??????????????
????public final int remaining() {
????return limit - position;
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11