Exemplo n.º 1
0
Arquivo: IO.java Projeto: nremond/boon
  public static CharBuf read(Reader input, CharBuf charBuf, final int bufSize) {

    if (charBuf == null) {
      charBuf = CharBuf.create(bufSize);
    } else {
      charBuf.readForRecycle();
    }

    try {

      char[] buffer = charBuf.toCharArray();
      int size = input.read(buffer);
      if (size != -1) {
        charBuf._len(size);
      }
      if (size < buffer.length) {
        return charBuf;
      }

      copy(input, charBuf);

    } catch (IOException e) {
      Exceptions.handle(e);
    } finally {
      try {
        input.close();
      } catch (IOException e) {
        Exceptions.handle(e);
      }
    }

    return charBuf;
  }
Exemplo n.º 2
0
Arquivo: IO.java Projeto: nremond/boon
 public static byte[] input(String fileName) {
   try {
     return input(Files.newInputStream(IO.path(fileName)));
   } catch (IOException e) {
     return Exceptions.handle(byte[].class, e);
   }
 }
Exemplo n.º 3
0
  @Override
  public <T> Supplier<T> getSupplier(final Class<T> type, final String name) {

    ProviderInfo nullInfo = null;

    try {
      Set<ProviderInfo> set = Sets.set(supplierNameMap.getAll(name));

      for (ProviderInfo info : set) {

        if (info.type() == null) {
          nullInfo = info;
          continue;
        }
        if (type.isAssignableFrom(info.type())) {
          return (Supplier<T>) info.supplier();
        }
      }

      return (Supplier<T>)
          (nullInfo != null
              ? nullInfo.supplier()
              : new Supplier<T>() {
                @Override
                public T get() {
                  return null;
                }
              });

    } catch (Exception e) {
      Exceptions.handle(e);
      return null;
    }
  }
Exemplo n.º 4
0
Arquivo: IO.java Projeto: nremond/boon
 public static void eachLine(File file, EachLine eachLine) {
   try (FileReader reader = new FileReader(file)) {
     eachLine(reader, eachLine);
   } catch (Exception ex) {
     Exceptions.handle(List.class, ex);
   }
 }
Exemplo n.º 5
0
Arquivo: IO.java Projeto: nremond/boon
 public static List<String> readLines(File file) {
   try (FileReader reader = new FileReader(file)) {
     return readLines(reader);
   } catch (Exception ex) {
     return Exceptions.handle(List.class, ex);
   }
 }
Exemplo n.º 6
0
Arquivo: IO.java Projeto: nremond/boon
 public static String read(File file) {
   try (Reader reader = new FileReader(file)) {
     return read(reader);
   } catch (Exception ex) {
     return Exceptions.handle(String.class, ex);
   }
 }
Exemplo n.º 7
0
Arquivo: IO.java Projeto: nremond/boon
  public static void write(Path file, byte[] contents) {
    try {
      Files.write(file, contents);

    } catch (Exception ex) {
      Exceptions.handle(ex);
    }
  }
Exemplo n.º 8
0
Arquivo: IO.java Projeto: nremond/boon
  public static void write(OutputStream out, String content, Charset charset) {

    try (OutputStream o = out) {
      o.write(content.getBytes(charset));
    } catch (Exception ex) {
      Exceptions.handle(ex);
    }
  }
Exemplo n.º 9
0
Arquivo: IO.java Projeto: nremond/boon
  public static CharBuf read(InputStream inputStream, CharBuf charBuf, Charset charset) {

    try (Reader reader = new InputStreamReader(inputStream, charset)) {
      return read(reader, charBuf);
    } catch (Exception ex) {
      return Exceptions.handle(CharBuf.class, ex);
    }
  }
Exemplo n.º 10
0
Arquivo: IO.java Projeto: nremond/boon
  public static char[] readCharBuffer(InputStream inputStream) {

    try (Reader reader = new InputStreamReader(inputStream)) {
      return readCharBuffer(reader);
    } catch (Exception ex) {
      return Exceptions.handle(char[].class, ex);
    }
  }
