@Override public final int read() throws IOException { final int c = peek(); if ( c >= 0 ) { // update position and buffer offset if peek says we aren't yet done position++; nextChar++; } return c; }
@Override public final int read() throws IOException { final int c = peek(); if ( c >= 0 ) { // update position and buffer offset if peek says we aren't yet done position++; nextChar++; } return c; }
@Override public final int read() throws IOException { final int c = peek(); if ( c >= 0 ) { // update position and buffer offset if peek says we aren't yet done position++; nextChar++; } return c; }
@Override public final boolean isDone() throws IOException { return nChars == -1 || peek() == -1; }
@Override public final boolean isDone() throws IOException { return nChars == -1 || peek() == -1; }
@Override public final boolean isDone() throws IOException { return nChars == -1 || peek() == -1; }
@Override public FeatureCodecHeader readHeader(final PositionalBufferedStream stream) throws IOException { // Construct a reader that does not read ahead (because we don't want to consume data from the stream that is not the header) final AsciiLineReader nonReadAheadLineReader = new AsciiLineReader(stream); final List<String> headerLines = new ArrayList<String>(); long headerLengthInBytes = 0; while (stream.peek() == ('#' & 0xff)) { // Look for header lines, which are prefixed by '#'. headerLines.add(nonReadAheadLineReader.readLine()); headerLengthInBytes = stream.getPosition(); } return new FeatureCodecHeader(headerLines, headerLengthInBytes); }
@Override public FeatureCodecHeader readHeader(final PositionalBufferedStream stream) throws IOException { // Construct a reader that does not read ahead (because we don't want to consume data from the stream that is not the header) final AsciiLineReader nonReadAheadLineReader = new AsciiLineReader(stream); final List<String> headerLines = new ArrayList<String>(); long headerLengthInBytes = 0; while (stream.peek() == ('#' & 0xff)) { // Look for header lines, which are prefixed by '#'. headerLines.add(nonReadAheadLineReader.readLine()); headerLengthInBytes = stream.getPosition(); } return new FeatureCodecHeader(headerLines, headerLengthInBytes); }
@Override public FeatureCodecHeader readHeader(final PositionalBufferedStream stream) throws IOException { // Construct a reader that does not read ahead (because we don't want to consume data from the stream that is not the header) final AsciiLineReader nonReadAheadLineReader = new AsciiLineReader(stream); final List<String> headerLines = new ArrayList<String>(); long headerLengthInBytes = 0; while (stream.peek() == ('#' & 0xff)) { // Look for header lines, which are prefixed by '#'. headerLines.add(nonReadAheadLineReader.readLine()); headerLengthInBytes = stream.getPosition(); } return new FeatureCodecHeader(headerLines, headerLengthInBytes); }
if (c == CARRIAGE_RETURN && stream.peek() == LINEFEED) {
/** For compressed BCF, unless the end has been reached, this is quite * inaccurate. */ @Override public float getProgress() { if (length == 0) return 1; if (!isBGZF) return (float)(in.getPosition() - fileStart) / length; try { if (in.peek() == -1) return 1; } catch (IOException e) { return 1; } // Add 1 to the denominator to make sure that we never report 1 here. return (float)((bci.getFilePointer() >>> 16) - fileStart) / (length + 1); } @Override public LongWritable getCurrentKey () { return key; }
if (c == CARRIAGE_RETURN && stream.peek() == LINEFEED) {
/** For compressed BCF, unless the end has been reached, this is quite * inaccurate. */ @Override public float getProgress() { if (length == 0) return 1; if (!isBGZF) return (float)(in.getPosition() - fileStart) / length; try { if (in.peek() == -1) return 1; } catch (IOException e) { return 1; } // Add 1 to the denominator to make sure that we never report 1 here. return (float)((bci.getFilePointer() >>> 16) - fileStart) / (length + 1); } @Override public LongWritable getCurrentKey () { return key; }
/** For compressed BCF, unless the end has been reached, this is quite * inaccurate. */ @Override public float getProgress() { if (length == 0) return 1; if (!isBGZF) return (float)(in.getPosition() - fileStart) / length; try { if (in.peek() == -1) return 1; } catch (IOException e) { return 1; } // Add 1 to the denominator to make sure that we never report 1 here. return (float)((bci.getFilePointer() >>> 16) - fileStart) / (length + 1); } @Override public LongWritable getCurrentKey () { return key; }
@Test public void testPeek() throws Exception{ int trials = 10; PositionalBufferedStream is = new PositionalBufferedStream(FileIs); int bb = is.peek(); for(int ii=0; ii < trials; ii++){ Assert.assertEquals(is.peek(), bb); Assert.assertEquals(is.getPosition(), 0); } while((bb = is.peek()) >= 0){ Assert.assertEquals(is.read(), bb); } }
@Override public boolean nextKeyValue() throws IOException { if (in.peek() == -1) return false; if (!isBGZF && in.getPosition() >= fileStart + length) return false; final VariantContext v = codec.decode(in); Integer chromIdx = contigDict.get(v.getContig()); if (chromIdx == null) chromIdx = (int)MurmurHash3.murmurhash3(v.getContig(), 0); key.set((long)chromIdx << 32 | (long)(v.getStart() - 1)); vc.set(v); return true; } }
@Override public boolean nextKeyValue() throws IOException { if (in.peek() == -1) return false; if (!isBGZF && in.getPosition() >= fileStart + length) return false; final VariantContext v = codec.decode(in); Integer chromIdx = contigDict.get(v.getContig()); if (chromIdx == null) chromIdx = (int)MurmurHash3.murmurhash3(v.getContig(), 0); key.set((long)chromIdx << 32 | (long)(v.getStart() - 1)); vc.set(v); return true; } }
@Override public boolean nextKeyValue() throws IOException { if (in.peek() == -1) return false; if (!isBGZF && in.getPosition() >= fileStart + length) return false; final VariantContext v = codec.decode(in); Integer chromIdx = contigDict.get(v.getContig()); if (chromIdx == null) chromIdx = (int)MurmurHash3.murmurhash3(v.getContig(), 0); key.set((long)chromIdx << 32 | (long)(v.getStart() - 1)); vc.set(v); return true; } }
@Test public void testSkip() throws IOException { for ( int skipSizeBase : Arrays.asList(0, 10, 100, 1000, 10000, 1000000)) { for ( int skipSizeAdd = 0; skipSizeAdd < 10; skipSizeAdd ++ ) { final int skipSize = skipSizeBase + skipSizeAdd; final byte[] bytes = new byte[skipSize+2]; Arrays.fill(bytes, 0, skipSize, (byte)0); bytes[skipSize] = 1; bytes[skipSize+1] = 2; final InputStream is = new ByteArrayInputStream(bytes); final PositionalBufferedStream pbs = new PositionalBufferedStream(is); pbs.skip(skipSize); // first value is 1 Assert.assertTrue(! pbs.isDone()); Assert.assertEquals(pbs.getPosition(), skipSize); Assert.assertEquals(pbs.peek(), 1); Assert.assertEquals(pbs.read(), 1); Assert.assertTrue(! pbs.isDone()); Assert.assertEquals(pbs.getPosition(), skipSize + 1); Assert.assertEquals(pbs.peek(), 2); Assert.assertEquals(pbs.read(), 2); Assert.assertTrue(pbs.isDone()); } } }
private void testStream(final byte[] bytes) throws IOException { final InputStream is = new ByteArrayInputStream(bytes); final PositionalBufferedStream pbs = new PositionalBufferedStream(is); int bytePos = 0; while ( ! pbs.isDone() ) { Assert.assertTrue(bytePos < bytes.length); // test position Assert.assertEquals(pbs.getPosition(), bytePos); // test peek final byte atPos = bytes[bytePos]; Assert.assertEquals(toByte(pbs.peek()), atPos); // test position Assert.assertEquals(pbs.getPosition(), bytePos); // test read Assert.assertEquals(toByte(pbs.read()), atPos); bytePos++; // test position Assert.assertEquals(pbs.getPosition(), bytePos); // test repeek if ( bytePos < bytes.length ) { Assert.assertEquals(toByte(pbs.peek()), bytes[bytePos]); // test position Assert.assertEquals(pbs.getPosition(), bytePos); } } Assert.assertEquals(bytePos, bytes.length); pbs.close(); }