Java NIO/ MappedByteBuffer and map, read part of file in chunks -



Java NIO/ MappedByteBuffer and map, read part of file in chunks -

i want read parts of big file in loop. had read entire file didn't work, getting exception file large. changed code listing below. code below reads in first chunk. need alter move next chunk.

final fileinputstream fis = new fileinputstream(f); final filechannel fc = fis.getchannel(); final long sizeread = fc.size() < defaultreadbuffersize ? fc.size() : defaultreadbuffersize; final mappedbytebuffer bb = fc.map(filechannel.mapmode.read_only, 0, sizeread); while (bb.hasremaining()) { final charbuffer cb = decoder.decode(bb); this.search(f, cb); system.out.println("============>" + cb.length()); system.out.println("============>" + bb.hasremaining()); } fc.close();

the problem have character encoded info cannot accessed way. i.e. need know boundaries between characters are.

the cost of accessing file , character decoding far more expensive how read it, utilize bufferedreader much simpler well.

e.g. want read 1000th byte. can won't know if 1000th byte part of multi-byte character or not.

if can assume characters bytes, whole issue much simpler , don't need charbuffer, can access bytebuffer straight much faster.

java nio bytebuffer

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -