Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014, The OpenNMS Group
   * 
   * 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 org.opennms.newts.gsod;
 
 
 import rx.Observable;
 import rx.Subscriber;
 
 public class FileObservable {
     
     public static Observable<PathfileTreeWalker(final Path root) {
         
         return Observable.create(new OnSubscribe<Path>() {
             @Override
             public void call(final Subscriber<? super Patho) {
                 SimpleFileVisitor<Pathvisitor = new SimpleFileVisitor<Path>() {
 
                     @Override
                     public FileVisitResult visitFile(Path fileBasicFileAttributes attrsthrows IOException {
                         if (o.isUnsubscribed()) return .;
                         o.onNext(file);
                         return .;
                     }
 
                     @Override
                     public FileVisitResult visitFileFailed(Path fileIOException excthrows IOException {
                         if (o.isUnsubscribed()) return .;
                         o.onError(exc);
                         return .;
                     }
                     
                 };
                 try {
                     Files.walkFileTree(rootvisitor);
                     if (o.isUnsubscribed()) return;
                     o.onCompleted();
                 } catch (IOException e) {
                     if (o.isUnsubscribed()) return;
                     o.onError(e);
                 }
             }
 
         });
         
     }
     
     
     public static Observable<Stringlines(final Path path) {
         return Observable.create(new OnSubscribe<String>() {
 
             @Override
             public void call(Subscriber<? super Strings) {
                 try (BufferedReader in = fileReader(path))
                 {
                     String line = null;
                     while ((line = in.readLine()) != null) {
                         if (s.isUnsubscribed()) return;
                         s.onNext(line);
                     }
                     s.onCompleted();
                     
                 } catch (Exception e) {
                     if (!s.isUnsubscribed()) s.onError(e);
                 }
             }
             
         });
     }
     
     public static Observable<StringunzipLines(final Path path) {
         return Observable.create(new OnSubscribe<String>() {
 
            @Override
            public void call(Subscriber<? super Strings) {
                try (BufferedReader in = zippedFileReader(path))
                {
                    String line = null;
                    while ((line = in.readLine()) != null) {
                        if (s.isUnsubscribed()) return;
                        s.onNext(line);
                    }
                    s.onCompleted();
                    
                } catch (Exception e) {
                    if (!s.isUnsubscribed()) s.onError(e);
                }
            }
            
        });
        
    }
    
    public static Func1<PathObservable<String>> lines() {
        return new Func1<PathObservable<String>>() {
            @Override
            public Observable<Stringcall(Path path) {
                return FileObservable.unzipLines(path);
            }
        };
    }
    
    private static BufferedReader fileReader(final Path path)  throws IOException {
        return Files.newBufferedReader(path, Charset.forName("US-ASCII"));
    }
    private static BufferedReader zippedFileReader(final Path path)  throws IOException {
        InputStream gzipStream = new GZIPInputStream(new FileInputStream(path.toFile()));
        return new BufferedReader(new InputStreamReader(gzipStream"US-ASCII"));
    }
New to GrepCode? Check out our FAQ X