Changeset 8b8d32e
- Timestamp:
- Dec 18, 2017 10:23:42 PM (3 years ago)
- Branches:
- master
- Children:
- 73796458
- Parents:
- be004cd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
core/java/src/net/i2p/util/LookaheadInputStream.java
rbe004cd r8b8d32e 7 7 import java.util.Arrays; 8 8 9 import net.i2p.data.DataHelper; 10 9 11 /** 10 12 * Simple lookahead buffer to keep the last K bytes in reserve, 11 13 * configured to easily be reused. Currently only used by the 12 * ResettableGZIPInputStream 13 * 14 * ResettableGZIPInputStream. 14 15 */ 15 16 public class LookaheadInputStream extends FilterInputStream { 16 17 private boolean _eofReached; 17 18 private final byte[] _footerLookahead; 19 private final int size; 20 // Next byte to read. 21 private int index; 18 22 private static final InputStream _fakeInputStream = new ByteArrayInputStream(new byte[0]); 19 23 … … 30 34 super(_fakeInputStream); 31 35 _footerLookahead = new byte[lookaheadSize]; 36 size = lookaheadSize; 32 37 } 33 38 … … 38 43 * Resets everything if the LookaheadInputStream was previously used. 39 44 * WARNING - blocking until lookaheadSize bytes are read! 45 * 46 * @throws IOException if less than lookaheadSize bytes could be read. 40 47 */ 41 48 public void initialize(InputStream src) throws IOException { 42 49 in = src; 43 50 _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); 51 53 } 52 54 … … 54 56 public int read() throws IOException { 55 57 if (_eofReached) 56 return -1; //throw new IOException("Already past the EOF");58 return -1; 57 59 int c = in.read(); 58 60 if (c == -1) { … … 60 62 return -1; 61 63 } 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; 67 69 return rv; 68 }69 70 @Override71 public int read(byte buf[]) throws IOException {72 return read(buf, 0, buf.length);73 70 } 74 71 … … 91 88 } 92 89 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 } 95 116 96 117 /******* … … 112 133 if (lis.getFooter()[i] != (byte)(i+24)) 113 134 throw new RuntimeException("Error at footer " + i + " [" + lis.getFooter()[i] + "]"); 114 System.out.println("Everything is fine in general");115 135 } catch (Exception e) { 116 136 e.printStackTrace(); … … 118 138 119 139 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 } 121 144 } 145 System.out.println("Everything is fine in general"); 122 146 } 123 147 … … 138 162 if (lis.getFooter()[i] != buf[i+(size-8)]) 139 163 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); 141 165 return true; 142 166 } catch (Exception e) {
Note: See TracChangeset
for help on using the changeset viewer.