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

xapi.dev.resource.impl.StringDataResource Maven / Gradle / Ivy

Go to download

Everything needed to run a comprehensive dev environment. Just type X_ and pick a service from autocomplete; new dev modules will be added as they are built. The only dev service not included in the uber jar is xapi-dev-maven, as it includes all runtime dependencies of maven, adding ~4 seconds to build time, and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).

The newest version!
package xapi.dev.resource.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;

import xapi.dev.resource.api.ClasspathResource;
import xapi.util.X_Debug;
import xapi.util.X_Util;

public class StringDataResource extends DelegateClasspathResource{

  public StringDataResource(ClasspathResource source) {
    super(source);
  }

  /**
   * @throws IOException
   */
  public Iterable readLines() throws IOException {
    return new Iterable(){
      @Override
      public Iterator iterator() {
        try {
          // reader is closed by StringReader
          BufferedReader reader = new BufferedReader(new InputStreamReader(open()));
          return new StringReader(reader);
        } catch (IOException e) {
          throw X_Util.rethrow(e);
        }
      }
    };
  }

  public String readAll() {
    StringBuilder b = new StringBuilder();
    try {
      for (String line : readLines())
        b.append(line).append('\n');
      return b.toString();
    } catch (IOException e) {
      throw X_Debug.rethrow(e);
    }
  }

}
class StringReader implements Iterator {

  private BufferedReader reader;
  private String next;

  public StringReader(BufferedReader reader) {
    this.reader = reader;
  }

  @Override
  public boolean hasNext() {
    try {
      next = reader.readLine();
      if (next == null) {
        reader.close();
        return false;
      }
      return true;
    }catch(IOException e) {
      throw X_Util.rethrow(e);
    }
  }

  @Override
  public String next() {
    return next;
  }

  @Override
  public void remove() {
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy