Search in sources :

Example 11 with MongoGridFSException

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

the class GridFSDownloadStreamImpl method getBufferFromChunk.

private byte[] getBufferFromChunk(final Document chunk, final int expectedChunkIndex) {
    if (chunk == null || chunk.getInteger("n") != expectedChunkIndex) {
        throw chunkNotFound(expectedChunkIndex);
    } else if (!(chunk.get("data") instanceof Binary)) {
        throw new MongoGridFSException("Unexpected data format for the chunk");
    }
    byte[] data = chunk.get("data", Binary.class).getData();
    long expectedDataLength = expectedChunkIndex + 1 == numberOfChunks ? fileInfo.getLength() - (expectedChunkIndex * (long) fileInfo.getChunkSize()) : fileInfo.getChunkSize();
    if (data.length != expectedDataLength) {
        throw new MongoGridFSException(format("Chunk size data length is not the expected size. " + "The size was %s for file_id: %s chunk index %s it should be %s bytes.", data.length, fileInfo.getId(), expectedChunkIndex, expectedDataLength));
    }
    return data;
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) Binary(org.bson.types.Binary)

Example 12 with MongoGridFSException

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

the class GridFSBucketImpl method downloadToStream.

private void downloadToStream(final GridFSDownloadStream downloadStream, final OutputStream destination) {
    byte[] buffer = new byte[downloadStream.getGridFSFile().getChunkSize()];
    int len;
    MongoGridFSException savedThrowable = null;
    try {
        while ((len = downloadStream.read(buffer)) != -1) {
            destination.write(buffer, 0, len);
        }
    } catch (IOException e) {
        savedThrowable = new MongoGridFSException("IOException when reading from the OutputStream", e);
    } catch (Exception e) {
        savedThrowable = new MongoGridFSException("Unexpected Exception when reading GridFS and writing to the Stream", e);
    } finally {
        try {
            downloadStream.close();
        } catch (Exception e) {
        // Do nothing
        }
        if (savedThrowable != null) {
            throw savedThrowable;
        }
    }
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) IOException(java.io.IOException) MongoGridFSException(com.mongodb.MongoGridFSException) IOException(java.io.IOException)

Example 13 with MongoGridFSException

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

the class GridFSDownloadStreamImpl method getBufferFromChunk.

private byte[] getBufferFromChunk(final Document chunk, final int expectedChunkIndex) {
    if (chunk == null || chunk.getInteger("n") != expectedChunkIndex) {
        throw new MongoGridFSException(format("Could not find file chunk for file_id: %s at chunk index %s.", fileId, expectedChunkIndex));
    }
    if (!(chunk.get("data") instanceof Binary)) {
        throw new MongoGridFSException("Unexpected data format for the chunk");
    }
    byte[] data = chunk.get("data", Binary.class).getData();
    long expectedDataLength = 0;
    boolean extraChunk = false;
    if (expectedChunkIndex + 1 > numberOfChunks) {
        extraChunk = true;
    } else if (expectedChunkIndex + 1 == numberOfChunks) {
        expectedDataLength = length - (expectedChunkIndex * (long) chunkSizeInBytes);
    } else {
        expectedDataLength = chunkSizeInBytes;
    }
    if (extraChunk && data.length > expectedDataLength) {
        throw new MongoGridFSException(format("Extra chunk data for file_id: %s. Unexpected chunk at chunk index %s." + "The size was %s and it should be %s bytes.", fileId, expectedChunkIndex, data.length, expectedDataLength));
    } else if (data.length != expectedDataLength) {
        throw new MongoGridFSException(format("Chunk size data length is not the expected size. " + "The size was %s for file_id: %s chunk index %s it should be %s bytes.", data.length, fileId, expectedChunkIndex, expectedDataLength));
    }
    return data;
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) Binary(org.bson.types.Binary)

Aggregations

MongoGridFSException (com.mongodb.MongoGridFSException)13 BsonDocument (org.bson.BsonDocument)6 SingleResultCallback (com.mongodb.async.SingleResultCallback)3 IOException (java.io.IOException)3 BsonString (org.bson.BsonString)3 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)2 DeleteResult (com.mongodb.client.result.DeleteResult)2 BsonArray (org.bson.BsonArray)2 BsonObjectId (org.bson.BsonObjectId)2 BsonValue (org.bson.BsonValue)2 Document (org.bson.Document)2 Binary (org.bson.types.Binary)2 ObjectId (org.bson.types.ObjectId)2 UpdateResult (com.mongodb.client.result.UpdateResult)1