private void closeLast() throws IOException { BoundedInputStream last = this.lastEntryContents; if (last != null) { last.close(); this.lastEntryContents = null; } }
@Override public OutputStream subStream(int streamPosition, int length) { return new FixedByteArrayOutputStream(this.buf, streamPosition, length); }
@Override public void write(byte[] array) { this.write(array, 0, array.length); }
@Override protected EnhancedByteArrayOutputStream createInstance(byte[] initialData) { val s = new EnhancedByteArrayOutputStream(); s.write(initialData); return s; }
/** * Attempts to read the contents of the InputStream and load it into this ByteArraySegment. Up to getLength() bytes * will be read from the InputStream, but no guarantees are made that the entire ByteArraySegment will be populated. * <p> * Only attempts to read the data, and does not expect any other header/footer information in the InputStream. This * method is the exact reverse of writeTo(). * * @param stream The InputStream to read from. * @return The number of bytes read. This will be less than or equal to getLength(). * @throws IOException If the InputStream threw one. */ public int readFrom(InputStream stream) throws IOException { return StreamHelpers.readAll(stream, this.array, this.startOffset, this.length); }
@Override public void close() throws IOException { // Skip over the remaining bytes. Do not close the underlying InputStream. if (this.remaining > 0) { int toSkip = this.remaining; long skipped = skip(toSkip); if (skipped != toSkip) { throw new SerializationException(String.format("Read %d fewer byte(s) than expected only able to skip %d.", toSkip, skipped)); } } else if (this.remaining < 0) { throw new SerializationException(String.format("Read more bytes than expected (%d).", -this.remaining)); } }
@After public void tearDown() { FileHelpers.deleteFileOrDirectory(baseDir); baseDir = null; }
CompletableFuture<Long> getLength(Duration timeout) { return CompletableFuture.supplyAsync(() -> { synchronized (this.data) { return (long) this.data.size(); } }, executorService()); }
private void validateHeader(int keyLength, int valueLength) throws SerializationException { if (keyLength <= 0 || keyLength > MAX_KEY_LENGTH || (valueLength < 0 && valueLength != NO_VALUE) || keyLength + valueLength > MAX_SERIALIZATION_LENGTH) { throw new SerializationException(String.format("Read header with invalid data. KeyLength=%s, ValueLength=%s", keyLength, valueLength)); } }
private boolean reachedEnd() { return this.contents.getRemaining() <= 0; } }
@Override protected EnhancedByteArrayOutputStream createInstance(byte[] initialData) { val s = new EnhancedByteArrayOutputStream(); s.write(initialData); return s; }
TestLogItem(InputStream input) throws IOException { DataInputStream dataInput = new DataInputStream(input); this.sequenceNumber = dataInput.readLong(); this.data = new byte[dataInput.readInt()]; int readBytes = StreamHelpers.readAll(dataInput, this.data, 0, this.data.length); assert readBytes == this.data.length : "SeqNo " + this.sequenceNumber + ": expected to read " + this.data.length + " bytes, but read " + readBytes; this.failAfterCompleteRatio = -1; }
@Override public OutputStream subStream(int streamPosition, int length) { return new FixedByteArrayOutputStream(this.array, this.offset + streamPosition, length); }
private void startNewRecordInCurrentFrame(boolean firstRecordEntry) throws SerializationException { if (!this.currentFrame.startNewEntry(firstRecordEntry)) { throw new SerializationException("Unable to start a new record."); } this.hasDataInCurrentFrame = true; }
@Override protected FixedByteArrayOutputStream createInstance(byte[] initialData) { return new FixedByteArrayOutputStream(initialData, 0, initialData.length); }
@Override protected FixedByteArrayOutputStream createInstance(byte[] initialData) { return new FixedByteArrayOutputStream(initialData, 0, initialData.length); }