use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method readAndWriteInputStream.
private void readAndWriteInputStream(final AsyncInputStream source, final GridFSUploadStream uploadStream, final ByteBuffer buffer, final SingleResultCallback<Void> callback) {
buffer.clear();
source.read(buffer, new SingleResultCallback<Integer>() {
@Override
public void onResult(final Integer result, final Throwable t) {
if (t != null) {
if (t instanceof IOException) {
uploadStream.abort(new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable abortException) {
if (abortException != null) {
callback.onResult(null, abortException);
} else {
callback.onResult(null, new MongoGridFSException("IOException when reading from the InputStream", t));
}
}
});
} else {
callback.onResult(null, t);
}
} else if (result > 0) {
buffer.flip();
uploadStream.write(buffer, new SingleResultCallback<Integer>() {
@Override
public void onResult(final Integer result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
readAndWriteInputStream(source, uploadStream, buffer, callback);
}
}
});
} else {
uploadStream.close(callback);
}
}
});
}
use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method rename.
@Override
public void rename(final BsonValue id, final String newFilename, final SingleResultCallback<Void> callback) {
notNull("id", id);
notNull("newFilename", newFilename);
notNull("callback", callback);
final SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
filesCollection.updateOne(new BsonDocument("_id", id), new BsonDocument("$set", new BsonDocument("filename", new BsonString(newFilename))), new SingleResultCallback<UpdateResult>() {
@Override
public void onResult(final UpdateResult result, final Throwable t) {
if (t != null) {
errHandlingCallback.onResult(null, t);
} else if (result.wasAcknowledged() && result.getMatchedCount() == 0) {
errHandlingCallback.onResult(null, new MongoGridFSException(format("No file found with the ObjectId: %s", id)));
} else {
errHandlingCallback.onResult(null, null);
}
}
});
}
use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.
the class GridFSUploadStreamImpl method writeChunk.
private void writeChunk(final SingleResultCallback<Void> callback) {
if (md5 == null) {
callback.onResult(null, new MongoGridFSException("No MD5 message digest available, cannot upload file"));
} else if (bufferOffset > 0) {
chunksCollection.insertOne(new Document("files_id", fileId).append("n", chunkIndex).append("data", getData()), new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
if (t != null) {
callback.onResult(null, t);
} else {
md5.update(buffer);
chunkIndex++;
bufferOffset = 0;
callback.onResult(null, null);
}
}
});
} else {
callback.onResult(null, null);
}
}
use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method delete.
@Override
public void delete(final BsonValue id) {
DeleteResult result = filesCollection.deleteOne(new BsonDocument("_id", id));
chunksCollection.deleteMany(new BsonDocument("files_id", id));
if (result.wasAcknowledged() && result.getDeletedCount() == 0) {
throw new MongoGridFSException(format("No file found with the id: %s", id));
}
}
use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method uploadFromStream.
@Override
public void uploadFromStream(final BsonValue id, final String filename, final InputStream source, final GridFSUploadOptions options) {
GridFSUploadStream uploadStream = openUploadStream(id, filename, options);
int chunkSize = options.getChunkSizeBytes() == null ? chunkSizeBytes : options.getChunkSizeBytes();
byte[] buffer = new byte[chunkSize];
int len;
try {
while ((len = source.read(buffer)) != -1) {
uploadStream.write(buffer, 0, len);
}
uploadStream.close();
} catch (IOException e) {
uploadStream.abort();
throw new MongoGridFSException("IOException when reading from the InputStream", e);
}
}
Aggregations