Search in sources :

Example 6 with EndOfSegmentException

use of io.pravega.client.segment.impl.EndOfSegmentException in project pravega by pravega.

the class EventStreamReaderImpl method fetchEvent.

@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();
        return deserializer.deserialize(buffer);
    } catch (EndOfSegmentException e) {
        throw new NoSuchEventException(e.getMessage());
    } catch (NoSuchSegmentException | SegmentTruncatedException e) {
        throw new NoSuchEventException("Event no longer exists.");
    }
}
Also used : EndOfSegmentException(io.pravega.client.segment.impl.EndOfSegmentException) EventSegmentReader(io.pravega.client.segment.impl.EventSegmentReader) NoSuchEventException(io.pravega.client.segment.impl.NoSuchEventException) SegmentTruncatedException(io.pravega.client.segment.impl.SegmentTruncatedException) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) NoSuchSegmentException(io.pravega.client.segment.impl.NoSuchSegmentException)

Example 7 with EndOfSegmentException

use of io.pravega.client.segment.impl.EndOfSegmentException in project pravega by pravega.

the class ByteStreamReaderImpl method read.

@Override
public int read() throws IOException {
    Exceptions.checkNotClosed(closed.get(), this);
    ByteBuffer buffer = ByteBuffer.allocate(1);
    try {
        int read;
        synchronized (input) {
            read = input.read(buffer, Long.MAX_VALUE);
        }
        if (read > 0) {
            buffer.flip();
            return buffer.get() & 0xFF;
        } else {
            return read;
        }
    } catch (EndOfSegmentException e) {
        return -1;
    }
}
Also used : EndOfSegmentException(io.pravega.client.segment.impl.EndOfSegmentException) ByteBuffer(java.nio.ByteBuffer)

Example 8 with EndOfSegmentException

use of io.pravega.client.segment.impl.EndOfSegmentException in project pravega by pravega.

the class MockSegmentIoStreams method read.

/**
 * Event read.
 * @see io.pravega.client.segment.impl.EventSegmentReader#read(long)
 */
@Override
@Synchronized
public ByteBuffer read(long timeout) throws EndOfSegmentException, SegmentTruncatedException {
    if (readOffset >= writeOffset) {
        throw new EndOfSegmentException();
    }
    if (readOffset < startingOffset) {
        throw new SegmentTruncatedException("Data below " + startingOffset + " has been truncated");
    }
    ByteBuffer buffer = dataWritten.floorEntry(readOffset).getValue();
    readOffset += buffer.remaining();
    ByteBuffer result = buffer.slice();
    result.position(WireCommands.TYPE_PLUS_LENGTH_SIZE);
    return result;
}
Also used : EndOfSegmentException(io.pravega.client.segment.impl.EndOfSegmentException) SegmentTruncatedException(io.pravega.client.segment.impl.SegmentTruncatedException) ByteBuffer(java.nio.ByteBuffer) Synchronized(lombok.Synchronized)

Aggregations

EndOfSegmentException (io.pravega.client.segment.impl.EndOfSegmentException)8 ByteBuffer (java.nio.ByteBuffer)7 SegmentTruncatedException (io.pravega.client.segment.impl.SegmentTruncatedException)5 EventSegmentReader (io.pravega.client.segment.impl.EventSegmentReader)4 Segment (io.pravega.client.segment.impl.Segment)4 Cleanup (lombok.Cleanup)3 Controller (io.pravega.client.control.impl.Controller)2 SegmentInputStreamFactory (io.pravega.client.segment.impl.SegmentInputStreamFactory)2 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)2 MockController (io.pravega.client.stream.mock.MockController)2 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)2 Timer (io.pravega.common.Timer)2 Semaphore (java.util.concurrent.Semaphore)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Synchronized (lombok.Synchronized)2 Test (org.junit.Test)2 InOrder (org.mockito.InOrder)2 NoSuchEventException (io.pravega.client.segment.impl.NoSuchEventException)1 NoSuchSegmentException (io.pravega.client.segment.impl.NoSuchSegmentException)1 SegmentInputStream (io.pravega.client.segment.impl.SegmentInputStream)1