
com.mongodb.reactivestreams.client.internal.GridFSAsyncStreamHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongodb-driver-reactivestreams
Show all versions of mongodb-driver-reactivestreams
A Reactive Streams implementation of the MongoDB Java driver
/*
* Copyright 2016 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.reactivestreams.client.internal;
import com.mongodb.Block;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.reactivestreams.client.Success;
import com.mongodb.reactivestreams.client.gridfs.AsyncInputStream;
import com.mongodb.reactivestreams.client.gridfs.AsyncOutputStream;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import java.nio.ByteBuffer;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.async.client.Observables.observe;
import static com.mongodb.reactivestreams.client.internal.PublisherHelper.voidToSuccessCallback;
/**
* Internal GridFS AsyncStream Helper
*
* This should not be considered a part of the public API.
*/
public final class GridFSAsyncStreamHelper {
/**
* Converts the callback AsyncInputStream to a Publisher AsyncInputStream
*
* This should not be considered a part of the public API.
* @param wrapper the callback AsyncInputStream
* @return the Publisher AsyncInputStream
*/
public static AsyncInputStream toAsyncInputStream(final com.mongodb.async.client.gridfs.AsyncInputStream wrapper) {
notNull("wrapper", wrapper);
return new AsyncInputStream() {
@Override
public Publisher read(final ByteBuffer dst) {
return new ObservableToPublisher(observe(new Block>() {
@Override
public void apply(final SingleResultCallback callback) {
wrapper.read(dst, callback);
}
}));
}
@Override
public Publisher close() {
return new ObservableToPublisher(observe(new Block>() {
@Override
public void apply(final SingleResultCallback callback) {
wrapper.close(voidToSuccessCallback(callback));
}
}));
}
};
}
/**
* Converts the callback AsyncOutputStream to a Publisher AsyncOutputStream
*
* This should not be considered a part of the public API.
* @param wrapper the callback AsyncOutputStream
* @return the Publisher AsyncOutputStream
*/
public static AsyncOutputStream toAsyncOutputStream(final com.mongodb.async.client.gridfs.AsyncOutputStream wrapper) {
notNull("wrapper", wrapper);
return new AsyncOutputStream() {
@Override
public Publisher write(final ByteBuffer src) {
return new ObservableToPublisher(observe(new Block>() {
@Override
public void apply(final SingleResultCallback callback) {
wrapper.write(src, callback);
}
}));
}
@Override
public Publisher close() {
return new ObservableToPublisher(observe(new Block>() {
@Override
public void apply(final SingleResultCallback callback) {
wrapper.close(voidToSuccessCallback(callback));
}
}));
}
};
}
static com.mongodb.async.client.gridfs.AsyncInputStream toCallbackAsyncInputStream(final AsyncInputStream wrapped) {
notNull("wrapped", wrapped);
return new com.mongodb.async.client.gridfs.AsyncInputStream() {
@Override
public void read(final ByteBuffer dst, final SingleResultCallback callback) {
wrapped.read(dst).subscribe(new Subscriber() {
private Integer result = null;
@Override
public void onSubscribe(final Subscription s) {
s.request(1);
}
@Override
public void onNext(final Integer integer) {
result = integer;
}
@Override
public void onError(final Throwable t) {
callback.onResult(null, t);
}
@Override
public void onComplete() {
callback.onResult(result, null);
}
});
}
@Override
public void close(final SingleResultCallback callback) {
wrapped.close().subscribe(new Subscriber() {
@Override
public void onSubscribe(final Subscription s) {
s.request(1);
}
@Override
public void onNext(final Success success) {
}
@Override
public void onError(final Throwable t) {
callback.onResult(null, t);
}
@Override
public void onComplete() {
callback.onResult(null, null);
}
});
}
};
}
static com.mongodb.async.client.gridfs.AsyncOutputStream toCallbackAsyncOutputStream(final AsyncOutputStream wrapped) {
notNull("wrapped", wrapped);
return new com.mongodb.async.client.gridfs.AsyncOutputStream() {
@Override
public void write(final ByteBuffer src, final SingleResultCallback callback) {
wrapped.write(src).subscribe(new Subscriber() {
private Integer result = null;
@Override
public void onSubscribe(final Subscription s) {
s.request(1);
}
@Override
public void onNext(final Integer integer) {
result = integer;
}
@Override
public void onError(final Throwable t) {
callback.onResult(null, t);
}
@Override
public void onComplete() {
callback.onResult(result, null);
}
});
}
@Override
public void close(final SingleResultCallback callback) {
wrapped.close().subscribe(new Subscriber() {
@Override
public void onSubscribe(final Subscription s) {
s.request(1);
}
@Override
public void onNext(final Success success) {
}
@Override
public void onError(final Throwable t) {
callback.onResult(null, t);
}
@Override
public void onComplete() {
callback.onResult(null, null);
}
});
}
};
}
private GridFSAsyncStreamHelper() {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy