java - How can I increase performance on reading the InputStream? -
this may kiss moment, feel should ask anyway.
i have thread , it's reading sockets inputstream. since dealing in particularly small data sizes (as in data can expect recieve in order of 100 - 200 bytes), set buffer array size 256. part of read function have check ensure when read inputstream got of data. if didn't recursively call read function again. each recursive call merge 2 buffer arrays together.
my problem is, while never anticipate using more buffer of 256, want safe. if sheep begin fly , buffer more read function (by estimation) begin take exponential curve more time complete.
how can increase effiency of read function and/or buffer merging?
here read function stands.
int buffer_amount = 256; private int read(byte[] buffer) throws ioexception { int bytes = minstream.read(buffer); // read input stream if (bytes == -1) { // if bytes == -1 didn't of data byte[] newbuffer = new byte[buffer_amount]; // try rest int newbytes; newbytes = read(newbuffer); // recurse until have data byte[] oldbuffer = new byte[bytes + newbytes]; // make final array size // merge buffer begining of old buffer. // once method finishes, can add // modified buffer queue later in class processing. (int = 0; < bytes; i++) oldbuffer[i] = buffer[i]; (int = bytes; < bytes + newbytes; i++) // merge newbuffer latter half of old buffer oldbuffer[i] = newbuffer[i]; // used recursion buffer = oldbuffer; // , set buffer new buffer full of data. return bytes + newbytes; } return bytes; }
edit: being paranoid (unjustifiedly) , should set buffer 2048 , call done?
bufferedinputstream, noted roland, , datainputstream.readfully(), replaces looping code.
Comments
Post a Comment