Search in sources :

Example 1 with MongoSocketReadException

use of com.mongodb.MongoSocketReadException in project mongo-java-driver by mongodb.

the class SocketChannelStream method read.

@Override
public ByteBuf read(final int numBytes) throws IOException {
    ByteBuf buffer = bufferProvider.getBuffer(numBytes);
    isTrue("open", !isClosed());
    int totalBytesRead = 0;
    while (totalBytesRead < buffer.limit()) {
        int bytesRead = socketChannel.read(buffer.asNIO());
        if (bytesRead == -1) {
            buffer.release();
            throw new MongoSocketReadException("Prematurely reached end of stream", getAddress());
        }
        totalBytesRead += bytesRead;
    }
    return buffer.flip();
}
Also used : MongoSocketReadException(com.mongodb.MongoSocketReadException) ByteBuf(org.bson.ByteBuf)

Example 2 with MongoSocketReadException

use of com.mongodb.MongoSocketReadException in project mongo-java-driver by mongodb.

the class SocketStream method read.

@Override
public ByteBuf read(final int numBytes) throws IOException {
    ByteBuf buffer = bufferProvider.getBuffer(numBytes);
    int totalBytesRead = 0;
    byte[] bytes = buffer.array();
    while (totalBytesRead < buffer.limit()) {
        int bytesRead = inputStream.read(bytes, totalBytesRead, buffer.limit() - totalBytesRead);
        if (bytesRead == -1) {
            buffer.release();
            throw new MongoSocketReadException("Prematurely reached end of stream", getAddress());
        }
        totalBytesRead += bytesRead;
    }
    return buffer;
}
Also used : MongoSocketReadException(com.mongodb.MongoSocketReadException) ByteBuf(org.bson.ByteBuf)

Aggregations

MongoSocketReadException (com.mongodb.MongoSocketReadException)2 ByteBuf (org.bson.ByteBuf)2