java - ByteBuffer.wrap came out unexpectedly -
i used bytearray create new string, results came out unexpectedly. here code
void foo(byte[] data, ...) { bytebuffer bytebuf = bytebuffer.wrap(data, 0, 15); string msg = new string(bytebuf.array()); log.i("foo", string.format("bytearraysize=%d\t\tmsglen=%d,%d", data.length, bytebuf.array().length, msg.length())); }
the length of byte array 518400. log info shows:
bytearraysize=518400 msglen=518400,518400
rather than
bytearraysize=518400 msglen=15,15
what wrong?
that expected result.
according javadoc bytebuffer.wrap()
:
wraps byte array buffer.
the new buffer backed given byte array; is, modifications buffer cause array modified , vice versa.
and bytebuffer.array()
:
returns byte array backs buffer (optional operation).
modifications buffer's content cause returned array's content modified, , vice versa.
that means bytebuffer.array()
return same array wrapped bytebuffer.wrap()
.
Comments
Post a Comment