Changeset 8b8d32e


Ignore:
Timestamp:
Dec 18, 2017 10:23:42 PM (2 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
73796458
Parents:
be004cd
Message:

Util: LookaheadInputStream? speedups and cleanups

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/util/LookaheadInputStream.java

    rbe004cd r8b8d32e  
    77import java.util.Arrays;
    88
     9import net.i2p.data.DataHelper;
     10
    911/**
    1012 * Simple lookahead buffer to keep the last K bytes in reserve,
    1113 * configured to easily be reused.  Currently only used by the
    12  * ResettableGZIPInputStream
    13  *
     14 * ResettableGZIPInputStream.
    1415 */
    1516public class LookaheadInputStream extends FilterInputStream {
    1617    private boolean _eofReached;
    1718    private final byte[] _footerLookahead;
     19    private final int size;
     20    // Next byte to read.
     21    private int index;
    1822    private static final InputStream _fakeInputStream = new ByteArrayInputStream(new byte[0]);
    1923   
     
    3034        super(_fakeInputStream);
    3135        _footerLookahead = new byte[lookaheadSize];
     36        size = lookaheadSize;
    3237    }
    3338   
     
    3843     *  Resets everything if the LookaheadInputStream was previously used.
    3944     *  WARNING - blocking until lookaheadSize bytes are read!
     45     *
     46     *  @throws IOException if less than lookaheadSize bytes could be read.
    4047     */
    4148    public void initialize(InputStream src) throws IOException {
    4249        in = src;
    4350        _eofReached = false;
    44         Arrays.fill(_footerLookahead, (byte)0x00);
    45         int footerRead = 0;
    46         while (footerRead < _footerLookahead.length) {
    47             int read = in.read(_footerLookahead, footerRead, _footerLookahead.length - footerRead);
    48             if (read == -1) throw new IOException("EOF reading the footer lookahead");
    49             footerRead += read;
    50         }
     51        index = 0;
     52        DataHelper.read(in, _footerLookahead);
    5153    }
    5254   
     
    5456    public int read() throws IOException {
    5557        if (_eofReached)
    56             return -1; //throw new IOException("Already past the EOF");
     58            return -1;
    5759        int c = in.read();
    5860        if (c == -1) {
     
    6062            return -1;
    6163        }
    62         int rv = _footerLookahead[0];
    63         // FIXME use an index!!!!!!!!!!!!
    64         System.arraycopy(_footerLookahead, 1, _footerLookahead, 0, _footerLookahead.length-1);
    65         _footerLookahead[_footerLookahead.length-1] = (byte)c;
    66         if (rv < 0) rv += 256;
     64        int rv = _footerLookahead[index] & 0xff;
     65        _footerLookahead[index] = (byte)c;
     66        index++;
     67        if (index >= size)
     68            index = 0;
    6769        return rv;
    68     }
    69 
    70     @Override
    71     public int read(byte buf[]) throws IOException {
    72         return read(buf, 0, buf.length);
    7370    }
    7471
     
    9188    }
    9289   
    93     /** grab the lookahead footer */
    94     public byte[] getFooter() { return _footerLookahead; }
     90    /**
     91     * Grab the lookahead footer.
     92     * This will be of size lookaheadsize given in constructor.
     93     * The last byte received will be in the last byte of the array.
     94     */
     95    public byte[] getFooter() {
     96        if (index == 0)
     97            return _footerLookahead;
     98        byte[] rv = new byte[size];
     99        System.arraycopy(_footerLookahead, index, rv, 0, size - index);
     100        System.arraycopy(_footerLookahead, 0, rv, size - index, index);
     101        return rv;
     102    }
     103   
     104    /**
     105     *  @since 0.9.33
     106     */
     107    @Override
     108    public long skip(long n) throws IOException {
     109        long rv = 0;
     110        int c;
     111        while (rv < n && (c = read()) >= 0) {
     112            rv++;
     113        }
     114        return rv;
     115    }
    95116   
    96117/*******
     
    112133                if (lis.getFooter()[i] != (byte)(i+24))
    113134                    throw new RuntimeException("Error at footer " + i + " [" + lis.getFooter()[i] + "]");
    114             System.out.println("Everything is fine in general");
    115135        } catch (Exception e) {
    116136            e.printStackTrace();
     
    118138       
    119139        for (int i = 9; i < 32*1024; i++) {
    120             if (!test(i)) break;
     140            if (!test(i)) {
     141                System.out.println("Everything is NOT fine at size=" + i);
     142                break;
     143            }
    121144        }
     145        System.out.println("Everything is fine in general");
    122146    }
    123147   
     
    138162                if (lis.getFooter()[i] != buf[i+(size-8)])
    139163                    throw new RuntimeException("Error at footer " + i + " [" + lis.getFooter()[i] + "]");
    140             System.out.println("Everything is fine at size=" + size);
     164            //System.out.println("Everything is fine at size=" + size);
    141165            return true;
    142166        } catch (Exception e) {
Note: See TracChangeset for help on using the changeset viewer.