java - IndexOutOfBoundsException when use MappedByteBuffer -
i thinking of using mappedbytebuffer store/load data file. let's assume have field of type long , field b of string looks below when serialized: a(long) | b(string)
now want write , read it. here piece of sample code:
randomaccessfile file = new randomaccessfile(datapath.tostring(), "rw"); mappedbytebuffer mbb = file.getchannel().map(filechannel.mapmode .read_write, 0, 256); long num = 500; mbb.putlong(0, num); // (1) first write long value @ beginning string str = "hello world!"; byte[] input = str.getbytes(); //then write string mbb.put(input, 8, input.length); // (2) indexoutofboundsexception
so later can retrieve long calling mbb.getlong(0)
, mbb.get(outputarray,8,outputarray.length)
but failing @ place (2). suggestions?
try
mbb.put(destarray, 0, sourcearray.length)
i don't think want start writing @ 8 byte offset, otherwise you'd trying write 8 bytes on lenght of array.
Comments
Post a Comment