Exemplo n.º 11
0
Arquivo: IO.java Projeto: nremond/boon
  public static String read(InputStream inputStream) {

    try (Reader reader = new InputStreamReader(inputStream, DEFAULT_CHARSET)) {
      return read(reader);
    } catch (Exception ex) {
      return Exceptions.handle(String.class, ex);
    }
  }
Exemplo n.º 12
0
Arquivo: IO.java Projeto: nremond/boon
  public static String readCharBuffer(InputStream inputStream, Charset charset) {

    try (Reader reader = new InputStreamReader(inputStream, charset)) {
      return read(reader);
    } catch (Exception ex) {
      return Exceptions.handle(String.class, ex);
    }
  }
Exemplo n.º 13
0
Arquivo: IO.java Projeto: nremond/boon
  public static void writeNoClose(OutputStream out, String content) {

    try {
      out.write(content.getBytes(DEFAULT_CHARSET));
    } catch (Exception ex) {
      Exceptions.handle(ex);
    }
  }
Exemplo n.º 14
0
Arquivo: IO.java Projeto: nremond/boon
 public static InputStream inputStream(String resource) {
   Path path = path(resource);
   try {
     return Files.newInputStream(path);
   } catch (IOException e) {
     return Exceptions.handle(InputStream.class, "unable to open " + resource, e);
   }
 }
Exemplo n.º 15
0
Arquivo: IO.java Projeto: nremond/boon
  public static String read(Path path) {
    try {

      return read(Files.newBufferedReader(path, DEFAULT_CHARSET));

    } catch (IOException ex) {
      return Exceptions.handle(String.class, ex);
    }
  }
Exemplo n.º 16
0
Arquivo: IO.java Projeto: nremond/boon
  private static String readFromFileSchema(URI uri) {
    Path thePath = uriToPath(uri);

    try {
      return read(Files.newBufferedReader(thePath, DEFAULT_CHARSET));
    } catch (IOException e) {

      return Exceptions.handle(Typ.string, e); //
    }
  }
Exemplo n.º 17
0
Arquivo: IO.java Projeto: nremond/boon
  public static String readChild(Path parentDir, String childFileName) {
    try {

      final Path newFilePath = path(parentDir.toString(), childFileName);

      return read(newFilePath);
    } catch (Exception ex) {
      return Exceptions.handle(String.class, ex);
    }
  }
Exemplo n.º 18
0
Arquivo: IO.java Projeto: nremond/boon
  public static char[] readCharBuffer(Path path) {
    try {

      long bufSize = Files.size(path);
      return readCharBuffer(Files.newBufferedReader(path, DEFAULT_CHARSET), (int) bufSize);

    } catch (IOException ex) {
      return Exceptions.handle(char[].class, ex);
    }
  }
Exemplo n.º 19
0
Arquivo: IO.java Projeto: nremond/boon
  public static void eachLine(InputStream is, EachLine eachLine) {

    try (Reader reader = new InputStreamReader(is, DEFAULT_CHARSET)) {

      eachLine(reader, eachLine);

    } catch (Exception ex) {

      Exceptions.handle(ex);
    }
  }
Exemplo n.º 20
0
Arquivo: IO.java Projeto: nremond/boon
  public static List<String> readLines(InputStream is) {

    try (Reader reader = new InputStreamReader(is, DEFAULT_CHARSET)) {

      return readLines(reader);

    } catch (Exception ex) {

      return Exceptions.handle(List.class, ex);
    }
  }
Exemplo n.º 21
0
Arquivo: IO.java Projeto: nremond/boon
  public static List<String> readLines(Reader reader) {

    try (BufferedReader bufferedReader = new BufferedReader(reader)) {

      return readLines(bufferedReader);

    } catch (Exception ex) {

      return Exceptions.handle(List.class, ex);
    }
  }
Exemplo n.º 22
0
Arquivo: IO.java Projeto: nremond/boon
  public static void eachLine(Reader reader, EachLine eachLine) {

    try (BufferedReader bufferedReader = new BufferedReader(reader)) {

      eachLine(bufferedReader, eachLine);

    } catch (Exception ex) {

      Exceptions.handle(List.class, ex);
    }
  }
