/** * Reads the bytes of a single event from the segment. Buffering is performed internally to try to prevent * blocking. If there is no event after timeout, then it returns null. EndOfSegmentException indicates the * segment has ended and there are no more events to read. * * @return A ByteBuffer containing the serialized data that was written via * {@link EventStreamWriter#writeEvent(String, Object)} * @throws EndOfSegmentException If no event could be read because the end of the segment was reached. * @throws SegmentTruncatedException If the segment has been truncated beyond the current offset and the data cannot be read. */ public default ByteBuffer read() throws EndOfSegmentException, SegmentTruncatedException { return read(Long.MAX_VALUE); }
@Override @SneakyThrows(EndOfSegmentException.class) //endingOffset should make this impossible. public T next() { if (!hasNext()) { throw new NoSuchElementException(); } try { return deserializer.deserialize(input.read()); } catch (NoSuchSegmentException | SegmentTruncatedException e) { throw new TruncatedDataException("Segment " + segment + " has been truncated."); } }
Mockito.when(segInputStreamFactory.createEventReaderForSegment(any(Segment.class), anyLong())).thenReturn(segmentInputStream); Mockito.when(segmentInputStream.read(anyLong())).thenThrow(SegmentTruncatedException.class);
Mockito.when(segmentInputStream1.read(anyLong())).thenThrow(new SegmentTruncatedException()); Mockito.when(segmentInputStream1.getSegmentId()).thenReturn(segment); SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, ""); ByteBuffer buffer = writeInt(stream, 1); Mockito.when(segmentInputStream2.read(anyLong())).thenReturn(buffer); Mockito.when(segmentInputStream2.getSegmentId()).thenReturn(Segment.fromScopedName("Foo/test/0")); Mockito.when(segmentInputStream2.getOffset()).thenReturn(10L);
Mockito.when(segmentInputStream1.read(anyLong())).thenThrow(new EndOfSegmentException(EndOfSegmentException.ErrorType.END_OFFSET_REACHED)); Mockito.when(segmentInputStream1.getSegmentId()).thenReturn(segment); SegmentOutputStream stream = segmentStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallback, writerConfig, ""); ByteBuffer buffer = writeInt(stream, 1); Mockito.when(segmentInputStream2.read(anyLong())).thenReturn(buffer); Mockito.when(segmentInputStream2.getSegmentId()).thenReturn(Segment.fromScopedName("Foo/test/0")); Mockito.when(segmentInputStream2.getOffset()).thenReturn(10L);
@Override public Type fetchEvent(EventPointer pointer) throws NoSuchEventException { Preconditions.checkNotNull(pointer); // Create SegmentInputStream @Cleanup EventSegmentReader inputStream = inputStreamFactory.createEventReaderForSegment(pointer.asImpl().getSegment(), pointer.asImpl().getEventLength()); inputStream.setOffset(pointer.asImpl().getEventStartOffset()); // Read event try { ByteBuffer buffer = inputStream.read(); Type result = deserializer.deserialize(buffer); return result; } catch (EndOfSegmentException e) { throw new NoSuchEventException(e.getMessage()); } catch (NoSuchSegmentException | SegmentTruncatedException e) { throw new NoSuchEventException("Event no longer exists."); } }
offset = segmentReader.getOffset(); try { buffer = segmentReader.read(waitTime); } catch (EndOfSegmentException e) { boolean fetchSuccessors = e.getErrorType().equals(ErrorType.END_OF_SEGMENT_REACHED);
ByteBuffer result = in.read(); assertEquals(ByteBuffer.wrap(testString.getBytes()), result); out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>())); out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[150000]), new CompletableFuture<>())); assertEquals(in.read().capacity(), 15); assertEquals(in.read().capacity(), 15); assertEquals(in.read().capacity(), 150000);
ByteBuffer result = in.read(); assertEquals(ByteBuffer.wrap(testString), result); assertNull(in.read(100)); assertFalse(out.write(ByteBuffer.wrap(testString), 0)); assertTrue(out.write(ByteBuffer.wrap(testString), testString.length + WireCommands.TYPE_PLUS_LENGTH_SIZE)); result = in.read(); assertEquals(ByteBuffer.wrap(testString), result); assertNull(in.read(100));