Java Code Examples for io.reactivex.Flowable#using()

The following examples show how to use io.reactivex.Flowable#using() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Serialized.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
public <T> Flowable<T> read(final Class<T> cls, final File file, final int bufferSize) {
    Callable<Input> resourceFactory = new Callable<Input>() {
        @Override
        public Input call() throws FileNotFoundException {
            return new Input(new FileInputStream(file), bufferSize);
        }
    };
    Function<Input, Flowable<T>> flowableFactory = new Function<Input, Flowable<T>>() {

        @Override
        public Flowable<T> apply(final Input input) {
            return read(cls, input);
        }
    };
    Consumer<Input> disposeAction = Consumers.close();
    return Flowable.using(resourceFactory, flowableFactory, disposeAction, true);
}
 
Example 2
Source File: Serialized.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
public <T> Flowable<T> write(final Flowable<T> source, final File file, final boolean append,
        final int bufferSize) {
    Callable<Output> resourceFactory = new Callable<Output>() {
        @Override
        public Output call() throws FileNotFoundException {
            return new Output(new FileOutputStream(file, append), bufferSize);
        }
    };
    Function<Output, Flowable<T>> flowableFactory = new Function<Output, Flowable<T>>() {

        @Override
        public Flowable<T> apply(final Output output) {
            return source.doOnNext(new Consumer<T>() {
                @Override
                public void accept(T t) {
                    kryo.writeObject(output, t);
                }
            });
        }
    };
    Consumer<Output> disposeAction = Consumers.close();
    return Flowable.using(resourceFactory, flowableFactory, disposeAction, true);
}
 
Example 3
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
private static <T1> Flowable<Notification<CallableResultSet1<T1>>> createWithOneResultSet(Connection con,
        String sql, Flowable<List<Object>> parameterGroups, List<ParameterPlaceholder> parameterPlaceholders,
        Function<? super ResultSet, ? extends T1> f1, int fetchSize) {
    log.debug("Update.create {}", sql);
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<CallableResultSet1<T1>>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> {
                        List<Object> outputValues = executeAndReturnOutputValues(parameterPlaceholders, stmt,
                                parameters);
                        Flowable<T1> flowable1 = createFlowable(stmt, f1);
                        return Single.just(new CallableResultSet1<T1>(outputValues, flowable1)).toFlowable();
                    }) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example 4
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
private static <T1, T2> Flowable<Notification<CallableResultSet2<T1, T2>>> createWithTwoResultSets(Connection con,
        String sql, Flowable<List<Object>> parameterGroups, List<ParameterPlaceholder> parameterPlaceholders,
        Function<? super ResultSet, ? extends T1> f1, Function<? super ResultSet, ? extends T2> f2, int fetchSize) {
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<CallableResultSet2<T1, T2>>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> {
                        List<Object> outputValues = executeAndReturnOutputValues(parameterPlaceholders, stmt,
                                parameters);
                        final Flowable<T1> flowable1 = createFlowable(stmt, f1);
                        stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
                        final Flowable<T2> flowable2 = createFlowable(stmt, f2);
                        return Single.just(new CallableResultSet2<T1, T2>(outputValues, flowable1, flowable2))
                                .toFlowable();
                    }) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example 5
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
private static <T1, T2, T3> Flowable<Notification<CallableResultSet3<T1, T2, T3>>> createWithThreeResultSets(
        Connection con, String sql, Flowable<List<Object>> parameterGroups,
        List<ParameterPlaceholder> parameterPlaceholders, Function<? super ResultSet, ? extends T1> f1,
        Function<? super ResultSet, ? extends T2> f2, Function<? super ResultSet, ? extends T3> f3, int fetchSize) {
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<CallableResultSet3<T1, T2, T3>>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> {
                        List<Object> outputValues = executeAndReturnOutputValues(parameterPlaceholders, stmt,
                                parameters);
                        final Flowable<T1> flowable1 = createFlowable(stmt, f1);
                        stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
                        final Flowable<T2> flowable2 = createFlowable(stmt, f2);
                        stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
                        final Flowable<T3> flowable3 = createFlowable(stmt, f3);
                        return Single.just(
                                new CallableResultSet3<T1, T2, T3>(outputValues, flowable1, flowable2, flowable3))
                                .toFlowable();
                    }) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example 6
Source File: Serialized.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
/**
 * Writes the source stream to the given file in given append mode and using
 * the given buffer size.
 * 
 * @param source
 *            flowable stream to write
 * @param file
 *            file to write to
 * @param append
 *            if true writes are appended to file otherwise overwrite the
 *            file
 * @param bufferSize
 *            the buffer size in bytes to use.
 * @param <T>
 *            the generic type of the input stream
 * @return re-emits the input stream
 */
public static <T extends Serializable> Flowable<T> write(final Flowable<T> source, final File file,
        final boolean append, final int bufferSize) {
    Callable<ObjectOutputStream> resourceFactory = new Callable<ObjectOutputStream>() {
        @Override
        public ObjectOutputStream call() throws IOException {
            return new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file, append), bufferSize));
        }
    };
    Function<ObjectOutputStream, Flowable<T>> flowableFactory = new Function<ObjectOutputStream, Flowable<T>>() {

        @Override
        public Flowable<T> apply(ObjectOutputStream oos) {
            return write(source, oos);
        }
    };
    Consumer<ObjectOutputStream> disposeAction = Consumers.close();
    return Flowable.using(resourceFactory, flowableFactory, disposeAction, true);
}
 
Example 7
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
private static Flowable<Notification<CallableResultSetN>> createWithNResultSets(Connection con, String sql,
        Flowable<List<Object>> parameterGroups, List<ParameterPlaceholder> parameterPlaceholders,
        List<Function<? super ResultSet, ?>> functions, int fetchSize) {
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<CallableResultSetN>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> {
                        List<Object> outputValues = executeAndReturnOutputValues(parameterPlaceholders, stmt,
                                parameters);
                        List<Flowable<?>> flowables = Lists.newArrayList();
                        int i = 0;
                        do {
                            Function<? super ResultSet, ?> f = functions.get(i);
                            flowables.add(createFlowable(stmt, f));
                            i++;
                        } while (stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT));
                        return Single.just(new CallableResultSetN(outputValues, flowables)).toFlowable();
                    }) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example 8
Source File: FlowableServerSocket.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
private static Flowable<byte[]> createSocketFlowable(final Socket socket, long timeoutMs, final int bufferSize) {
    setTimeout(socket, timeoutMs);
    return Flowable.using( //
            new Callable<InputStream>() {
                @Override
                public InputStream call() throws Exception {
                    return socket.getInputStream();
                }
            }, //
            new Function<InputStream, Flowable<byte[]>>() {
                @Override
                public Flowable<byte[]> apply(InputStream is) {
                    return Bytes.from(is, bufferSize);
                }
            }, //
            Consumers.close(), //
            true);
}
 
Example 9
Source File: Bytes.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
public static Flowable<byte[]> from(final File file, final int size) {
    Callable<InputStream> resourceFactory = new Callable<InputStream>() {

        @Override
        public InputStream call() throws FileNotFoundException {
            return new BufferedInputStream(new FileInputStream(file), size);
        }
    };
    Function<InputStream, Flowable<byte[]>> observableFactory = new Function<InputStream, Flowable<byte[]>>() {

        @Override
        public Flowable<byte[]> apply(InputStream is) {
            return from(is, size);
        }
    };
    return Flowable.using(resourceFactory, observableFactory, InputStreamCloseHolder.INSTANCE, true);
}
 
Example 10
Source File: FlowableServerSocket.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
public static Flowable<Flowable<byte[]>> create(final Callable<? extends ServerSocket> serverSocketFactory,
        final int timeoutMs, final int bufferSize, Action preAcceptAction, int acceptTimeoutMs,
        Predicate<? super Socket> acceptSocket) {
    Function<ServerSocket, Flowable<Flowable<byte[]>>> FlowableFactory = createFlowableFactory(timeoutMs,
            bufferSize, preAcceptAction, acceptSocket);
    return Flowable.<Flowable<byte[]>, ServerSocket>using( //
            createServerSocketFactory(serverSocketFactory, acceptTimeoutMs), //
            FlowableFactory, //
            new Consumer<ServerSocket>() {
                // Note that in java 1.6, ServerSocket does not implement
                // Closeable
                @Override
                public void accept(ServerSocket ss) throws Exception {
                    ss.close();
                }
            }, //
            true);
}
 
Example 11
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
private static <T> Flowable<Notification<T>> createWithParameters(Connection con, String sql,
        Flowable<List<Object>> parameterGroups, List<ParameterPlaceholder> parameterPlaceholders,
        BiFunction<NamedCallableStatement, List<Object>, Single<T>> single) {
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<T>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> single.apply(stmt, parameters).toFlowable()) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example 12
Source File: Strings.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public static Flowable<String> from(final Callable<Reader> readerFactory) {
    Function<Reader, Flowable<String>> flowableFactory = new Function<Reader, Flowable<String>>() {
        @Override
        public Flowable<String> apply(Reader reader) {
            return from(reader);
        }
    };
    return Flowable.using(readerFactory, flowableFactory, DisposeActionHolder.INSTANCE, true);
}
 
Example 13
Source File: Bytes.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public static Flowable<ZippedEntry> unzip(final File file) {
    Callable<ZipInputStream> resourceFactory = new Callable<ZipInputStream>() {
        @Override
        public ZipInputStream call() throws FileNotFoundException {
            return new ZipInputStream(new FileInputStream(file));
        }
    };
    Function<ZipInputStream, Flowable<ZippedEntry>> observableFactory = ZipHolder.OBSERVABLE_FACTORY;
    Consumer<ZipInputStream> disposeAction = ZipHolder.DISPOSER;
    return Flowable.using(resourceFactory, observableFactory, disposeAction);
}
 
Example 14
Source File: Update.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 5 votes vote down vote up
public static <T> Flowable<T> create(Callable<Connection> connectionFactory, List<Object> parameters, String sql,
                                     Function<? super ResultSet, T> mapper) {
    Callable<PreparedStatement> resourceFactory = () -> {
        Connection con = connectionFactory.call();
        // TODO set parameters
        return con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
    };
    Function<PreparedStatement, Flowable<T>> singleFactory = ps -> create(ps, mapper);
    Consumer<PreparedStatement> disposer = Update::closeAll;
    return Flowable.using(resourceFactory, singleFactory, disposer);
}
 
Example 15
Source File: Call.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
private static <T1, T2, T3, T4> Flowable<Notification<CallableResultSet4<T1, T2, T3, T4>>> createWithFourResultSets(
        Connection con, String sql, Flowable<List<Object>> parameterGroups,
        List<ParameterPlaceholder> parameterPlaceholders, Function<? super ResultSet, ? extends T1> f1,
        Function<? super ResultSet, ? extends T2> f2, Function<? super ResultSet, ? extends T3> f3,
        Function<? super ResultSet, ? extends T4> f4, int fetchSize) {
    Callable<NamedCallableStatement> resourceFactory = () -> Util.prepareCall(con, sql, parameterPlaceholders);
    final Function<NamedCallableStatement, Flowable<Notification<CallableResultSet4<T1, T2, T3, T4>>>> flowableFactory = //
            stmt -> parameterGroups //
                    .flatMap(parameters -> {
                        List<Object> outputValues = executeAndReturnOutputValues(parameterPlaceholders, stmt,
                                parameters);
                        final Flowable<T1> flowable1 = createFlowable(stmt, f1);
                        stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
                        final Flowable<T2> flowable2 = createFlowable(stmt, f2);
                        stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
                        final Flowable<T3> flowable3 = createFlowable(stmt, f3);
                        stmt.stmt.getMoreResults(Statement.KEEP_CURRENT_RESULT);
                        final Flowable<T4> flowable4 = createFlowable(stmt, f4);
                        return Single.just(new CallableResultSet4<T1, T2, T3, T4>(outputValues, flowable1,
                                flowable2, flowable3, flowable4)).toFlowable();
                    }) //
                    .materialize() //
                    .doOnComplete(() -> Util.commit(stmt.stmt)) //
                    .doOnError(e -> Util.rollback(stmt.stmt));
    Consumer<NamedCallableStatement> disposer = Util::closeCallableStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, true);
}
 
Example 16
Source File: Select.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
static <T> Flowable<T> create(Connection con, String sql,
                              Flowable<List<Object>> parameterGroups, int fetchSize,
                              Function<? super ResultSet, T> mapper, boolean eagerDispose, int queryTimeoutSec) {
    log.debug("Select.create called with con={}", con);
    Callable<NamedPreparedStatement> initialState = () -> Util.prepare(con, fetchSize, sql, queryTimeoutSec);
    Function<NamedPreparedStatement, Flowable<T>> observableFactory = ps -> parameterGroups
            .flatMap(parameters -> create(ps.ps, parameters, mapper, ps.names, sql, fetchSize, queryTimeoutSec),
                    true, 1);
    Consumer<NamedPreparedStatement> disposer = Util::closePreparedStatementAndConnection;
    return Flowable.using(initialState, observableFactory, disposer, eagerDispose);
}
 
Example 17
Source File: Update.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
private static <T> Flowable<T> createReturnGeneratedKeys(Connection con,
        Flowable<List<Object>> parameterGroups, String sql,
        Function<? super ResultSet, T> mapper, boolean eagerDispose) {
    Callable<NamedPreparedStatement> resourceFactory = () -> Util
            .prepareReturnGeneratedKeys(con, sql);
    Function<NamedPreparedStatement, Flowable<T>> obsFactory = ps -> parameterGroups
            .flatMap(parameters -> create(ps, parameters, mapper), true, 1) //
            .doOnComplete(() -> Util.commit(ps.ps)) //
            .doOnError(e -> Util.rollback(ps.ps));
    Consumer<NamedPreparedStatement> disposer = Util::closePreparedStatementAndConnection;
    return Flowable.using(resourceFactory, obsFactory, disposer, eagerDispose);
}
 
Example 18
Source File: Update.java    From rxjava2-jdbc with Apache License 2.0 4 votes vote down vote up
private static Flowable<Notification<Integer>> create(Connection con, String sql,
                                                      Flowable<List<Object>> parameterGroups, int batchSize, boolean eagerDispose, int queryTimeoutSec) {
    log.debug("Update.create {}", sql);
    Callable<NamedPreparedStatement> resourceFactory = () -> Util.prepare(con, sql, queryTimeoutSec);
    final Function<NamedPreparedStatement, Flowable<Notification<Integer>>> flowableFactory;
    if (batchSize == 0) {
        flowableFactory = ps -> parameterGroups //
                .flatMap(parameters -> create(ps, Util.toParameters(parameters), sql)
                        .toFlowable()) //
                .materialize() //
                .doOnComplete(() -> Util.commit(ps.ps)) //
                .doOnError(e -> Util.rollback(ps.ps));
    } else {
        flowableFactory = ps -> {
            int[] count = new int[1];
            return parameterGroups //
                    .flatMap(parameters -> {
                        List<Parameter> params = Util.toParameters(parameters);
                        if (Util.hasCollection(params)) {
                            return create(ps, params, sql).toFlowable();
                        } else {
                            Util.incrementCounter(ps.ps.getConnection());
                            count[0] += 1;
                            Flowable<Integer> result;
                            if (count[0] == batchSize) {
                                count[0] = 0;
                                result = createExecuteBatch(ps, parameters);
                            } else {
                                result = createAddBatch(ps, parameters).toFlowable();
                            }
                            return result;
                        }
                    }) //
                    .materialize() //
                    .flatMap(n -> executeFinalBatch(ps, n, count[0] > 0)) //
                    .doOnComplete(() -> Util.commit(ps.ps)) //
                    .doOnError(e -> Util.rollback(ps.ps));
        };
    }
    Consumer<NamedPreparedStatement> disposer = Util::closePreparedStatementAndConnection;
    return Flowable.using(resourceFactory, flowableFactory, disposer, eagerDispose);
}
 
Example 19
Source File: Serialized.java    From rxjava2-extras with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the deserialized objects from the given {@link File} as a
 * {@link Flowable} stream. Uses buffer of size <code>bufferSize</code>
 * buffer reads from the File.
 * 
 * @param file
 *            the input file
 * @param bufferSize
 *            the buffer size for reading bytes from the file.
 * @param <T>
 *            the generic type of the deserialized objects returned in the
 *            stream
 * @return the stream of deserialized objects from the {@link InputStream}
 *         as a {@link Flowable}.
 */
public static <T extends Serializable> Flowable<T> read(final File file, final int bufferSize) {
    Callable<ObjectInputStream> resourceFactory = new Callable<ObjectInputStream>() {
        @Override
        public ObjectInputStream call() throws IOException {
            return new ObjectInputStream(new BufferedInputStream(new FileInputStream(file), bufferSize));
        }
    };
    @SuppressWarnings("unchecked")
    Function<ObjectInputStream, Flowable<T>> flowableFactory = (Function<ObjectInputStream, Flowable<T>>) (Function<?, ?>) ObjectInputStreamFlowableFactoryHolder.INSTANCE;
    Consumer<ObjectInputStream> disposeAction = Consumers.close();
    return Flowable.using(resourceFactory, flowableFactory, disposeAction, true);
}