java - How can we enhance performance using hooking? -


the java docs says java docs

public class bufferedreader extends reader

reads text character-input stream, buffering characters provide >efficient reading of characters, arrays, , lines.

the buffer size may specified, or default size may used. default large >enough purposes.

in general, each read request made of reader causes corresponding read request >made of underlying character or byte stream. therefore advisable wrap >bufferedreader around reader read() operations may costly, such >filereaders , inputstreamreaders. example,

bufferedreader in = new bufferedreader(new filereader("foo.in"));

will buffer input specified file. without buffering, each invocation of >read() or readline() cause bytes read file, converted >characters, , returned, can inefficient.

but hooking filereader using filereader's read method read file reads 1 character @ time,so if file contains 2000 characters filereader firstly read 2000 characters 1 @ time , transfer buffer , read buffer using bufferedreader how enhances performance.we using filereader only?

you imagin this:

you in location a , data @ location b a , b 5km away each other.

by retieving data 1 unit @ time have make travel (read request made of underlying character or byte stream) a b each time, if have 100 data units going travel 100 times, , waht costs time.

wehen buffering having truck , when travel load amount of data units (the size of buffer) onto load floor (the buffer) don't have make travel lot of times , efficient.

usually open file reading want read more 1 charachter, therefore bufferedread prefered.

the same applies writing too.

edit

first off, filereader not read itself, it's underlying input stream does. in case fileinputstream.

the bufferedreader manages filereasder you, , uses in fill() method method

abstract public int read(char cbuf[], int off, int len) throws ioexception; 

of reader fill buffuer. it not use method reads single character! in other word, when fill method has travel, retrieves range of charcters underlying input stream , keeps in memory, when need read, first looks in buffer, consumes , re-fills if necessary.

you achieve same calling read(char cbuf[], int off, int len) of filereader yourself, have range of charcters having example following text

lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 

may example this

"onsetetur sadipscing elitr,\r\nsed diam nonumy eirmod temp" 

and have deal find line of text if need one.

but dont have because bufferedreader takes care of you, , 1 of reasons why better write

bufferedreader in = new bufferedreader(new filereader("foo.in")); 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -