/** * Gets a value representing the length of this InputStream, in bytes, excluding the 4 bytes required for encoding * the length. */ @VisibleForTesting int getLength() { return ((BoundedInputStream) this.in).getBound(); }
@VisibleForTesting int getLength() { return this.contents.getBound(); }
/** * Creates a new instance of the DataFrameEntry class. * * @param header The Header for this entry. * @param data A ByteArraySegment representing the contents of this frame. * @param frameAddress The Address of the containing DataFrame. * @param lastEntryInDataFrame Whether this is the last entry in the DataFrame. * @param frameOffset The offset within the DataFrame where this Entry starts. */ private DataFrameEntry(EntryHeader header, BoundedInputStream data, LogAddress frameAddress, boolean lastEntryInDataFrame, int frameOffset) { this.firstRecordEntry = header.isFirstRecordEntry(); this.lastRecordEntry = header.isLastRecordEntry(); this.lastEntryInDataFrame = lastEntryInDataFrame; this.frameAddress = frameAddress; this.data = data; this.length = data.getBound(); this.frameOffset = frameOffset; }
/** * Tests the ability to skip remaining bytes upon closing. */ @Test public void testSkipRemainingOnClose() throws Exception { for (int size = 0; size < MAX_SIZE; size++) { int bound = size / 2; val data = construct(size); @Cleanup val input = new ByteArrayInputStream(data); @Cleanup val bis = new BoundedInputStream(input, bound); Assert.assertEquals("Unexpected value for getBound().", bound, bis.getBound()); if (size % 2 == 1) { // Only read some data for every other iteration. int bytesRead = bis.read(new byte[bound / 2]); Assert.assertEquals("Unexpected number of bytes read.", bound / 2, bytesRead); } bis.close(); Assert.assertEquals("Unexpected number of bytes remaining to be read after skipRemaining().", data.length - bis.getBound(), input.available()); } }
@Override public DataFrameEntry getNext() throws IOException { closeLast(); if (reachedEnd()) { return null; } // Integrity check. This means that we have a corrupt frame. if (this.contents.getRemaining() < EntryHeader.HEADER_SIZE) { throw new SerializationException(String.format("Data Frame is corrupt. InputStream has insufficient bytes for a new Entry Header (%d).", this.contents.getRemaining())); } // Determine the length of the next record and advance the position by the appropriate amount of bytes. ReadEntryHeader header = new ReadEntryHeader(this.contents); // Integrity check. This means that we have a corrupt frame. if (this.contents.getRemaining() < header.getEntryLength()) { throw new SerializationException(String.format("Data Frame is corrupt. Found Entry Length %d which cannot fit in the Frame's remaining length of %d.", header.getEntryLength(), this.contents.getRemaining())); } // Get the result contents && advance the positions. int frameOffset = this.bufferOffset + this.contents.getBound() - this.contents.getRemaining(); BoundedInputStream resultContents = this.contents.subStream(header.getEntryLength()); this.lastEntryContents = resultContents; return new DataFrameEntry(header, resultContents, this.frameAddress, reachedEnd(), frameOffset); }
while (bis.available() > 0) { val s = bis.subStream(size); Assert.assertEquals("Unexpected bound from sub-stream for bound " + size, size, s.getBound()); int c = s.read(buffer); AssertExtensions.assertArrayEquals("Unexpected data read into buffer for bound " + size, data, expectedPos, buffer, 0, c); Assert.assertEquals("Unexpected result from available() after reading a buffer for bound " + size, s.getBound() - c, s.available()); Assert.assertEquals("Base BoundedInputStream's position did not advance for bound " + size, bis.getBound() - expectedPos, bis.available()); size = Math.min(size + 1, bis.available());