Exemplo n.º 23
0
Arquivo: IO.java Projeto: nremond/boon
  public static void writeChild(Path parentDir, String childFileName, String childContents) {

    try {

      final Path newFilePath = path(parentDir.toString(), childFileName);

      write(newFilePath, childContents);
    } catch (Exception ex) {
      Exceptions.handle(ex);
    }
  }
Exemplo n.º 24
0
  @Override
  public boolean test(Object o) {

    FieldAccess field = null;

    try {

      requireNonNull(o, "object under test can't be null");

      this.objectUnderTest = o;

      initIfNeeded();
      if (this.useDelegate) {

        this.nativeDelegate.fields = this.fields;
        this.nativeDelegate.objectUnderTest = this.objectUnderTest;
        return this.nativeDelegate.resolve(o);
      }

      field = field();

      if (!path && field == null && o instanceof Map) {
        return false;
      }

      boolean result = resolve(o);

      return result;

    } catch (Exception ex) {

      return Exceptions.handle(
          Typ.bool,
          sputl(
              "In class " + this.getClass().getName(),
              "the test method is unable to test the following criteria operator",
              String.valueOf(this.getOperator()),
              sputs("The field name is          :          ", this.getName()),
              sputs("The value is               :          ", this.getValue()),
              sputs("The value type is          :          ", this.getValue().getClass().getName()),
              sputs("The object under test      :          ", this.objectUnderTest),
              sputs(
                  "The object under test type :          ",
                  this.objectUnderTest == null
                      ? "null"
                      : this.objectUnderTest.getClass().getName()),
              sputs("Field                      :          ", field),
              sputs("Fields                     :          ", fields),
              sputs()),
          ex);
    }
  }
Exemplo n.º 25
0
Arquivo: IO.java Projeto: nremond/boon
  public static String readFromClasspath(Class<?> clazz, String location) {
    List<Path> resources = Classpaths.resources(clazz, location);

    if (len(resources) > 0) {
      try {
        return read(Files.newBufferedReader(resources.get(0), DEFAULT_CHARSET));
      } catch (IOException e) {
        return Exceptions.handle(String.class, "unable to read classpath resource " + location, e);
      }
    } else {
      return null;
    }
  }
Exemplo n.º 26
0
Arquivo: IO.java Projeto: nremond/boon
  public static Path createDirectory(String dir) {

    try {

      final Path newDir = path(dir);
      createDirectory(newDir);

      return newDir;

    } catch (Exception ex) {
      return Exceptions.handle(Path.class, ex);
    }
  }
Exemplo n.º 27
0
Arquivo: IO.java Projeto: nremond/boon
  public static long copyLarge(Reader reader, Writer writer, char[] buffer) {
    long count = 0;
    int n;

    try {
      while (EOF != (n = reader.read(buffer))) {
        writer.write(buffer, 0, n);
        count += n;
      }
    } catch (IOException e) {
      Exceptions.handle(e);
    }
    return count;
  }
Exemplo n.º 28
0
Arquivo: IO.java Projeto: nremond/boon
  public static char[] readCharBuffer(Reader reader, int size) {

    char[] buffer = new char[size];

    try (Reader r = reader) {

      reader.read(buffer);

    } catch (Exception ex) {
      return Exceptions.handle(char[].class, ex);
    }

    return buffer;
  }
Exemplo n.º 29
0
Arquivo: IO.java Projeto: nremond/boon
  public static Path createDirectory(Path dir) {

    try {

      if (!Files.exists(dir)) {
        return Files.createDirectory(dir);
      } else {
        return null;
      }

    } catch (Exception ex) {
      return Exceptions.handle(Path.class, ex);
    }
  }
Exemplo n.º 30
0
Arquivo: IO.java Projeto: nremond/boon
  public static List<String> listByGlob(Path pathFromFileSystem, String glob) {

    List<String> result = new ArrayList<>();

    try {
      try (DirectoryStream<Path> stream = Files.newDirectoryStream(pathFromFileSystem, glob)) {
        for (Path entry : stream) {
          result.add(entry.toAbsolutePath().toString());
        }
      }
      return result;
    } catch (IOException ex) {
      return Exceptions.handle(List.class, ex);
    }
  }