View Javadoc
1   package au.gov.amsa.streams;
2   
3   import java.io.File;
4   import java.io.Reader;
5   import java.nio.charset.Charset;
6   
7   import rx.Observable;
8   import rx.functions.Func1;
9   
10  /**
11   * Utilities for stream processing of lines of text from
12   * <ul>
13   * <li>sockets</li>
14   * </ul>
15   */
16  public final class Strings {
17  
18      public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
19  
20      /**
21       * Returns null if input is null otherwise returns input.toString().trim().
22       */
23      public static Func1<Object, String> TRIM = new Func1<Object, String>() {
24  
25          @Override
26          public String call(Object input) {
27              if (input == null)
28                  return null;
29              else
30                  return input.toString().trim();
31          }
32      };
33  
34      public static Observable<String> from(final Reader reader, final int size) {
35          return new OnSubscribeReader(reader, size).toObservable();
36          // return StringObservable.from(reader, size);
37      }
38  
39      public static Observable<String> from(Reader reader) {
40          return from(reader, 8192);
41      }
42  
43      public static Observable<String> lines(Reader reader) {
44          return from(reader, 8192).compose(com.github.davidmoten.rx.Transformers.split("\n"));
45      }
46  
47      public static Observable<String> lines(File file) {
48          return from(file).compose(com.github.davidmoten.rx.Transformers.split("\n"));
49      }
50  
51      public static Observable<String> from(File file) {
52          return from(file, DEFAULT_CHARSET);
53      }
54  
55      public static Observable<String> split(Observable<String> o, String pattern) {
56          return o.compose(com.github.davidmoten.rx.Transformers.split(pattern));
57      }
58  
59      public static Observable<String> split(Observable<String> o, int maxItemLength, String pattern, int maxPatternLength) {
60          return o.compose(com.github.davidmoten.rx.Transformers.split(maxItemLength, pattern, maxPatternLength));
61      }
62  
63      
64      public static Observable<String> from(final File file, final Charset charset) {
65          return com.github.davidmoten.rx.Strings.from(file, charset);
66      }
67  
68  }