9,14 → 9,10 |
private InputStream in; |
private String charsetName = "UTF-8"; |
|
// following members and the method toNextBuffer() are public |
// but they should be accessed directly only is speed critical applications; |
// common way is to use cur() and toNext() methods |
public byte[][] bufs = new byte[2][BUFFER_SIZE]; // buffers |
public int bufCur = -1; // current buffer number from the 'bufs' |
public int lenCur = 0; // length of valid data in current buffer |
public int pos = 0; // position in buffers, 0 means begin of current one |
|
private byte[][] bufs = new byte[2][BUFFER_SIZE]; // buffers |
private int bufCur = -1; // current buffer number from the 'bufs' |
private int lenCur = 0; // length of valid data in current buffer |
private int pos = 0; // position in buffers, 0 means begin of current one |
private int[] lens = new int[bufs.length]; // lengthes of valid data in buffers |
private int bufNext = -1; // next buffer to be used, -1 in not loaded |
private int bufPrev = -1; // previous buffer was used, -1 in not loaded |
66,13 → 62,15 |
byte[] buf; |
|
if(p >= lenCur) { |
if(!loadNextBuffer()) return 0; // no more data |
if(!loadNextBuffer()) { |
throw new IOException("end of input stream"); |
} |
|
p -= lenCur; |
buf = bufs[bufNext]; |
|
if(p > lens[bufNext]) { |
return 0; |
throw new IOException("end of input stream"); |
} |
} |
else { |
89,9 → 87,16 |
if(pos >= lenCur) toNextBuffer(); |
} |
|
public void toNextBuffer() |
public void skip(int n) |
throws IOException |
{ |
pos += n; |
while(pos >= lenCur) toNextBuffer(); |
} |
|
private void toNextBuffer() |
throws IOException |
{ |
if(bufNext < 0) { |
if(!loadNextBuffer()) return; |
} |
227,34 → 232,6 |
} |
|
// == TEST METHODS ================================================================================================= |
private static void testDirectAccess(CycleInputBuffer buf) |
throws Exception |
{ |
for(int i = 0; !buf.isEnd(); ++i) { |
byte c; |
if(buf.pos >= buf.lenCur) { |
c = buf.at(0); |
} |
else { |
c = buf.bufs[buf.bufCur][buf.pos]; |
} |
//System.out.print((char)c); |
|
++buf.pos; |
if(buf.pos >= buf.lenCur) buf.toNextBuffer(); |
} |
} |
|
private static void testIndirectAccess(CycleInputBuffer buf) |
throws Exception |
{ |
for(int i = 0; !buf.isEnd(); ++i) { |
byte c = buf.cur(); |
//System.out.print((char)c); |
buf.toNext(); |
} |
} |
|
public static void main(String[] args) |
throws Exception |
{ |
271,21 → 248,13 |
long startTime = System.nanoTime(); |
CycleInputBuffer buf = new CycleInputBuffer(in); |
|
testDirectAccess(buf); |
System.out.println("Direct access: " + (System.nanoTime() - startTime) / 1000000 + "ms"); |
while(!buf.isEnd()) { |
byte c = buf.cur(); |
//System.out.print((char)c); |
buf.toNext(); |
} |
finally { |
if(in != null) in.close(); |
System.out.println("Elapsed: " + (System.nanoTime() - startTime) / 1000000 + "ms"); |
} |
|
in = new FileInputStream(fileName); |
try { |
long startTime = System.nanoTime(); |
CycleInputBuffer buf = new CycleInputBuffer(in); |
|
testIndirectAccess(buf); |
System.out.println("Indirect access: " + (System.nanoTime() - startTime) / 1000000 + "ms"); |
} |
finally { |
if(in != null) in.close(); |
} |