All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.async.client.gridfs.helpers.AsynchronousChannelHelper Maven / Gradle / Ivy

There is a newer version: 3.12.14
Show newest version
/*
 * Copyright 2008-present 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.async.client.gridfs.helpers;

import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.gridfs.AsyncInputStream;
import com.mongodb.async.client.gridfs.AsyncOutputStream;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousByteChannel;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;

import static com.mongodb.assertions.Assertions.notNull;

/**
 * A helper class to convert to {@link AsynchronousByteChannel} or {@link AsynchronousFileChannel} instances into {@link AsyncInputStream}
 * or {@link AsyncOutputStream} instances.
 *
 * 

Requires Java 7 or greater.

* @since 3.3 * @deprecated Prefer the Reactive Streams-based asynchronous driver (mongodb-driver-reactivestreams artifactId) */ @Deprecated public final class AsynchronousChannelHelper { /** * Converts a {@link AsynchronousByteChannel} into a {@link AsyncInputStream} * * @param asynchronousByteChannel the AsynchronousByteChannel * @return the AsyncInputStream */ public static AsyncInputStream channelToInputStream(final AsynchronousByteChannel asynchronousByteChannel) { notNull("asynchronousByteChannel", asynchronousByteChannel); return new AsyncInputStream() { @Override public void read(final ByteBuffer dst, final SingleResultCallback callback) { asynchronousByteChannel.read(dst, null, new CompletionHandler() { @Override public void completed(final Integer result, final Object attachment) { callback.onResult(result, null); } @Override public void failed(final Throwable exc, final Object attachment) { callback.onResult(null, exc); } }); } @Override public void skip(final long bytesToSkip, final SingleResultCallback callback) { notNull("callback", callback); callback.onResult(0L, null); } @Override public void close(final SingleResultCallback callback) { try { asynchronousByteChannel.close(); callback.onResult(null, null); } catch (Exception e) { callback.onResult(null, e); } } }; } /** * Converts a {@link AsynchronousFileChannel} into a {@link AsyncInputStream} * * @param asynchronousFileChannel the AsynchronousFileChannel * @return the AsyncInputStream */ public static AsyncInputStream channelToInputStream(final AsynchronousFileChannel asynchronousFileChannel) { notNull("asynchronousByteChannel", asynchronousFileChannel); return new AsyncInputStream() { private int position = 0; @Override public void read(final ByteBuffer dst, final SingleResultCallback callback) { asynchronousFileChannel.read(dst, position, null, new CompletionHandler() { @Override public void completed(final Integer result, final Object attachment) { position += result; callback.onResult(result, null); } @Override public void failed(final Throwable exc, final Object attachment) { callback.onResult(null, exc); } }); } @Override public void skip(final long bytesToSkip, final SingleResultCallback callback) { notNull("callback", callback); callback.onResult(0L, null); } @Override public void close(final SingleResultCallback callback) { try { asynchronousFileChannel.close(); callback.onResult(null, null); } catch (Exception e) { callback.onResult(null, e); } } }; } /** * Converts a {@link AsynchronousByteChannel} into a {@link AsyncOutputStream} * * @param asynchronousByteChannel the AsynchronousByteChannel * @return the AsyncOutputStream */ public static AsyncOutputStream channelToOutputStream(final AsynchronousByteChannel asynchronousByteChannel) { notNull("asynchronousByteChannel", asynchronousByteChannel); return new AsyncOutputStream() { @Override public void write(final ByteBuffer src, final SingleResultCallback callback) { asynchronousByteChannel.write(src, null, new CompletionHandler() { @Override public void completed(final Integer result, final Object attachment) { callback.onResult(result, null); } @Override public void failed(final Throwable exc, final Object attachment) { callback.onResult(null, exc); } }); } @Override public void close(final SingleResultCallback callback) { try { asynchronousByteChannel.close(); callback.onResult(null, null); } catch (IOException e) { callback.onResult(null, e); } } }; } /** * Converts a {@link AsynchronousFileChannel} into a {@link AsyncOutputStream} * * @param asynchronousFileChannel the AsynchronousFileChannel * @return the AsyncOutputStream */ public static AsyncOutputStream channelToOutputStream(final AsynchronousFileChannel asynchronousFileChannel) { notNull("asynchronousByteChannel", asynchronousFileChannel); return new AsyncOutputStream() { private int position = 0; @Override public void write(final ByteBuffer src, final SingleResultCallback callback) { asynchronousFileChannel.write(src, position, null, new CompletionHandler() { @Override public void completed(final Integer result, final Object attachment) { position += result; callback.onResult(result, null); } @Override public void failed(final Throwable exc, final Object attachment) { callback.onResult(null, exc); } }); } @Override public void close(final SingleResultCallback callback) { try { asynchronousFileChannel.close(); callback.onResult(null, null); } catch (IOException e) { callback.onResult(null, e); } } }; } private AsynchronousChannelHelper() { } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy