rx.observables.StringObservable Java Examples

The following examples show how to use rx.observables.StringObservable. 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: LinesBackpressure.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@Test
public void decode() {
    byte[] bytes = new byte[16];
    Arrays.fill(bytes, (byte)65);

    byte[] bytes2 = new byte[16];
    Arrays.fill(bytes, (byte)65);
    bytes[15] = 10;

    Observable<byte[]> data = Observable.just(bytes, bytes, bytes2).repeat(500);

    StringObservable.decode(data, StandardCharsets.UTF_8)
    .observeOn(Schedulers.computation(), false, 1)
    .toBlocking()
    .subscribe(System.out::println);
}
 
Example #2
Source File: RxStringOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenStringObservable_whenFromInputStream_ThenSuccessfull()
{
    //given
    ByteArrayInputStream is = new ByteArrayInputStream("Lorem ipsum loream, Lorem ipsum lore".getBytes(StandardCharsets.UTF_8));
    TestSubscriber<String> subscriber = TestSubscriber.create();

    // when
    StringObservable.decode(StringObservable.from(is), StandardCharsets.UTF_8)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValues("Lorem ipsum loream, Lorem ipsum lore");
}
 
Example #3
Source File: RxStringOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenStringObservable_whenEncodingString_ThenSuccessfullObtainingByteStream()
{
    //given
    Observable<String> sourceObservable = Observable.just("Lorem ipsum loream");
    TestSubscriber<byte[]> subscriber = TestSubscriber.create();

    // when
    StringObservable.encode(sourceObservable, StandardCharsets.UTF_8)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.getOnNextEvents()
        .stream()
        .forEach(bytes -> Assert.assertTrue(Arrays.equals(bytes, "Lorem ipsum loream".getBytes(StandardCharsets.UTF_8))));
}
 
Example #4
Source File: RxStringOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenStringObservable_whenConcatenatingStrings_ThenSuccessfullObtainingSingleString()
{
    //given
    Observable<String> sourceObservable = Observable.just("Lorem ipsum loream","Lorem ipsum lore");
    TestSubscriber<String> subscriber = TestSubscriber.create();

    // when
    StringObservable.stringConcat(sourceObservable)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValues("Lorem ipsum loreamLorem ipsum lore");
}
 
Example #5
Source File: RxStringOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenStringObservable_whenDecodingByteArray_ThenSuccessfullObtainingStringStream()
{
    //given
    Observable<byte[]> sourceObservable = Observable.just("Lorem ipsum loream".getBytes(StandardCharsets.UTF_8));
    TestSubscriber<String> subscriber = TestSubscriber.create();

    // when
    StringObservable.decode(sourceObservable, StandardCharsets.UTF_8)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValues("Lorem ipsum loream");
}
 
Example #6
Source File: RxStringOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenStringObservable_whenStringSplitted_ThenSuccessfullObtainingStringsStream()
{
    //given
    Observable<String> sourceObservable = Observable.just("Lorem ipsum loream,Lorem ipsum lore");
    TestSubscriber<String> subscriber = TestSubscriber.create();

    // when
    StringObservable.split(sourceObservable,",")
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(2);
    subscriber.assertValues("Lorem ipsum loream", "Lorem ipsum lore");
}
 
Example #7
Source File: RxStringOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenStringObservable_whenSplittingByLine_ThenSuccessfullObtainingStringsStream() {
    //given
    Observable<String> sourceObservable = Observable.just("Lorem ipsum loream\nLorem ipsum lore");
    TestSubscriber<String> subscriber = TestSubscriber.create();

    // when
    StringObservable.byLine(sourceObservable)
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(2);
    subscriber.assertValues("Lorem ipsum loream", "Lorem ipsum lore");
}
 
Example #8
Source File: RxStringOperatorsUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenStringObservable_whenJoiningStrings_ThenSuccessfullObtainingSingleString() {
    //given
    Observable<String> sourceObservable = Observable.just("Lorem ipsum loream","Lorem ipsum lore");
    TestSubscriber<String> subscriber = TestSubscriber.create();

    // when
    StringObservable.join(sourceObservable,",")
        .subscribe(subscriber);

    // then
    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(1);
    subscriber.assertValues("Lorem ipsum loream,Lorem ipsum lore");
}
 
Example #9
Source File: RxDocumentServiceRequest.java    From azure-cosmosdb-java with MIT License 5 votes vote down vote up
/** Creates a DocumentServiceRequest with a stream.
 *
 * @param operation    the operation type.
 * @param resourceType the resource type.
 * @param relativePath the relative URI path.
 * @param inputStream  the input stream.
 * @param headers      the request headers.
 * @return the created document service request.
 */
public static RxDocumentServiceRequest create(OperationType operation,
        ResourceType resourceType,
        String relativePath,
        InputStream inputStream,
        Map<String, String> headers) {
    // StringObservable is mis-named. It doesn't make any assumptions on character set
    // and handles bytes only
    return new RxDocumentServiceRequest(operation, resourceType, relativePath, StringObservable.from(inputStream), headers, AuthorizationTokenType.PrimaryMasterKey);
}
 
Example #10
Source File: RxDocumentServiceRequest.java    From azure-cosmosdb-java with MIT License 5 votes vote down vote up
/** Creates a DocumentServiceRequest with a stream.
 *
 * @param operation    the operation type.
 * @param resourceType the resource type.
 * @param relativePath the relative URI path.
 * @param inputStream  the input stream.
 * @param headers      the request headers.
 * @param authorizationTokenType      the request authorizationTokenType.
 * @return the created document service request.
 */
public static RxDocumentServiceRequest create(OperationType operation,
        ResourceType resourceType,
        String relativePath,
        InputStream inputStream,
        Map<String, String> headers,
        AuthorizationTokenType authorizationTokenType) {
    // StringObservable is mis-named. It doesn't make any assumptions on character set
    // and handles bytes only
    return new RxDocumentServiceRequest(operation, resourceType, relativePath, StringObservable.from(inputStream), headers, authorizationTokenType);
}
 
Example #11
Source File: RxDocumentServiceRequest.java    From azure-cosmosdb-java with MIT License 5 votes vote down vote up
/**
 * Creates a DocumentServiceRequest with a query.
 *
 * @param resourceType           the resource type.
 * @param relativePath           the relative URI path.
 * @param querySpec              the query.
 * @param queryCompatibilityMode the QueryCompatibilityMode mode.
 * @param headers                the request headers.
 * @return the created document service request.
 */
public static RxDocumentServiceRequest create(ResourceType resourceType,
        String relativePath,
        SqlQuerySpec querySpec,
        QueryCompatibilityMode queryCompatibilityMode,
        Map<String, String> headers) {
    OperationType operation;
    String queryText;
    switch (queryCompatibilityMode) {
    case SqlQuery:
        if (querySpec.getParameters() != null && querySpec.getParameters().size() > 0) {
            throw new IllegalArgumentException(
                    String.format("Unsupported argument in query compatibility mode '{%s}'",
                            queryCompatibilityMode.name()));
        }

        operation = OperationType.SqlQuery;
        queryText = querySpec.getQueryText();
        break;

    case Default:
    case Query:
    default:
        operation = OperationType.Query;
        queryText = querySpec.toJson();
        break;
    }

    Observable<byte[]> body = StringObservable.encode(Observable.just(queryText), StandardCharsets.UTF_8);
    return new RxDocumentServiceRequest(operation, resourceType, relativePath, body, headers, AuthorizationTokenType.PrimaryMasterKey);
}
 
Example #12
Source File: Benchmarks.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String splitRxJavaStringTake5() {
    return Observable.just(lines).compose(new Transformer<String, String>() {
        @Override
        public Observable<String> call(Observable<String> o) {
            return StringObservable.split(o, "\n");
        }
    }).take(5).last().toBlocking().last();
}
 
Example #13
Source File: Benchmarks.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String splitRxJavaString() {
    return Observable.just(lines).compose(new Transformer<String, String>() {
        @Override
        public Observable<String> call(Observable<String> o) {
            return StringObservable.split(o, "\n");
        }
    }).last().toBlocking().last();
}
 
Example #14
Source File: StringSplitTest.java    From rtree-3d with Apache License 2.0 5 votes vote down vote up
public void testSplitOnStreamThatThrowsExceptionImmediately() {
    RuntimeException ex = new RuntimeException("boo");
    try {
        StringObservable.split(Observable.<String> error(ex), "\n").count().toBlocking()
                .single();
        fail();
    } catch (RuntimeException e) {
        assertEquals(ex, e);
    }
}
 
Example #15
Source File: StringSplitTest.java    From rtree-3d with Apache License 2.0 4 votes vote down vote up
public void testSplitOnEmptyStream() {
    assertEquals(0, (int) StringObservable.split(Observable.<String> empty(), "\n").count()
            .toBlocking().single());
}
 
Example #16
Source File: InputStreamBody.java    From datamill with ISC License 4 votes vote down vote up
@Override
public Observable<byte[]> asChunks() {
    return StringObservable.from(inputStream)
            .doAfterTerminate(completionHandler != null ? completionHandler : () -> {});
}